Advertisement
Guest User

Untitled

a guest
Jan 15th, 2025
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. import discord
  2. from discord.ext import commands
  3. from discord.ui import Button, View
  4. import random
  5. import json
  6.  
  7. intents = discord.Intents.default()
  8. intents.message_content = True
  9. bot = commands.Bot(command_prefix='/', intents=intents)
  10. user_sessions = {}
  11. # questions
  12. questions = {
  13. "Algebra 1": [
  14. # reuglar stuff
  15. {"question": "Solve for x: 2x + 3 = 7", "answer": "x = 2"},
  16. # imagine more questions over here lol and in other subjects
  17. ]
  18. }
  19.  
  20. # view for buttons
  21. class ProblemView(View):
  22. def __init__(self, subject, user_id):
  23. super().__init__(timeout=None)
  24. self.subject = subject
  25. self.user_id = user_id
  26. self.current_problem = 0
  27.  
  28. async def on_timeout(self):
  29. pass
  30.  
  31. # button itself
  32. @discord.ui.button(label="Show Answer", style=discord.ButtonStyle.primary, custom_id="show_answer")
  33. async def show_answer(self, interaction: discord.Interaction, button: Button):
  34. question_data = questions[self.subject][self.current_problem]
  35. answer_embed = discord.Embed(
  36. title="Math Problem",
  37. description=f"**Question:** {question_data['question']}\n\n**Answer:** {question_data['answer']}",
  38. color=discord.Color.green()
  39. )
  40.  
  41. # keep buttons, update answer
  42. self.clear_items()
  43. self.add_item(Button(label="Next Problem", style=discord.ButtonStyle.success, custom_id="next_problem"))
  44. self.add_item(Button(label="Report Problem", style=discord.ButtonStyle.danger, custom_id="report_problem"))
  45. # Pass the updated view with the buttons
  46. await interaction.message.edit(embed=answer_embed, view=self)
  47.  
  48. # to report problem
  49. @discord.ui.button(label="Report Problem", style=discord.ButtonStyle.danger, custom_id="report_problem")
  50. async def report_problem(self, interaction: discord.Interaction, button: Button):
  51. # Send the problem to the report channel
  52. problem = questions[self.subject][self.current_problem]
  53. channel = interaction.client.get_channel(channel id here lmao)
  54. if channel:
  55. await channel.send(f"Reported Problem: {problem['question']} | Answer: {problem['answer']}")
  56. await interaction.response.send_message("Problem reported!", ephemeral=True)
  57. else:
  58. await interaction.response.send_message("Could not find the report channel.", ephemeral=True)
  59.  
  60. # next problem button
  61. @discord.ui.button(label="Next Problem", style=discord.ButtonStyle.success, custom_id="next_problem")
  62. async def next_problem(self, interaction: discord.Interaction, button: Button):
  63. try:
  64. # update
  65. self.current_problem += 1
  66.  
  67. # loop
  68. if self.current_problem >= len(questions[self.subject]):
  69. self.current_problem = 0
  70.  
  71. # get next prob
  72. problem_data = questions[self.subject][self.current_problem]
  73.  
  74. # update msg to next prob
  75. problem_embed = discord.Embed(
  76. title="Math Problem",
  77. description=problem_data["question"],
  78. color=discord.Color.blue()
  79. )
  80.  
  81. # keep buttons, edit msg
  82. await interaction.message.edit(embed=problem_embed, view=self)
  83.  
  84. except Exception as e:
  85. print(f"Error: {e}")
  86. await interaction.response.send_message("There was an error processing your request. Please try again.", ephemeral=True)
  87.  
  88. # slash command
  89. @bot.tree.command(name="problems")
  90. async def problems(interaction: discord.Interaction, subject: str):
  91. if subject not in questions:
  92. await interaction.response.send_message("Invalid subject! Please choose a valid subject.")
  93. return
  94.  
  95. # session
  96. user_sessions[interaction.user.id] = {"subject": subject, "current_problem": 0}
  97.  
  98. # make problem and send
  99. problem_data = questions[subject][0]
  100. embed = discord.Embed(
  101. title="Math Problem",
  102. description=problem_data["question"],
  103. color=discord.Color.blue()
  104. )
  105.  
  106. # create view
  107. view = ProblemView(subject, interaction.user.id)
  108. await interaction.response.send_message(embed=embed, view=view)
  109.  
  110. @bot.event
  111. async def on_ready():
  112. print(f'Logged in as {bot.user}')
  113. await bot.tree.sync() # Sync commands
  114.  
  115. # running the bot
  116. bot.run('token')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement