How to implement Discord Slash Command in Python

Discord has recently implemented a new feature called the "slash command". Even if you look at the official reference, it's only in English, and even if you try to translate it, you can't understand it.

Therefore, I will understand the mechanism while implementing it easily using the library.

Standard specifications for slash commands

無題3_20210105114325.png

I thought it would be difficult to explain in words for a long time, so I prepared a diagram.

When the client uses the slash command, the bot is notified via the gateway with information such as "○○ used the command □□, the arguments are A, B, ...".

The bot will respond accordingly.

However, since this method is via WebSocket, it is difficult to handle the raw data sent.

Use discord-py-slash-command

The easiest and recommended way is to use a volunteer module. If you use this, you can concentrate on the command implementation and not get caught up in anything else.

python3 -m pip install discord-py-slash-command

Install the package with.

before that

The slash command cannot be used with the current bot settings.

You need to give the bot new permissions in the Discord Developer Portal (https://discord.com/developers/) and re-enter the server.

Make the necessary settings in the Developer Portal. Select the bot you want to set and follow the figure below.

無題4_20210106130237.png

There will be a URL for authentication under SCOPES.

無題7_20210106131134.png

From now on, you will be asked to install it via this URL.

Write code

import discord
from discord.ext import commands
from discord_slash import SlashCommand, SlashContext

bot = discord.Client(intents=discord.Intents.all())
#Or:
# bot = commands.Bot(command_prefix='@', intents=discord.Intents.all())

slashClient = SlashCommand(bot)

@slashClient.slash(name="hello")
async def _slash_hello(ctx: SlashContext):
    await ctx.send(content="Hello!")

@bot.event
async def on_ready():
  print('bot ready.')

bot.run("discord_token")

The code for a bot using the slash command looks like this:

slashClient = SlashCommand(bot)

This is an object dedicated to the slash command.

@slashClient.slash(name="hello")
async def _slash_hello(ctx: SlashContext):
    await ctx.send(content="Hello!")

Create a slash command with this code. Basically, the context passed to the function is the same as in Cog.

Only this.

Finally, I will devise something. When creating an object for SlashCommand

slashClient = SlashCommand(bot, auto_register=True)

If you set auto_register to True, the module will automatically register the slash command.

Commands that can be used on any server and those that cannot

I think that my bot will not come out even if I write a program, start it, and enter "/" on the test channel.

image.png

This is a specification, and the command you just registered is a "global command" that can be used on any server. It takes ** up to 1 hour ** from registration to use.

On the other hand, "server commands" that can only be used on a certain server are ** instantly reflected **.

There is also a limit to the number of registrations. I have summarized it in a table briefly.

type Time to reflect Maximum number
Global Command Up to 1 hour 50
Guild Command Immediate reflection 50

What do you think. The slash command has an explanation from the beginning and will politely tell you the arguments. Let's look forward to it in the future.

Notice

discord-py-slash-command is not an official Discord.py library, but was developed by volunteers. It is a public beta currently under development and may suddenly stop working or affect the behavior of your bot. Please understand the above points before using the module.

reference

discord-py-slash-command - PyPI https://pypi.org/project/discord-py-slash-command/

discord-py-slash-command official reference https://discord-py-slash-command.readthedocs.io/en/latest/

Discord Developer Portal Documentation Interactions: Slash Commands https://discord.com/developers/docs/interactions/slash-commands

Recommended Posts

How to implement Discord Slash Command in Python
How to receive command line arguments in Python
How to implement shared memory in Python (mmap.mmap)
How to notify a Discord channel in Python
How to develop in Python
[For beginners] How to use say command in python!
How to execute a command using subprocess in Python
How to collect images in Python
How to use SQLite in Python
In the python command python points to python3.8
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to handle Japanese in Python
How to implement Python EXE for Windows in Docker container
Discord in Python
I tried to implement PLSA in Python
[Introduction to Python] How to use class in Python?
I tried to implement permutation in Python
How to dynamically define variables in Python
How to do R chartr () in Python
[Itertools.permutations] How to put permutations in Python
I tried to implement PLSA in Python 2
[python] How to use __command__, function explanation
How to implement nested serializer in drf-flex-fields
How to work with BigQuery in Python
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to check opencv version in python
I tried to implement ADALINE in Python
I tried to implement PPO in Python
[Python / Tkinter] How to pass arguments to command
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
How to use regular expressions in Python
How to implement Scroll View in pythonista 1
How to display Hello world in python
How to use is and == in Python
How to write Ruby to_s in Python
How to get a string from a command line argument in python
Implement Enigma in python
How to use the C library in Python
[Python] How to test command line parser click
[REAPER] How to play with Reascript in Python
How to clear tuples in a list (Python)
How to install Python
How to generate permutations in Python and C ++
How to implement Rails helper-like functionality in Django
How to embed a variable in a python string
How to hide the command prompt when running python in visual studio 2015
Summary of how to import files in Python 3
How to simplify restricted polynomial fit in python
How to use Python Image Library in python3 series
How to create a JSON file in Python
Implement recommendations in Python
How to install python
Implement XENO in python