Перейти к содержимому

Как сделать кнопки discord py

  • автор:

Welcome to discord.py-message-components’ documentation!¶

Name of the Project (discord.py-message-components)

PyPI Logo

Visit on PyPI here

You need help? Or have ideas/feedback?¶

Open a Issue/Pull request on GitHub, join the support-Server or send me a direct-message on Discord: mccuber04#2960

Installing:¶

Python 3.5.3 or higher is required

This Library overwrite the original discord.py Library so to be sure all will work fine first uninstall the original discord.py Library if it is installed:

Then install this Library using:

quickstart¶

Sending-buttons¶

Interact when a button was pressed¶

You could set the parameter hidden in the response to True to make the message ephemeral. See discord.Interaction.respond for more information about respond() .

Sending-SelectMenu’s and respond to them¶

A coroutine is a function that must be invoked with await or yield from . When Python encounters an await it stops the function’s execution at that point and works on other things until it comes back to that point and finishes off its work. This allows for your program to be doing multiple things at the same time without using threads or complicated multiprocessing.

If you forget to await a coroutine then the coroutine will not run. Never forget to await a coroutine.

Buttons

On May 26, 2021, Discord added a new interaction called buttons. Instead of reactions, bots could now send buttons and users could use them to interact with bots. This opened up a whole new world of possibilities for bots. Soon after, developers made calculators, polls, and games like blackjack, UNO, and even Minecraft! Buttons provided a clear and easy to use interface for interacting with bots.

So, let's learn how you can add buttons to your bot!

Concept​

Buttons weren't the only update to the interactions system in Discord. Discord also added Select Menus and Modal Dialogs, both of which work very similarly to buttons.

These UI elements reside in a "view". To learn more about views, please refer to the interactions page.

Usage Syntax​

Let's see how to create a simple responsive button.

Using this command should return the following message:

BobDotCom used / button

As you can see, we create a class called MyView that subclasses discord.ui.View .

Then, we add a function called button_callback to the MyView class with the decorator discord.ui.button . This decorator adds a button to a component. This function takes two arguments: the button that was clicked and the interaction. These arguments are passed to the function when the button is clicked by the module. We use the interaction.response.send_message function to send a message to the channel where the interaction was sent.

Finally, we create a global slash command called button that sends the message, along with the view that contains a button.

This is the basic syntax of creating a button. What you create with it is up to you. You can worry about making your button do amazing things, while Pycord handles the rest!

Button Styles​

Name Usage Color
Primary discord.ButtonStyle.primary / discord.ButtonStyle.blurple Blurple
Secondary discord.ButtonStyle.secondary / discord.ButtonStyle.grey / discord.ButtonStyle.gray Grey
Success discord.ButtonStyle.success / discord.ButtonStyle.green Green
Danger discord.ButtonStyle.danger / discord.ButtonStyle.red Red
Link discord.ButtonStyle.link / discord.ButtonStyle.url Grey

Check out the discord.ButtonStyle class for more information.

You can set a button's style by adding the style argument in the discord.ui.button decorator.

Action Rows​

We have discussed that Views can have 5 rows. Each row has 5 slots, and each button takes up 1 slot. So, how do we move the buttons to another row?

This can be done by specifying the row argument in the discord.ui.button decorator.

The row argument

The row argument specifies the relative row this button belongs to. A Discord component can only have 5 rows. By default, items are arranged automatically into those 5 rows. If you’d like to control the relative positioning of the row then passing an index is advised. For example, row=1 will show up before row=2. Defaults to None, which is automatic ordering. The row number must be between 0 and 4 (i.e. zero indexed).

Disabling Buttons​

Pre-Disabled Buttons​

Disabling Buttons on Press​

  • Disabling a single component
  • Disabling all the components of a view

Timeouts​

Sometimes, you want to have a button that is disabled after a certain amount of time. This is where timeouts come in.

  • Specifying time when creating a view object
  • Specifying the time when subclassing

Here, we disable all buttons and select menus in the view. Then, we edit the message to show that the timeout was reached.

If the on_timeout coroutine is not present, the components will simply stop working after the specified time.

Persistent Views​

Sometimes, instead of a button that is disabled after a certain amount of time, you want to have a button that is always working.

Normally, when the bot goes offline, all of its buttons stop working. You will be able to see the buttons, but nothing will happen when you press them. This is a problem if you are trying to create a self-role system with buttons, for example. This is where persistent views come in.

Persistent views work forever. When the bot goes offline, the buttons will stop working. When the bot comes back online, however, the buttons will start working again.

In a Persistent View, the timeout must be set to None and all the children in the view much have a custom_id attribute set.

How many buttons can I have in a message?​

Each message can have a maximum of 25 buttons. Views can have up to 5 rows, and each row has 5 slots. A button takes up one slot, while a select menu takes up all five slots.

Can I add more than one view to a message?​

No. As a Discord limitation, you can only have one view per message.

Why are UI Components so confusing?​

They cannot be simple like commands. This system makes them flexible and doesn't limit your imagination. There are loads of different ways you can use UI Components. For example, you could subclass Buttons or Select Menus and add them to a view using the view's add_item function.

UI Components aren't hard to use if you know Python. We recommend learning Object-Oriented Programming with Python.

What is OOP? What is subclassing? ​

OOP (object-oriented programming) is a programming paradigm that allows you to create objects that have their own properties and methods. Almost everything in python is an object or a class. discord.Embed and discord.ui.View are both classes. When you use view = discord.ui.View() to create a view, you are actually creating an object of type discord.ui.View .

Subclassing is a Python OOP concept. It means that you can create a class that inherits from another class. In other words, the class that subclasses another class can inherit all the methods and attributes of that class.

We highly recommend you learn about basic Python concepts like classes and inheritance before you start learning Pycord.

Resources:

Do buttons need any special permissions?​

No new permissions are needed for either the bot or the server to allow bots to use buttons.

Should I replace reactions with buttons for my bot?​

That is up to you. Buttons do provide a cleaner interface for your bot and are easier to use.

How to add Buttons to your command [interactions.py]

Last time, we created a simple slash command with interactions.py. However, if you go around Discord and use different bots, you will see that there are some commands that use buttons to let you interact with the bot. This is pretty cool, and you may want to do the same, so let’s see how it can be done with interactions.py.

Disclaimer: I will create a new sample file for this post, however, you can add it in your main one if you like.

Assuming that you have already imported the main library, declare the client and start the bot from the previous post, we will walk straight in with creating button.

Button

First, let’s create a global variable for the button.

Exit fullscreen mode

  • interactions.Button : This object forms the button that we will send to Discord API.
  • style : The style of the button. Currently, Discord supports a variety of styles that you can use. You can have a look in this image below. alt=»Component_type» width=»849″ height=»508″ />
  • label : The label of the button. This is the text that displays on the button.
  • custom_id : This is the customized ID that we will receive when someone clicks on the button. We will need this for the component callback to do the job we want to.

We have a button now, so what’s next? Send it, of course.

Exit fullscreen mode

  • components=button : components is used to form the component in the message.

This_interaction_failed

Run the bot and use the command. If you see something like this, congratulations, you just create a button with interactions.py, but hold on, when you click on it, nothing happens and you get a «This interaction failed».

The reason for this is because we haven’t added a button callback yet. When we click on the button, it creates an INTERACTIONS_CREATE dispatch gateway event and we need a button callback to handle that event.

Exit fullscreen mode

  • @client.component() : This is the decorator that handles specifically component event. click_me is the custom_id that we create above. What this will do is listening to the INTERACTIONS_CREATE dispatch gateway event and see if it matches with the custom_id that we create.

Success

Now that we have a button, a command and a callback event, let’s run the bot again and test it. If you have something like this, congratulations, you just create a functioning command with button that you can interact with interactions.py.

Action Rows

Action Rows is a way to organize your components into rows. According to Discord, you can have up to 5 Action Rows per message and each Action Row cannot contain another Action Row.

Now that you have a good look with Action Rows, let’s do it with interactions.py.

Exit fullscreen mode

  • interactions.ActionRow() : this object is responsible for creating an Action Row and send it to Discord.
  • components : this is a list of Button that we will send to Discord API.

ActionRow

Once you finish with copying the above code into your bot file, run the bot and execute the command. If you see something like this, congratulations, you just created an Action Row with interactions.py.

Add button components to a message (discord.py)

I was wondering after seeing this (message components) on discord’s API reference if there was any way to implement it using python ?

I tried making a json array and passing it in my message but couldn’t make it work.

I also tried looking on the reference for python but couldn’t find anything.

If you have any informations thank you if you share it.

Dominik's user avatar

7 Answers 7

New Answer

Discord.py 2.0 Allows for use of Buttons and Dropdowns, and has new added support for Slash Commands. A 3rd party repository is no longer necessary. However, if you don’t want to use the default one for some reason, you can checkout discord_slash .

To upgrade to Discord.py 2.0:

MacOS and Linux:

Old Answer:

(This Answer is OUTDATED.)

As of right now, you can get a library called discord_components to use buttons.

To install this library, use pip install —upgrade discord-components (Sometimes the command would be pip3 install —upgrade discord-components ).

To import Discord Component Buttons, use

Then just add this code in the bot’s on_ready() :

(Make sure to replace bot with the name of your bot, the same one you use for @something.command() )

To add a button to a message, do this:

(A message is required)

To do something when the button is clicked, you can do something like this:

This method even survives reboots!

Here’s an example I put together for you if you need it:

Hope this Helps!

Tip: If you want the buttons to be in one row, use [[]] instead of just [] for Example: [[btn1, btn2],[btn3, btn4]] will result in:

Extra Tip: You can also set a variable as a button then send the variable

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *