Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import discord
- from discord.ext import commands
- from discord.ui import Button, View
- import random
- import json
- intents = discord.Intents.default()
- intents.message_content = True
- bot = commands.Bot(command_prefix='/', intents=intents)
- user_sessions = {}
- # questions
- questions = {
- "Algebra 1": [
- # reuglar stuff
- {"question": "Solve for x: 2x + 3 = 7", "answer": "x = 2"},
- # imagine more questions over here lol and in other subjects
- ]
- }
- # view for buttons
- class ProblemView(View):
- def __init__(self, subject, user_id):
- super().__init__(timeout=None)
- self.subject = subject
- self.user_id = user_id
- self.current_problem = 0
- async def on_timeout(self):
- pass
- # button itself
- @discord.ui.button(label="Show Answer", style=discord.ButtonStyle.primary, custom_id="show_answer")
- async def show_answer(self, interaction: discord.Interaction, button: Button):
- question_data = questions[self.subject][self.current_problem]
- answer_embed = discord.Embed(
- title="Math Problem",
- description=f"**Question:** {question_data['question']}\n\n**Answer:** {question_data['answer']}",
- color=discord.Color.green()
- )
- # keep buttons, update answer
- self.clear_items()
- self.add_item(Button(label="Next Problem", style=discord.ButtonStyle.success, custom_id="next_problem"))
- self.add_item(Button(label="Report Problem", style=discord.ButtonStyle.danger, custom_id="report_problem"))
- # Pass the updated view with the buttons
- await interaction.message.edit(embed=answer_embed, view=self)
- # to report problem
- @discord.ui.button(label="Report Problem", style=discord.ButtonStyle.danger, custom_id="report_problem")
- async def report_problem(self, interaction: discord.Interaction, button: Button):
- # Send the problem to the report channel
- problem = questions[self.subject][self.current_problem]
- channel = interaction.client.get_channel(channel id here lmao)
- if channel:
- await channel.send(f"Reported Problem: {problem['question']} | Answer: {problem['answer']}")
- await interaction.response.send_message("Problem reported!", ephemeral=True)
- else:
- await interaction.response.send_message("Could not find the report channel.", ephemeral=True)
- # next problem button
- @discord.ui.button(label="Next Problem", style=discord.ButtonStyle.success, custom_id="next_problem")
- async def next_problem(self, interaction: discord.Interaction, button: Button):
- try:
- # update
- self.current_problem += 1
- # loop
- if self.current_problem >= len(questions[self.subject]):
- self.current_problem = 0
- # get next prob
- problem_data = questions[self.subject][self.current_problem]
- # update msg to next prob
- problem_embed = discord.Embed(
- title="Math Problem",
- description=problem_data["question"],
- color=discord.Color.blue()
- )
- # keep buttons, edit msg
- await interaction.message.edit(embed=problem_embed, view=self)
- except Exception as e:
- print(f"Error: {e}")
- await interaction.response.send_message("There was an error processing your request. Please try again.", ephemeral=True)
- # slash command
- @bot.tree.command(name="problems")
- async def problems(interaction: discord.Interaction, subject: str):
- if subject not in questions:
- await interaction.response.send_message("Invalid subject! Please choose a valid subject.")
- return
- # session
- user_sessions[interaction.user.id] = {"subject": subject, "current_problem": 0}
- # make problem and send
- problem_data = questions[subject][0]
- embed = discord.Embed(
- title="Math Problem",
- description=problem_data["question"],
- color=discord.Color.blue()
- )
- # create view
- view = ProblemView(subject, interaction.user.id)
- await interaction.response.send_message(embed=embed, view=view)
- @bot.event
- async def on_ready():
- print(f'Logged in as {bot.user}')
- await bot.tree.sync() # Sync commands
- # running the bot
- bot.run('token')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement