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.
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.
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.
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.
There will be a URL for authentication under SCOPES.
From now on, you will be asked to install it via this URL.
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.
I think that my bot will not come out even if I write a program, start it, and enter "/" on the test channel.
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.
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.
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