Im getting someting went wrong when i hit submit on my modal on discord? Anyone knows why?
I tried to add some error handeling but i get nothing in my terminal!
The code is inside a cog and is pasted below:
The button, and form is showing before and when i fill out and hit submit it shows the error
import discord
from discord.ext import commands
from discord import app_commands
from discord.ui import TextInput, Button, View, Modal
class StaffApplicationModal(Modal):
def __init__(self):
super().__init__(title="Staff Application Form")
self.add_item(TextInput(label="Your Name", placeholder="Enter your full name"))
self.add_item(TextInput(label="Duration in Server", placeholder="How long have you been in the server?"))
self.add_item(TextInput(label="Reason for Applying", placeholder="Why do you want to be staff?"))
self.add_item(TextInput(label="Previous Experience", placeholder="Describe any relevant previous experience"))
async def callback(self, interaction: discord.Interaction):
try:
responses = [
f"**Name:** {self.children[0].value}",
f"**Duration in Server:** {self.children[1].value}",
f"**Reason for Applying:** {self.children[2].value}",
f"**Previous Experience:** {self.children[3].value}"
]
application_review = "**New Staff Application:**nn" + "n".join(responses)
application_channel_id = 1252323547091566712
application_channel = interaction.client.get_channel(application_channel_id)
if application_channel:
try:
await interaction.response.send_message(application_review)
except Exception as e:
print(f"Failed to send message: {str(e)}")
await interaction.response.send_message("Failed to send application review to the channel.", ephemeral=True)
else:
print("Failed to find application channel.") # This will confirm if there's an issue with the channel ID
await interaction.response.send_message("Application channel not found or bot lacks permissions.", ephemeral=True)
except Exception as e:
print(f"An error occurred: {str(e)}") # Logging the exception
await interaction.response.send_message(f"An error occurred: {str(e)}", ephemeral=True)
class ApplyButton(Button):
def __init__(self):
super().__init__(label="Apply for Staff", style=discord.ButtonStyle.green, custom_id="apply_staff")
async def callback(self, interaction: discord.Interaction):
modal = StaffApplicationModal()
await interaction.response.send_modal(modal)
class StaffApplication(commands.Cog):
def __init__(self, bot):
self.bot = bot
@app_commands.command(name='post_application_button', description="Post the application button in the channel.")
@app_commands.checks.has_permissions(administrator=True)
async def post_application_button(self, interaction: discord.Interaction):
"""Posts a button to start the application process."""
view = View()
view.add_item(ApplyButton())
await interaction.response.send_message("Click the button below to apply for staff!", view=view, ephemeral=True)
@app_commands.command(name='test_modal', description="Test the modal functionality.")
async def test_modal(self, interaction: discord.Interaction): # Added 'self' here
modal = StaffApplicationModal()
await interaction.response.send_modal(modal)
async def setup(bot):
staff_app = StaffApplication(bot)
await bot.add_cog(staff_app)
bot.tree.add_command(staff_app.post_application_button, guild=discord.Object(id=959220051427340379))
bot.tree.add_command(staff_app.test_modal, guild=discord.Object(id=959220051427340379))