#How to apply buttons and a drop-down list to one message on disnake.py ?

1 messages · Page 1 of 1 (latest)

rose burrow
#

I am making a discord bot and I need to apply a button and a drop-down list to one message as shown below:
https://i.stack.imgur.com/iHmED.jpg
I'm trying to do this:

    def __init__(self):
        # The details of the modal, and its components
        components = [
            disnake.ui.TextInput(
                label="Изменение статуса",
                placeholder="Введите ваш новый статус",
                custom_id="customStatus",
                style=TextInputStyle.short,
                max_length=12,
            )
        ]
        super().__init__(title="Новый статус", components=components)

    async def callback(self, inter: disnake.ModalInteraction):
        customStatusName = str(inter.text_values['customStatus'])
        # print(status)
        cursor.execute('UPDATE users SET customStatus = "{}" WHERE id = {}'.format(f"{customStatusName}", inter.author.id))
        connection.commit()
        await inter.send("Вы успешно поменяли статус!", ephemeral=True)

class EditStatus(disnake.ui.View):
    def __init__(self):
        super().__init__()
        self.value: Optional[bool] = None

    @disnake.ui.button(label="Изменить статус", style=disnake.ButtonStyle.grey)
    async def cancel(self, button: disnake.ui.Button, inter: disnake.MessageInteraction):
        await inter.response.send_modal(modal=MyModal())
        self.value = False

class Dropdown(disnake.ui.StringSelect):
    def __init__(self):
        options = [
            disnake.SelectOption(label="Сюрикен", description='значок на баннере'),
            disnake.SelectOption(label="Мечи", description='значок на баннере'),
            disnake.SelectOption(label="Корона", description='значок на баннере'),
            disnake.SelectOption(label="Цветок", description='значок на баннере'),
            disnake.SelectOption(label="Звезда", description='значок на баннере'),
        ]
        
        super().__init__(
            placeholder="Выбрать значок...",
            min_values=1,
            max_values=1,
            options=options
        )

    async def callback(self, inter: disnake.MessageInteraction):
        if self.values[0] == 'Сюрикен':
            cursor.execute('UPDATE users SET customBadge = "{}" WHERE id = {}'.format('knife', inter.author.id))
            connection.commit()
        if self.values[0] == 'Мечи':
            cursor.execute('UPDATE users SET customBadge = "{}" WHERE id = {}'.format('fight', inter.author.id))
            connection.commit()
        if self.values[0] == 'Корона':
            cursor.execute('UPDATE users SET customBadge = "{}" WHERE id = {}'.format('crown', inter.author.id))
            connection.commit()
        if self.values[0] == 'Цветок':
            cursor.execute('UPDATE users SET customBadge = "{}" WHERE id = {}'.format('flower', inter.author.id))
            connection.commit()
        if self.values[0] == 'Звезда':
            cursor.execute('UPDATE users SET customBadge = "{}" WHERE id = {}'.format('star', inter.author.id))
            connection.commit()

        await inter.response.send_message(f'Вы успешно поменяли значок на **{self.values[0]}**', ephemeral=True)

class DropdownView(disnake.ui.View):
    def __init__(self):
        super().__init__()
        self.add_item(Dropdown())

view = DropdownView(), EditStatus()
await inter.send(embed=profileEmbed, view=view)```

in the last lines I do it and it doesn't work out for me

Returns an error:
```Command raised an exception: AttributeError: 'tuple' object has no attribute 'to_components'```
#

In short, how do I use a drop-down list and buttons in one message using classes.

silk walrus
#

Put everything on one view, just space them on different rows

silk walrus
#

Buttons and dropdowns have row attribute/param

rose burrow
#

ouch.. I didn't know about it

silk walrus
rose burrow
#

@silk walrus , And I did everything, but now how can I apply it all to one message?

silk walrus
#

Send the message with a view

silk walrus
rose burrow
#

and i made that:

#
  await inter.send(embed=profileEmbed, view=view)```
#

but, didn't work

#

I am very confused

rose burrow
silk walrus
#

Why are you assigning two objects to view...?

rose burrow
silk walrus
#

No, firstly tell me why do you have two of them

#

What is EditStatus

rose burrow
#

Dropdown - select menu, EditStatus - buttons

#

select menu:

    def __init__(self):
        options = [
            disnake.SelectOption(label="Сюрикен", description='значок на баннере'),
            disnake.SelectOption(label="Мечи", description='значок на баннере'),
            disnake.SelectOption(label="Корона", description='значок на баннере'),
            disnake.SelectOption(label="Цветок", description='значок на баннере'),
            disnake.SelectOption(label="Звезда", description='значок на баннере'),
        ]
        
        super().__init__(
            placeholder="Выбрать значок...",
            min_values=1,
            max_values=1,
            options=options
        )

    async def callback(self, inter: disnake.MessageInteraction):
        if self.values[0] == 'Сюрикен':
            cursor.execute('UPDATE users SET customBadge = "{}" WHERE id = {}'.format('knife', inter.author.id))
            connection.commit()
        if self.values[0] == 'Мечи':
            cursor.execute('UPDATE users SET customBadge = "{}" WHERE id = {}'.format('fight', inter.author.id))
            connection.commit()
        if self.values[0] == 'Корона':
            cursor.execute('UPDATE users SET customBadge = "{}" WHERE id = {}'.format('crown', inter.author.id))
            connection.commit()
        if self.values[0] == 'Цветок':
            cursor.execute('UPDATE users SET customBadge = "{}" WHERE id = {}'.format('flower', inter.author.id))
            connection.commit()
        if self.values[0] == 'Звезда':
            cursor.execute('UPDATE users SET customBadge = "{}" WHERE id = {}'.format('star', inter.author.id))
            connection.commit()

        await inter.response.send_message(f'Вы успешно поменяли значок на **{self.values[0]}**', ephemeral=True)

class DropdownView(disnake.ui.View):
    def __init__(self):
        super().__init__()
        self.add_item(Dropdown())```
#

buttons :

    def __init__(self):
        # The details of the modal, and its components
        components = [
            disnake.ui.TextInput(
                label="Изменение статуса",
                placeholder="Введите ваш новый статус",
                custom_id="customStatus",
                style=TextInputStyle.short,
                max_length=12,
            )
        ]
        super().__init__(title="Новый статус", components=components)

    async def callback(self, inter: disnake.ModalInteraction):
        customStatusName = str(inter.text_values['customStatus'])
        # print(status)
        cursor.execute('UPDATE users SET customStatus = "{}" WHERE id = {}'.format(f"{customStatusName}", inter.author.id))
        connection.commit()
        await inter.send("Вы успешно поменяли статус!", ephemeral=True)

class EditStatus(disnake.ui.View):
    def __init__(self):
        super().__init__()
        self.value: Optional[bool] = None

    @disnake.ui.button(label="Изменить статус", style=disnake.ButtonStyle.grey, row=1)
    async def cancel(self, button: disnake.ui.Button, inter: disnake.MessageInteraction):
        await inter.response.send_modal(modal=MyModal())
        self.value = False```
silk walrus
#

There should be only one view

#

Put everything on one view

rose burrow
rose burrow
silk walrus
#

Why did you create two view classes

rose burrow
silk walrus
#

Yes

#

One view is responsible for everything

rose burrow
silk walrus
#

Just edit one of the existing classes by moving code from second one into it

rose burrow
#
    def __init__(self):
        super().__init__()
        self.value: Optional[bool] = None

    @disnake.ui.button(label="Изменить статус", style=disnake.ButtonStyle.grey, row=1)
    async def cancel(self, button: disnake.ui.Button, inter: disnake.MessageInteraction):
        await inter.response.send_modal(modal=MyModal())
        self.value = False

class Dropdown(disnake.ui.StringSelect):
    def __init__(self):
        options = [
            disnake.SelectOption(label="Сюрикен", description='значок на баннере'),
            disnake.SelectOption(label="Мечи", description='значок на баннере'),
            disnake.SelectOption(label="Корона", description='значок на баннере'),
            disnake.SelectOption(label="Цветок", description='значок на баннере'),
            disnake.SelectOption(label="Звезда", description='значок на баннере'),
        ]

        super().__init__(
            placeholder="Выбрать значок...",
            min_values=1,
            max_values=1,
            options=options
        )

    async def callback(self, inter: disnake.MessageInteraction):
        if self.values[0] == 'Сюрикен':
            cursor.execute('UPDATE users SET customBadge = "{}" WHERE id = {}'.format('knife', inter.author.id))
            connection.commit()
        if self.values[0] == 'Мечи':
            cursor.execute('UPDATE users SET customBadge = "{}" WHERE id = {}'.format('fight', inter.author.id))
            connection.commit()
        if self.values[0] == 'Корона':
            cursor.execute('UPDATE users SET customBadge = "{}" WHERE id = {}'.format('crown', inter.author.id))
            connection.commit()
        if self.values[0] == 'Цветок':
            cursor.execute('UPDATE users SET customBadge = "{}" WHERE id = {}'.format('flower', inter.author.id))
            connection.commit()
        if self.values[0] == 'Звезда':
            cursor.execute('UPDATE users SET customBadge = "{}" WHERE id = {}'.format('star', inter.author.id))
            connection.commit()

        await inter.response.send_message(f'Вы успешно поменяли значок на **{self.values[0]}**', ephemeral=True)

class DropdownView(disnake.ui.View):
    def __init__(self):
        super().__init__()
        self.add_item(Dropdown())```
silk walrus
#

Just move self.add_item from DropdownView to the init of EditStatus

#

That's all there is to it

rose burrow
#

THANK YOU, YOU ARE MY SAVIOR! Everything worked for me

rose burrow
silk walrus
#

Now we could talk about tidying up your code a bit, if that'd be of interest to you