In order to add slash commands to your Discord bot, you will need to use the discord_slash library instead of the discord.ext.commands library.
Here’s an updated version of your code that shows how to add a simple slash command:
import os
import discord
from discord.ext import commands
from discord_slash import SlashCommand, SlashContext
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)
slash = SlashCommand(bot, sync_commands=True)
@bot.event
async def on_ready():
print(f"Logged in as {bot.user}")
@slash.slash(name="ping")
async def _ping(ctx: SlashContext):
embed = discord.Embed(title="Pong!", color=0xC9E358)
await ctx.send(embed=embed)
bot.run(os.environ["DISCORD_TOKEN"])
Here, we first import the SlashCommand and SlashContext classes from the discord_slash library.
Then, we create a new instance of the SlashCommand class, passing in our main bot instance and setting sync_commands=True. This will ensure that any new slash commands we add are properly registered with Discord.
Next, we define a new slash command with the @slash.slash() decorator. In this case, we’re creating a command named “ping”.
Finally, inside our _ping command function, we create an embed just like before and send it using the ctx.send() method, except this time we’re using the ctx object of type SlashContext.
With these changes, you should be able to add more slash commands to your bot by creating additional functions decorated with @slash.slash().
As this resolves your primary issue you might as well mark the AI’s response as solution, if you would like to continue asking it questions relating to the answer it responded with you may ask it questions in dms.
Great point! To make your newly added slash commands appear on your Discord server, you will need to run the command /deploy after updating your code.
With the discord_slash library, you need to register your slash commands before they appear on Discord and the /deploy command allows you to do just that.
Here’s an updated code to include the register_slash_commands() function and the /deploy command:
The register_slash_commands() function utilizes the slash.sync_all_commands() method to register all the slash commands specified in your code.
Then, inside the on_ready() method, we call the register_slash_commands() function to register these slash commands on your Discord server.
Lastly, you can run the /deploy command once the code is up and running, to deploy your commands. You only need to run this command once, subsequent changes made to your commands will be overwritten during a new deployment.
The code looks good to me! However, don’t forget to add your Discord bot token to your environment variables as “DISCORD_TOKEN”.
Also, just a small note: if you have multiple events you want to catch, using the @bot.event decorator for each event can become tedious. Instead, you can use the bot.event method as a decorator for multiple events, like so:
@bot.event
async def on_ready():
print(f"Logged in as {bot.user}")
await register_slash_commands()
@bot.event
async def on_member_join(member):
# your code here
This eliminates redundant code and makes your code easier to read.
If you received a “Failed to fetch” error, it could mean that your hosting environment is having trouble accessing the URLs required to download necessary files and packages.
Here are a few things you can try to resolve the issue:
Check your internet connectivity: Ensure that your hosting environment has a stable internet connection and is not experiencing connectivity issues.
Check the package manager: Depending on what package manager you are using, you may be able to try different mirrors to see if there is one that is more reliable.
Retry the installation: Sometimes, the error may be a temporary issue with a remote server, so it’s worth trying again after some time has passed.
Use a VPN: If you’re able to, try using a Virtual Private Network (VPN) to connect to your hosting environment. This can help bypass any network restrictions that may be causing the issue.
Contact the hosting provider: If the issue persists, you may need to contact the hosting provider’s support team for further assistance. They should be able to help you diagnose and resolve the issue.
If your bot is still offline, there could be a few possible reasons why:
Incorrect environment variables: Make sure that you’ve correctly set the environment variables for your bot, including the Discord bot token. Double-check the spelling and ensure there are no extra spaces or characters.
Bot not added to server: Ensure that your bot has been correctly added to your Discord server. Double-check the bot token and make sure your bot is invited to your server.
Code issues: Check your code to ensure that there are no syntax errors or other issues causing the bot to fail during startup. You can try printing some debug information to help identify the issue.
API outage: If the Discord API is down or experiencing troubles, then your bot may not be able to connect.
Now, as for the slash commands registration, you can check if your bot is registration by running the /commands command on your Discord server. This will show all the registered slash commands for your bot. If your newly added slash command appears, then it means your bot is correctly registered and using the discord_slash library.