Reputation: 13
I was wondering how I could import a slash command from a different file with discord.py. I have the following slash commands that simply greets the user:
@tree.command(name = "hello", description = "Says hello to the user.")
async def self(interaction: discord.Interaction):
await interaction.response.send_message(f"Hello <@{interaction.user.id}>!")
And I have the following 2 files: main.py and slashcommands.py
Right now all my slash commands are in my main.py, but I would like to spread out these slash commands over different files to make my code cleaner. I have done this previously when I was still using Extensions/Cogs: https://discordpy.readthedocs.io/en/stable/ext/commands/extensions.html and https://discordpy.readthedocs.io/en/stable/ext/commands/cogs.html#ext-commands-cogs, but since I made the switch to slash commands I could not find any documentation about how to do this for slash commands. My question was if someone knows how I can make the command I posted above work in the slashcommands.py file, instead of having all my commands in my main.py file.
Upvotes: 0
Views: 2020
Reputation: 46
So, to import a slash command from another file, you must simply first define that slash command (using @commands.slash_command) in the other file (which isn't main.py) then in the on_ready
section in main.py, load the extension, THEN (or it won't work) sync the command tree. If you sync it before loading extension, it won't work.
Same applies for a reload extension: sync the tree after unloading / reloading a extension.
What is extension? It's a file containing bot commands that can be easily modified without needing to restart the whole main code. This make easier to modify commands without needing to reload the whole code, and using a reload command can reload all changes directly into the bot.
(Just to say, this post made me realize the error in my own code because I have this too.)
In this case, it would be like:
(Extension Name).py
import discord
from discord.ext import commands
@commands.hybrid_command()
async def hello(ctx):
await ctx.send(f"Hello {ctx.author.display_name}!")
async def setup(bot)
bot.add_command(hello)
main.py
#all imports here and all before on_ready event
async def on_ready():
await bot.load_extension(hello) #this is only an example
#if file is in another folder, do this:
#(example.hello), hello being your extension name, and example the folder name.
await bot.sync() #syncs command tree
#anything after it
If you wanna do a reload command, here's the code:
@commands.hybrid_command()
async def reload(ctx):
await bot.reload_extension(hello) #same as before
await bot.sync()
It isn't necessary to put into the main file, if you don't, then you must make one in main file under a different name that only reloads the reload command. However, it's recommended to put it in the main file. This one is the simplest and fastest command, compared to the other samples
Another working sample:
@commands.hybrid_command()
async def reload(ctx):
await bot.unload_extension(hello) #same as before
await bot.load_extension(hello) #same as before
await bot.sync()
This one is slower and takes more space.
Docs: https://discordpy.readthedocs.io/en/stable/ext/commands/extensions.html
I recommand checking this for a separated load and unload command: discord.py problems with loading extensions <- This is more detailed and much better than mine's which loads at start and only reload while this more detailed makes a load and unload command making it much better for implementing new extensions. I recommend you to take a tour to this page.
Upvotes: 1