#Can't get slash command to show on Discord

1 messages · Page 1 of 1 (latest)

glad ferry
#

It happens on the FIRST command I create, it's just a "ping" command that shows the latency of the bot.
That command is not shown on Discord but on VS Code, there's no errors raised.

main :

import disnake, os
from disnake.ext import commands
from config import token

import sys; sys.dont_write_bytecode = True

class Bot(commands.AutoShardedInteractionBot):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
    
    async def on_connect(self) -> None:
        print(f"Logged in as {self.user}")

    async def on_ready(self) -> None:
        await self.change_presence(activity=disnake.Activity(name="Disnake", type=disnake.ActivityType.watching), status=disnake.Status.do_not_disturb)

if __name__ == '__main__':
    bot = Bot(intents = disnake.Intents.default(), test_guilds = [])

    for file in os.listdir('./cogs/'):
        if not file.endswith('.py'):
            continue

        bot.load_extension(f"cogs.{file[:-3]}")
        print(f"cogs.{file[:-3]} has been loaded")
    
    bot.run(token)

ping command :

import disnake
from disnake.ext import commands

class Ping(commands.Cog):
    def __init__(self, bot: commands.AutoShardedInteractionBot) -> None:
        self.bot = bot

    @commands.Cog.listener()
    async def on_ready(self) -> None:
        print("Sent from ping.py")

    @commands.slash_command(name="ping", description="tells bot's latency")
    async def ping(self, inter: disnake.CommandInteraction) -> None:
        await inter.send(f"You just got pong'd !\nMy latency is **{int(self.bot.latency * 1000)}ms**.")

def setup(self: commands.AutoShardedBot) -> None:
    self.add_cog(Ping(self))
hallow agate
#

enable this in ur bot then start and send logs sync_commands_debug=True

#

and are u sure the cog is being loaded?

hallow agate
#

yeah enable cmd sync debug

glad ferry
#

Never heard of this

hallow agate
#

Bot(intents = disnake.Intents.default(), sync_commands_debug=True, test_guilds = [])

glad ferry
#

Uhh
Also it says that sync_commands_debug is deprecated

hallow agate
#

sooo the cmd isn't being registerd at all.

#

oh

#

thats why

#
def setup(self: commands.AutoShardedBot) -> None:
    self.add_cog(Ping(self))
#

should be bot

#
def setup(bot: commands.AutoShardedBot):
    bot.add_cog(Ping(bot))
#

i think? but ur logs show the cog was loaded anyways lmao.

#

try that and see what happens with the debug enabled

glad ferry
#

Nop, didn't move a atom
Btw the two first lines were not here on the first test with debug enabled

hallow agate
#

hmmm

#

try to remove test_guilds from bot

glad ferry
hallow agate
#

so good now?

glad ferry
#

Yes

hallow agate
#

i guess passing test_guilds stops global registration even if you don't pass any IDs.

glad ferry
#

Probably

warm elmBOT
#
Solved!

Marked the thread as solved. If your question has not been answered, please open a new thread in #1019642966526140566.

vestal gulch
#
async def on_ready(self) -> None:
        await self.change_presence(activity=disnake.Activity(name="Disnake", type=disnake.ActivityType.watching), status=disnake.Status.do_not_disturb)

also, don't do this.

#

Set the activity in the bot construction

glad ferry
#

Okay

#

So the consctructor should be async, right ?

vestal gulch
#

No..

glad ferry
#

Only the activity, not the entire function, right ?

vestal gulch
#

Yes.

#

activity=disnake.Activity(...))

#
bot = Bot(
  intents=disnake.Intents.default(), 
  activity=disnake.Activity(name="Disnake", type=disnake.ActivityType.watching), status=disnake.Status.do_not_disturb)
)
glad ferry
#

Done

#

But I don't understand that change

vestal gulch
#

on_ready isn't guaranteed to be called last AND can be called multiple times. Generally you should avoid making API calls or accessing cache from that function since it could be called during a point when the bot isn't actually ready.
Since you're just setting a static presence, you can do it when instantiating the bot.

if you were wanting to have a rotating presence of some sort, it should be done in a task. (which should also include a before_loop that contains bot.wait_until_ready() so that the task doesn't start until the bot is actually ready (connected and cache populated)

glad ferry
#

Okay

vestal gulch
#

Basically.. DON'T DO STUFF IN ON_READY

glad ferry
#

So I moved also the status to the constructor and let a print to tell me if the bot is ready
Thanks for the help guys and you, DLCHAMP for the explanations TCCdonkeyKongYes

vestal gulch
#

Being that this look like you are just creating a new bot, probably don't need to useAutoShardedIteractionBot at the moment.
Also, if you only plan to have interaction commands and listeners, use InteractionBot and you can move to AutoSharded* when needed.
I don't think it causes issues (?) but it's unnecessary until you hit something like 1000 +/- servers