Slash commands not appearing when loading them in cogs discord.py
Question:
When using cogs to organize slash commands, I can never get them to load.
main.py
import discord
from discord.ext import commands
from testing import MyCog
bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())
@bot.command()
async def sync(ctx):
await bot.add_cog(MyCog(bot))
await bot.tree.sync()
await ctx.send('Synced')
bot.run("token")
testing.py
import discord
from discord.ext import commands
from discord import app_commands
from discord import Interaction
intents = discord.Intents.default()
client = discord.Client(intents=intents)
class MyCog(commands.Cog):
def __init__(self, client):
self.client = client
@app_commands.command(name = "test", description = "testing")
async def test(self, interaction: discord.Interaction):
await interaction.response.send_message('Hello!')
I don’t ever get errors, but when I run the !sync command, Synced
is sent but the slash commands never appear. I’ve used multiple resources, but can never figure out the correct answer. Am I putting the .sync() in the incorrect spot?
Answers:
By syncing globally, you’d expect them to show up in about an hour. If you want them to show up immediately for debugging or testing purposes, you should consider copying them to a guild explicitly using the copy_global_to method of the command tree. Example from discord.py
repo.
bot = commands.Bot(...)
# This is for the example purposes only and should only be used for debugging
@bot.command()
async def sync(ctx: commands.Context):
# sync to the guild where the command was used
bot.tree.copy_global_to(guild=ctx.guild)
await bot.tree.sync(guild=ctx.guild)
await ctx.send(content="Success")
@bot.command()
async def sync_global(ctx: commands.Context):
# sync globally
await bot.tree.sync()
await ctx.send(content="Success")
If that did not solve your problems, see My bot’s commands are not showing up! FAQ.
By the way, you should make another command for adding cogs(or better, turn them into extensions) and should not define another discord.Client
or discord.ext.commands.Bot
instance if you already have one.
When using cogs to organize slash commands, I can never get them to load.
main.py
import discord
from discord.ext import commands
from testing import MyCog
bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())
@bot.command()
async def sync(ctx):
await bot.add_cog(MyCog(bot))
await bot.tree.sync()
await ctx.send('Synced')
bot.run("token")
testing.py
import discord
from discord.ext import commands
from discord import app_commands
from discord import Interaction
intents = discord.Intents.default()
client = discord.Client(intents=intents)
class MyCog(commands.Cog):
def __init__(self, client):
self.client = client
@app_commands.command(name = "test", description = "testing")
async def test(self, interaction: discord.Interaction):
await interaction.response.send_message('Hello!')
I don’t ever get errors, but when I run the !sync command, Synced
is sent but the slash commands never appear. I’ve used multiple resources, but can never figure out the correct answer. Am I putting the .sync() in the incorrect spot?
By syncing globally, you’d expect them to show up in about an hour. If you want them to show up immediately for debugging or testing purposes, you should consider copying them to a guild explicitly using the copy_global_to method of the command tree. Example from discord.py
repo.
bot = commands.Bot(...)
# This is for the example purposes only and should only be used for debugging
@bot.command()
async def sync(ctx: commands.Context):
# sync to the guild where the command was used
bot.tree.copy_global_to(guild=ctx.guild)
await bot.tree.sync(guild=ctx.guild)
await ctx.send(content="Success")
@bot.command()
async def sync_global(ctx: commands.Context):
# sync globally
await bot.tree.sync()
await ctx.send(content="Success")
If that did not solve your problems, see My bot’s commands are not showing up! FAQ.
By the way, you should make another command for adding cogs(or better, turn them into extensions) and should not define another discord.Client
or discord.ext.commands.Bot
instance if you already have one.