Search code examples
pythondiscordinteractionslashnextcord

How to do slash_commands in cogs


I'm trying to implement slash_commands in my python for discord, didn't had any trouble for usual prefix command but this seems tricky and I don't know what to do? This is my code:

my cog:

import nextcord
from nextcord import Interaction
from nextcord.ext import commands


class Ping(commands.Cog):

    def __init__(self, client):
        self.client = client

    @nextcord.slash_command(name="test", description="test", guild_ids=[GUILD_ID]) 
    async def test(self, interaction: Interaction):                                              
        await interaction.response.send_message("hello")

def setup(client):
    client.add_cog(Ping(client))

my main:

import nextcord


def main():

    intents = nextcord.Intents.default()
    intents.members = True
    intents.message_content = True

    client = commands.Bot(command_prefix=PREFIX, intents=intents)

    # ---------------------------------------------------------------------------
 
    @client.event
    async def on_ready():

        for folder in os.listdir("data"):
            if os.path.exists(os.path.join("data", folder, "cog.py")):
                client.load_extension(f"data.{folder}.cog")


    client.run(BOT_TOKEN)


if __name__ == "__main__":
    main()

I give discord bot Privileged Gateway Intents on developer portal, invited him as bot- administrator with applications-commands previlege. Still nothing :(

In code intents.members = True intents.message_content = True

It says that : Intents' object attribute 'members' is read-only, but I really don't know what to do next. I hope u will resolve my problem


Solution

  • I'd recommend joining the Discord server for quick help on Nextcord.

    Here is some details from the server:

    The issue you are facing seems relevant to #3.

    Most common issues

    1. You didn't invite the bot to your guild with the application.commands oauth enabled. Go to the developer portal, make an invite with both bot and applications.commands enabled, and re-authorize your bot for your server.

    2. You're using global commands and probably haven't waited (up to) an hour for them to appear.

    3. You're adding the cogs with slash commands in them too late inside of your bot. You either need to add them before on_connect is called (preferably before the bot is even started), or run the global and/or guild sync methods manually.

    Less common issues

    1. Discord gets weird sometimes and doesn't refresh available commands. Try restarting your Discord client.

    2. You're overriding on_connect, which adds the application commands to bot/client (including ones inside cogs) and rolls out global commands. Either stop overriding it, or add bot.add_all_application_commands() and await bot.sync_application_commands() to it.

    3. You're overriding on_guild_available, which rolls out commands to guilds. Either stop overriding it, or add the following codeblock to it:

    try:
        await bot.sync_application_commands(guild_id=guild.id)
    except Forbidden:
        pass