skip to Main Content

I’m having a problem that after restarting the bot, the slash commands doesn’t update, it stays the one I’ve made first, this is my simple code:

import discord
from discord.ext import commands
from discord_slash import cog_ext, SlashContext

class Slash(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @cog_ext.cog_slash(name="Soporte",description="Comando para ver las opciones de soporte")
    async def _support(self, ctx: SlashContext):
        await ctx.channel.send("✈️ Telegram: @Isaac_Sanzn💬 Discord: ElmerKao_#0058 n🌐 Página Web: https://nakiri.x10.mx/")

def setup(bot):
    bot.add_cog(Slash(bot))

Here is a prove that everything is running as it should be:
Terminal

But when I enter discord to run the command it only shows the test one I did before:
enter image description here

Could someone explain what is happening and any solution?

2

Answers


  1. Chosen as BEST ANSWER

    Found the issue, seems that you need to load the cogs before the something like this:

    cogs = ["cogs.roles","cogs.users","cogs.moderation","cogs.ticket-es","cogs.ticket-en"]
    
    bot = ComponentsBot("!", help_command=None,intents=intents)
    
    #BOT STARTUP
    @bot.event
    #When bot starts
    async def on_ready():
        print('Bot {0.user} funcionando perfectamente'.format(bot))
        print("-----------------------------------------")
        print("ID: " + str(bot.user.id))
        print("Version de Discord: " + str(discord.__version__))
        print(f'Actualmente en {len(bot.guilds)} servidores!')
        for server in bot.guilds:
            print(server.name)
        print("-----------------------------------------")
    
    #COGS
    #This loads the cogs like in line 16
    print("Cargando cogs . . .")
    for cog in cogs:
        try:
            bot.load_extension(cog)
            print(cog + " ha sido cargada.")
        except Exception as e:
            print(e)
    print("n")
    

    Here is how it should look like:

    enter image description here

    Here is the link of the question where I found the answer:

    Link


  2. They’re sort of in the middle of adding slash commands to discord.py but you can see a few examples in https://gist.github.com/Rapptz/c4324f17a80c94776832430007ad40e6 You seem to be using discord_slash, which I have not used.

    I’m not quite sure how to do it in cogs, because I have mine in groups but basically what I’m doing is a combination of @bot.tree.command() in the main bot file and a few groups in separate files.

    So here’s my main file

    import discord
    import simplegeneralgroup
    from config import TOKEN
    MY_GUILD = discord.Object(id=1234567890)
    
    class MyBot(discord.ext.commands.Bot):
        async def on_ready(self):
            await self.tree.sync(guild=MY_GUILD)
    
    bot: discord.ext.commands.Bot = MyBot
    
    @bot.tree.command(guild=MY_GUILD)
    async def slash(interaction: discord.Interaction, number: int, string: str):
        await interaction.response.send_message(f'Modify {number=} {string=}', ephemeral=True)
    
    bot.tree.add_command(simplegeneralgroup.Generalgroup(bot), guild=MY_GUILD)
    
    if __name__ == "__main__":
        bot.run(TOKEN)
    

    and then the simplegeneralgroup file

    import discord
    from discord import app_commands as apc
    class Generalgroup(apc.Group):
        """Manage general commands"""
        def __init__(self, bot: discord.ext.commands.Bot):
            super().__init__()
            self.bot = bot
    
        @apc.command()
        async def hello(self, interaction: discord.Interaction):
            await interaction.response.send_message('Hello')
    
        @apc.command()
        async def version(self, interaction: discord.Interaction):
            """tells you what version of the bot software is running."""
            await interaction.response.send_message('This is an untested test version')
    

    There should be three commands: /slash, which will prompt the user for a number and string, /generalgroup hello, and /generalgroup version

    The main documentation for this stuff is https://discordpy.readthedocs.io/en/master/interactions/api.html?highlight=dropdown#decorators but the main "how to" is that you’ve gotta make a "tree", attach commands to that tree, and sync your tree for the commands to show up. discord.ext.Bot makes its own tree, which is why I’m using that instead of client, which I think doesn’t make a tree by default.

    If you specify the guilds, the commands sync takes place instantly, but if you don’t specify the guild, I think it takes an hour to update or something like that, so specify the guild until you’re ready for deployment.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search