I'm trying to make a discord bot for a small server. When I try to run this code, I get the error: AttributeError: 'diff_btns' object has no attribute ctx
when I try to run my code.
Code:
#Import libraries:
from discord.interactions import Interaction
from discord.ui import Button, View
import discord
import quiz
'''
code to get question:
quiz.get_data(quiz.diff1)
await ctx.respond(quiz.q_output)
'''
bot = discord.Bot()
@bot.event
async def on_ready():
print(f'We have logged in as {bot.user}')
class diff_btns(discord.ui.View):
@discord.ui.button(label="Easy", row=0, style=discord.ButtonStyle.blurple)
async def button_callback1(self, button, interaction):
await interaction.response.send_message("You picked easy")
for child in self.children: # loop through all the children of the view
child.disabled = True # set the button to disabled
await interaction.response.edit_message(view=self)
@discord.ui.button(label="Medium", row=0, style=discord.ButtonStyle.blurple)
async def button_callback2(self, button, interaction):
await interaction.response.send_message("You picked medium")
for child in self.children: # loop through all the children of the view
child.disabled = True # set the button to disabled
await interaction.response.edit_message(view=self)
@discord.ui.button(label="Hard", row=0, style=discord.ButtonStyle.blurple)
async def button_callback3(self, button, interaction):
await interaction.response.send_message("You picked hard")
for child in self.children: # loop through all the children of the view
child.disabled = True # set the button to disabled
await interaction.response.edit_message(view=self)
#Interaction checks:
async def interaction_check(self, interaction) -> bool:
if interaction.user != self.ctx.author:
await interaction.response.send_message("Use /gkquiz to make your own quiz.", ephemeral=True)
return False
else:
return True
#Quiz question:
@bot.command(description="Starts a general knowledge quiz.")
async def quizgk(ctx):
#Send message
await ctx.respond("How difficult do you want the questions to be?", view=diff_btns())
bot.run()
The code is meant to send a message with the 3 buttons, then send another message based on which button was pressed. Thanks in advance for any help.
Ignoring exception in view <diff_btns timeout=180.0 children=3> for item <Button style=<ButtonStyle.primary: 1> url=None disabled=False label='Medium' emoji=None row=0>:
Traceback (most recent call last):
File "C:\Users\zadok\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ui\view.py", line 410, in _scheduled_task
allow = await self.interaction_check(interaction)
File "c:\Users\zadok\Documents\InterCity Bot\main.py", line 48, in interaction_check
if interaction.user != self.ctx.author:
AttributeError: 'diff_btns' object has no attribute 'ctx'