Home>

I wanted to make code for requests to the server using buttons, but I ran into a problem that in @bot.event I need inter and I need ctx, but when you add ctx it gives an error that there is no argument, and when there is no ctx, then problems with the code, because it is used there. Is it possible to somehow replace this or rewrite a little code? I will be grateful for any help. The code itself:

@bot.event
async def on_button_click(inter, ctx):
    if inter.component.id== 'zay1':
        ticket_created_embed= discord.Embed(
        title="Application",
        description=f"""Greetings, {ctx.author.name}! Send your **tag*\n to the chat and wait for the administration to reply.""", color=0xff00ff
    )
        ticket_created_embed.set_footer(text='Congratulations server administration')
        overwrites= {
        guild.default_role: discord.PermissionOverwrite(view_channel=False),
        guild.me: discord.PermissionOverwrite(view_channel=True)}
        ticket= await ticket_category.create_text_channel(
        f"{ctx.author.name}-{ctx.author.discriminator}", overwrites=overwrites
    )
        await inter.reply(f'#{ctx.author.name}-{ctx.author.discriminator} -your request channel.', ephemeral=True)
        row= ActionRow(Button(style=ButtonStyle.red, label='Close', custom_id='zay2'))
        await ticket.send(ctx.author.mention, embed=ticket_created_embed, components=[row])
             if inter.component.id== 'zay2':
                    await ctx.channel.delete()
  • Answer # 1

    the point is that you need to separate the code where to be usedctx, It's a good idea to write all functions related to ctxin one function, and everything related tointerto another function

    @bot.event
      async def on_button_click(ctx):
            code here
    @bot.event
      async def on_button_click(inter):
            code here
    

    If I split the code, how can I track which button was clicked by the user so that I can create a channel after clicking it?

    Toxa1432022-02-03 09:33:43
  • Answer # 2

    the point is that you need to separate the code where to be usedctx, It's a good idea to write all functions related to ctxin one function, and everything related tointerto another function

    @bot.event
      async def on_button_click(ctx):
            code here
    @bot.event
      async def on_button_click(inter):
            code here
    

    If I split the code, how can I track which button was clicked by the user so that I can create a channel after clicking it?

    Toxa1432022-02-03 09:33:43