Search code examples
pythondiscorddiscord.pybots

Problems with buttons and forms


I need to make a function which makes custom temporary voice channels and a text channel where user can setup his private room.

@client.event
async def on_ready():
    print('bot online')
    await private_rooms_menu()
#...
async def private_rooms_menu():
    private_rooms_menu_embed = discord.Embed(title="Private room menu")
    edit_private_rooms_channel = client.get_channel(EDIT_PRIVATE_ROOMS_ID)
    await edit_private_rooms_channel.purge(limit=None)

    menu_buttons = discord.ui.View()
    btn_change_private_room_name = discord.ui.Button(label='Change name', style=discord.ButtonStyle.primary)
    btn_private_room_open = discord.ui.Button(label='Open', style=discord.ButtonStyle.grey)
    btn_private_room_lock = discord.ui.Button(label='Lock', style=discord.ButtonStyle.grey)
    btn_private_room_give_access = discord.ui.Button(label='Give access', style=discord.ButtonStyle.green)
    btn_private_room_kick = discord.ui.Button(label='Kick', style=discord.ButtonStyle.red)

    menu_buttons.add_item(item=btn_change_private_room_name)
    menu_buttons.add_item(item=btn_private_room_open)
    menu_buttons.add_item(item=btn_private_room_lock)
    menu_buttons.add_item(item=btn_private_room_kick)
    menu_buttons.add_item(item=btn_private_room_give_access)

    async def btn_change_private_room_callback(ctx):
        changed_private_room_name = ''
        private_name_modal = discord.ui.Modal(title="Private room changing:")
        name_modal = discord.ui.TextInput(label="Enter new channel name:",
                                          placeholder='*something interesting*',
                                          required=True,
                                          max_length=MAX_PRIVATE_ROOM_NAME_LENGTH)
        private_name_modal.add_item(name_modal)

        async def on_submit(changed_name: str):
            changed_name = name_modal

        await on_submit(changed_private_room_name)
        print(changed_private_room_name)

    async def btn_private_room_open_callback(ctx):
        return

    async def btn_private_room_lock_callback(ctx):
        return

    async def btn_private_room_give_access_callback(ctx):
        return

    async def btn_private_room_kick_callback(ctx):
        return

    btn_change_private_room_name.callback = btn_change_private_room_callback
    btn_private_room_open.callback = btn_private_room_open_callback
    btn_private_room_lock.callback = btn_private_room_lock_callback
    btn_private_room_give_access.callback = btn_private_room_give_access_callback
    btn_private_room_kick.callback = btn_private_room_kick_callback

    await edit_private_rooms_channel.send(embed=private_rooms_menu_embed, view=menu_buttons, silent=True)

This menu should be interactible for everyone, who is in his private room. The problem that the

async def btn_change_private_room_callback(ctx):

isn't working. No errors in pycharm, just "this interaction failed" in discord chat this interaction failed I tried to comletely delete everything from that function, just to leave print(), but same problem appered

Also, i think there are several mistakes in modal, i just don't realy know how to write it


Solution

  • async def btn_change_private_room_callback(ctx):
            changed_private_room_name = ''
            private_name_modal = discord.ui.Modal(title="Private room changing:")
            name_modal = discord.ui.TextInput(label="Enter new channel name:",
                                              placeholder='*something interesting*',
                                              required=True,
                                              max_length=MAX_PRIVATE_ROOM_NAME_LENGTH)
            private_name_modal.add_item(name_modal)
    
            await ctx.response.send_modal(private_name_modal)
    

    By the way, use interactions, ctx is confusing in this case