#Slash Commands & Cogs

1 messages · Page 1 of 1 (latest)

wet summit
#

I've used snippet codes from the nextcord tutorials section just to get the basic cog working

When I try to do the slash command it doesn't work and the bot reports 0 errors and Cog loaded successfully!

class Test(Cog):
    def __init__(self, bot: Bot) -> None:
        self.bot = bot

    slash_command(name="ping", description="A simple ping command.", guild_ids=[...])
    async def test(self, inter: Interaction) -> None:
        await inter.send(f"Pong! {self.bot.latency * 1000:.2f}ms")

def setup(bot: Bot) -> None:
    bot.add_cog(Test(bot))
from dotenv import load_dotenv
import os
from nextcord.ext.commands import Bot

load_dotenv()

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

bot = Bot()

# Event: on_ready
@bot.event
async def on_ready():
    print(f'Logged in as {bot.user}')
    try:
        bot.load_extension('cogs.Test')
        print("Cog loaded successfully!")
    except Exception as e:

        print(f"Error loading cog: {e}")

bot.run(os.getenv('TOKEN'))
harsh sequoia
#

=docs cog.listener

orchid hawkBOT
mortal vale
#

You need to load the cogs before the bot starts to have Nextcord sync slash commands for you.

#

on_ready is ran well after the bot starts up.

wet summit
#

Even loading the cogs before on_ready still doesn't work

mortal vale
#

show the code for that?

#

oh, you forgot the @

#

it's supposed to be @slash_command(...), not slash_command(...)

#

also, is guild_ids actually set to [...], or are you just omitting your guild ID?

wet summit
#

Thanks for pointing that out, corrected that but still having the same issue

mortal vale
#

or you just don't provide guild_ids at all, and that would make it a global command

wet summit
#

Ah yes, actually adding that guild_id works, Just to clarify the guild is actually the server right? If so why would it need a guild_id if the bot is already added to the server?

mortal vale
#

Slash commands can be added in 2 different ways:

  1. Globally, any server the bot is in (and in bot DM's) can use the command, or
  2. Per-guild, where the command is added specifically to that guild and isn't accessible from anywhere else.
#

If guild_ids is omitted or force_global is set to True, then the command will be added to Discord as a global command.
If guild_ids is set, the bot will attempt to add the command to all the guilds provided.

mortal vale
#

if your bot is just for a single guild, then it doesn't really matter if the commands are set globally or for that specific guild unless you don't want people using the commands in bot DMs

wet summit
#

I understand now, I was just confused to why it needed a guild_id If it was already added to said guild.. I think my understanding Is so it can't be abused anywhere but the guild it's In?

mortal vale
#

essentially, yes

#

or if you have the bot in a variety of guilds, but want to add a command to only one of those guilds

wet summit
#

Right, yep I understand Thank you for explaining that I do however have another question with Cogs, If I was to add reload_extension I'm assuming I can just have this as a normal slash command rather than a cog itself?

mortal vale
#

I'm slightly confused by your wording, are you asking if you can add a reload command to your bot without using a cog/extension via @bot.slash_command()?

wet summit
#

Yes

mortal vale
#

yeah you can. You'll need to tell the bot to resync the application commands afterwards though

#

otherwise it wont add/update/remove application commands from Discord if needed.