-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.py
101 lines (80 loc) · 3.56 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import json
import os
from dotenv import load_dotenv
import discord
from discord.ext import commands
import random
load_dotenv()
BOT_TOKEN = os.getenv('DISCORD_TOKEN')
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(intents=intents, command_prefix='!')
QUIZFILENAME = "quiz.json"
with open('quiz.json', 'r', errors='ignore') as file:
quiz_data = json.load(file)
questions = quiz_data["questions"]
file.close()
# Called once bot is ready for further action.
@bot.event
async def on_ready():
print(f'Logged in as {bot.user}')
await bot.tree.sync()
@bot.command()
@commands.is_owner()
async def sync(ctx):
await ctx.bot.tree.sync()
await ctx.send("Synced")
class CurrentQuestion():
def __init__(self):
self.current_question = None
self.options = None
self.current_question_index = None
def new_question(self):
self.current_question = random.choice(questions)
self.options = self.current_question["options"]
self.current_question_index = self.options.index(self.current_question["answer"])
class Buttons(discord.ui.View):
def __init__(self,embed):
super().__init__()
self.value = None
self.embed = embed
def disable_all_buttons(self):
for child in self.children:
child.disabled = True
async def handle_confirmation(self, interaction: discord.Interaction, value: int, button: discord.ui.Button):
self.disable_all_buttons()
if curr_question.current_question_index == value:
button.style = discord.ButtonStyle.green
else:
button.style = discord.ButtonStyle.red
self.stop()
self.embed.add_field(name=f'Correct answer: {curr_question.current_question["answer"]}', value="", inline=False)
await interaction.response.edit_message(view=self,embed=self.embed)
@discord.ui.button(label='1', style=discord.ButtonStyle.blurple)
async def one(self, interaction: discord.Interaction, button: discord.ui.Button):
await self.handle_confirmation(interaction, 0, button)
@discord.ui.button(label='2', style=discord.ButtonStyle.blurple)
async def two(self, interaction: discord.Interaction, button: discord.ui.Button):
await self.handle_confirmation(interaction, 1, button)
@discord.ui.button(label='3', style=discord.ButtonStyle.blurple)
async def three(self, interaction: discord.Interaction, button: discord.ui.Button):
await self.handle_confirmation(interaction, 2, button)
@discord.ui.button(label='4', style=discord.ButtonStyle.blurple)
async def four(self, interaction: discord.Interaction, button: discord.ui.Button):
await self.handle_confirmation(interaction, 3, button)
@discord.ui.button(label='5', style=discord.ButtonStyle.blurple)
async def five(self, interaction: discord.Interaction, button: discord.ui.Button):
await self.handle_confirmation(interaction, 4, button)
curr_question = CurrentQuestion()
@bot.tree.command(name="question")
async def question(interaction: discord.Interaction):
"""Gives a question"""
curr_question.new_question()
embed = discord.Embed(
title= 'Quiz Question',
description=curr_question.current_question["question"])
for i,c in enumerate(curr_question.options):
embed.add_field(name=f'{i+1}: {curr_question.options[i]}', value="", inline=False)
view = Buttons(embed=embed)
await interaction.response.send_message(embed=embed, view=view )
bot.run(BOT_TOKEN)