Home > other >  Sending modals data in a channel with pycord
Sending modals data in a channel with pycord

Time:03-16

i'm trying to do a suggestion modal in a cog for a pycord discord bot, but I want to send the datas of the modal then I get an error (like always)...

class SuggestModal(Modal):
    def __init__(self, bot) -> None:
        self.title = "New suggestion:"
        self.bot = bot
        self.add_item(InputText(label="Username", placeholder="e.g: Wumpus#0000"))
        self.add_item(
            InputText(
                label="Your suggestion",
                placeholder="What should we do for the bot?",
                style=discord.InputTextStyle.long,
            )
        )

    async def callback(self, interaction: discord.Interaction):
        embed = discord.Embed(title="Your Modal Results", color=discord.Color.random())
        embed.add_field(name="Username", value=self.children[0].value, inline=False)
        embed.add_field(name="Suggestion", value=self.children[1].value, inline=False)
        await interaction.response.send_message("Just sent the suggestion!",embed=embed)
        channel = await self.bot.get_channel(ID_HERE)
        embed1 = discord.Embed(title="Results", color=discord.Color.Blue())
        embed1.add_field(name="Username", value=self.children[0].value, inline=False)
        embed1.add_field(name="Suggestion", value=self.children[1].value, inline=False)
        await channel.interaction.response.send_message("New suggestion!",embed=embed1)

class Utilities(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.time = datetime.datetime.now()
        self.today_time = self.time.strftime(" • Aujourd'hui a %I:%M %p")

    @slash_command(name="suggest", description="Send a suggestion!")
    async def suggestion(self, ctx):
      suggest = SuggestModal()
      await ctx.interaction.response.send_modal(suggest)

def setup(bot):
    bot.add_cog(Utilities(bot))

ERROR:

discord.commands.errors.ApplicationCommandInvokeError: Application Command raised an exception: TypeError: init() missing 1 required positional argument: 'bot'

(The command is a slash command)

CodePudding user response:

You didn't to pass the bot parameter to the init() method of SuggestModal.

@slash_command(name="suggest", description="Send a suggestion!")
async def suggestion(self, ctx):
    suggest = SuggestModal(self.bot)
    await ctx.interaction.response.send_modal(suggest)

CodePudding user response:

The AttributeError: 'SuggestModal' object has no attribute 'children' error is because you have to initialize the parent class

    def __init__(self, bot) -> None:
        super().__init__(title="New suggestion:")
  • Related