I’ve set up a Discord bot that reacts to commands and includes buttons for user input. However, when users click the button, an error message pops up stating that the interaction failed. Here’s a snippet of my code:
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="$", intents=intents)
@bot.event
async def on_ready():
print(f'{bot.user.name} is up and running!')
@bot.command()
async def suggestion(ctx):
await ctx.send("Press the button below to submit your suggestion.", view=SuggestionView())
class SuggestionView(discord.ui.View):
@discord.ui.button(label="Submit Your Suggestion", style=discord.ButtonStyle.primary)
async def submit_suggestion(self, button: discord.ui.Button, interaction: discord.Interaction):
try:
await interaction.response.send_modal(SuggestionModal())
except Exception as e:
print(f"Error showing modal: {e}")
await interaction.response.send_message("There was an issue trying to display the modal.", ephemeral=True)
class SuggestionModal(discord.ui.Modal):
def __init__(self):
super().__init__(title="Anonymous Suggestion")
self.add_item(discord.ui.InputText(label="What do you want to suggest?", style=discord.InputTextStyle.long))
async def on_submit(self, interaction: discord.Interaction):
suggestion = self.children[0].value
channel = discord.utils.get(interaction.guild.channels, name="suggestions")
if channel:
try:
await channel.send(f"New Anonymous Suggestion:\n{suggestion}")
await interaction.response.send_message("Your suggestion was submitted successfully!", ephemeral=True)
except Exception as e:
print(f"Error sending suggestion: {e}")
await interaction.response.send_message("Something went wrong while submitting your suggestion.", ephemeral=True)
else:
await interaction.response.send_message("The suggestions channel couldn't be found.", ephemeral=True)
@bot.event
async def on_application_command_error(ctx, error):
print(f"An error occurred: {error}")
bot.run('YOUR_BOT_TOKEN')
The bot functions properly and responds to the $suggestion command, showing the button. However, pressing the button results in an “interaction failed” message with no logs in the console. I’ve tried using different browsers and verified my firewall settings, but I’m still unable to resolve the issue. Any suggestions?