- API Reference
- Frequently Asked Questions
- discord.py is dead! Will this library die too?
- Are you going to/will/consider fork(ing) discord.py?
- Will discord.py be able to work with this library?
- Where should we go with discord.py being gone?
- Why are you not supporting cooldowns?
- Will we not be able to create message commands?
- I’m getting “
AttributeError: HTTPClient not found!” when I try to execute helper methods!
- My question is not answered on here!
discord-interactions is a Python library for the Discord Artificial Programming Interface. (API) A library in Python has to be installed through the pip file. Run this in your terminal/command line in order to install our library:
pip install -U discord-py-interactions
Bots can be a little confusing to create. That’s why we’ve decided to try and make the process as streamlined as humanly possible, in order for it to be friendlier to understand for our fellow bot developers. Please note that a Discord bot should not be your first project if you’re learning how to code. There are plenty of other projects to consider first before this, as a Discord bot is not exactly beginner-friendly.
This code block below shows a simple bot being created:
1import interactions 2 3bot = interactions.Client(token="...") 4 firstname.lastname@example.org( 6 name="test", 7 description="this is just a test command.", 8 scope=1234567890 9) 10async def test(ctx): 11 await ctx.send("Hello world!") 12 13bot.start()
There’s quite a lot of things that are going on here, so let’s break it down step-by-step:
import interactions– This is the import line. If this returns a
ModuleNotFoundError, please look at our section on how to install here.
bot = interactions.Client(token="...")– This is the
botvariable that defines our bot. This basically instantiates the application client, which requires a
tokenkeyword-argument to be passed. In order to get a token, please look at the image given below.
@bot.command()– This is something known as a decorator in Python. This decorator is in charge and responsible of making sure that the Discord API is told about the slash/sub command that you wish to create, and sends an HTTP request correspondingly. Any changes to the information contained in this decorator will be synchronously updated with the API automatically for you. The
scopefield shown here is optional, which represents a guild command if you wish to have a command appear in only specific servers that bot is in. This can be a guild object or the ID.
async def test(ctx):– This here is called our “command coroutine,” or what our library internally calls upon each time it recognizes an interaction event from the Discord API that affiliates with the data we’ve put into the decorator above it. Please note that
ctxis an abbreviation for context.
bot.start()– Finally, this is what tells our library to turn your bot from offline to online.
Being able to run your own commands is very useful for a lot of automation-related purposes as a bot developer, however, we also have something that we’re able to introduce for both the developer and a user to use that will be the “sprinkles” on top of a cupcake, so-to-speak: components.
Components are ways of being able to select pre-defined data, or define your own. They’re very simple but quite powerful when put into practice This code block below shows a simplified implementation of a component:
1button = interactions.Button( 2 style=interactions.ButtonStyle.PRIMARY, 3 label="hello world!", 4 custom_id="hello" 5) 6 email@example.com( 8 name="test", 9 description="this is just a test command.", 10 scope=1234567890 11) 12async def test(ctx): 13 await ctx.send("testing", components=button) 14 firstname.lastname@example.org(button) 16async def button_response(ctx): 17 print("someone clicked the button! :O")
This is a design that we ended up choosing to simplify responding
to buttons when someone presses on one, and to allow bot developers
to plug in which button they want a response to. No more
wait_for functions with huge if-else chains; this removes
redundancy in your code and overall eases into the practice of modularity.
What kinds of components are there?¶
As a bot developer, this may be fairly important for you to want to know.
Different components provide difference user experiences, interactions
and results. Currently you can choose between two components that Discord
SelectMenu. You’re able to find these component
How do I send components in a row?¶
You are also able to organize these components into rows, which are defined
ActionRow’s. It is worth noting that you can have only a maximum of
5 per message that you send. This code block below shows how:
1button = interactions.Button( 2 style=interactions.ButtonStyle.PRIMARY, 3 label="hello world!", 4 custom_id="hello" 5) 6menu = interactions.SelectMenu( 7 options=[ 8 interactions.SelectOption(label="Option one", value="o-one"), 9 interactions.SelectOption(label="Option two", value="o-two"), 10 interactions.SelectOption(label="Option three", value="o-three") 11 ] 12) 13row = interactions.ActionRow( 14 components=[button, menu] 15) 16 email@example.com(...) 18async def test(ctx): 19 await ctx.send("rows!", components=row)
By default, the
components keyword-argument field in the context sending
method will always support
ActionRow-less sending: you only need to declare
rows whenever you need or want to. This field will also support raw arrays and
tables, if you so wish to choose to not use our class objects instead.