I've been writing a discord bot for a week now and tried claiming Discord Active Developer Badge today. I was surprised, because I couldn't claim it - I checked requirements and my bot needed to execute at least 1 command this month - certainly I completed it already. I researched it a bit deeper, and it seems that discord encourages people to use slash-commands and not prefix-based commands as I did with my bot. Here's an example of it:
# Prefix based
import discord
from discord.ext import commands
permissions = discord.Intents()
permissions.message_content = True
permissions.messages = True
permissions.reactions = True
bot = commands.Bot(command_prefix="/", intents=permissions)
@bot.command()
async def test(ctx: commands.Context):
await ctx.send("Registered!")
bot.run(TOKEN_HERE)
After a good amount of time, I couldn't figure out how to create slash commands using regular discord libraries, but I found an "Interactions" library, with which, I could define a so called command schema and do something with it. Here's an example:
# Slash based
import discord
import interactions
bot = interactions.Client(TOKEN_HERE)
@bot.command(
name="testing",
description="testing_command!"
)
async def test(ctx: interactions.CommandContext):
await ctx.send("Registered!")
bot.start()
This is considered slash command, it even creates a helping information in the discord itself:
The question is, should I continue developing with commands.Bot
bot or switch to the interactions
library and learn it instead? Can I migrate my currently existing bot somehow or add a so-called schema without using interactions library?
It's just annoying that I found out about it when I'm already really familiar with regular discord
library
The discord
library does have in-built slash commands with app_commands.CommandTree. Here's an example of that
import discord
from discord import app_commands
class MyClient(discord.Client):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
async def on_ready(self):
print('Ready')
client = MyClient(intents=discord.Intents.default())
tree = app_commands.CommandTree(client)
@tree.command(name='mycommand')
async def mycommand(interaction: discord.Interaction):
await interaction.response.send_message('Hello World!')
# Important to note the difference between Interaction and InteractionResponse
Short answer: Yes
Long answer: Slash commands are more versatile than normal prefix commands. Although typing out prefix commands is faster and more convenient, the advantages of slash commands outweigh that of prefix commands. Discord would also be more focused on developing the library with slash commands than prefix commands so you would expect more features to be catered towards slash commands in the future.
Useful features of slash commands include, limiting user input, better handling of input arguments, and much more.
I've personally never used the interactions
library and stuck with discord
. However, looking at your code example, it seems like an easy way to convert prefix commands to slash commands.
From the documentation of Interactions library
Even if it is working, we strongly advise against using d.py with our library.
Additionally, we will not give help for any issue resulting from using d.py with our library except of “Do not use discord.py”.
The developers of the Interactions library discourage the use of discord.py which makes it unlikely they want to work with them.
For the forseeable future, it might be better to use discord.py because it is meant for discord. However, I feel that this comes down to personal preference but it would be better to choose only one of the two instead of both.
For more information on slash commands using discord.py, discord.Interaction, discord.app_commands.CommandTree, discord.Client