Can't create guild command
Summary
I'm trying to create a guild command but it doesn't work. It worked before when I had all commands only being guild commands.
Reproduction Steps
Create a bot with commands and only one of them being a guild commands.
Minimal Reproducible Code
@bot.command(guild_ids=[my_guild_id])
async def handler(ctx):
...
#Create other global commands here
Expected Results
Create all commands
Actual Results
Ignoring exception in on_connect Traceback (most recent call last): File "/usr/local/lib/python3.9/site-packages/discord/client.py", line 351, in _run_event await coro(*args, **kwargs) File "/usr/local/lib/python3.9/site-packages/discord/bot.py", line 359, in on_connect await self.register_commands() File "/usr/local/lib/python3.9/site-packages/discord/bot.py", line 157, in register_commands to_update = update_guild_commands[guild_id] KeyError: my_guild_id
Intents
none
System Information
python3.9 and pycord slash branch
Checklist
- [X] I have searched the open issues for duplicates.
- [X] I have shown the entire traceback, if possible.
- [X] I have removed my token from display, if visible.
Additional Context
Tested with multiple guilds and bots and only seems to occur when only 1 command is a guild-only-command.
Cannot reproduce. Can you confirm that this still occurs?
Can still reproduce. Maybe a caching issue? Just tried again, I can't reproduce it with my 3-guilds bot, but with a 800-guilds bot.
It runs fetch_guilds and then adds all the guild ids to the dict containing an empty list for each
async for guild in self.fetch_guilds(limit=None):
update_guild_commands[guild.id] = []
After this, it processes the guild_ids passed into all of the commands
for command in [cmd for cmd in self.pending_application_commands if cmd.guild_ids is not None]:
as_dict = command.to_dict()
for guild_id in command.guild_ids:
to_update = update_guild_commands[guild_id]
It appears this error could occur if a guild_id passed into one of the commands doesn't actually belong to a guild that the bot is in, could you check if that is true? A simple fix to this would be changing
to_update = update_guild_commands[guild_id]
to
to_update = update_guild_commands.get(guild_id, [])
but that would just be avoiding a bigger issue so I don't think it should be used.
I've tried it again in logging debug mode double checking the guild id, the guild was sent in the guild_create events on startup.
Hello, I am having this issue as well as I am trying to test slash commands in my bot which is in 6.1k servers:
# testing slash commands
@client.slash_command(guild_ids=[667412571728379940])
async def new(
ctx,
name: Option(str, "Enter your name"),
gender: Option(str, "Choose your gender", choices=["Male", "Female", "Other"]),
age: Option(int, "Enter your age", required=False, default=18),
):
await ctx.send(f'Hello {name}, it looks like you are {age} years old and {gender}.')
return
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/discord/client.py", line 351, in _run_event
await coro(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/discord/bot.py", line 370, in on_connect
await self.register_commands()
File "/usr/local/lib/python3.8/dist-packages/discord/bot.py", line 191, in register_commands
to_update = update_guild_commands[guild_id]
KeyError: 667412571728379940
@BobDotCom should take a look at this.
I believe this may be an issue with the optimization of guild commands at the moment for larger bots in our library. At the time being I would recommend not using guild commands for large bots until this issue is fixed.
It's also possible it could be happening only on sharded bots, but i haven't explored this possibility yet. Could @lukenamop and @Miolus confirm that they were using sharded bots?
Can reproduce it sharded and non sharded.
I have only tested and had the problem on a sharded bot.
Try adding
@bot.event
async def on_ready():
print(bot.guilds)
await bot.register_commands()
See if the server you're looking for is there. (You can use a different way to check, but do it in on_ready and before register_commands). It might be an issue with guilds not being cached.
I’ll make it check if the ID is in a list of bot.guilds IDs. I’ll let you know what I get.
@BobDotCom first I added the check (your print(bot.guilds), but I just made it iterate through the list of guilds and print a confirmation if my guild was found. on_ready() waited until all 8 shards were loaded and then ran, confirming that my target guild was found. Then I added the await bot.register_commands() after my check and un-commented the slash command and tried again. I received the error as soon as I started to execute the file, which to me means the bot tried to register the command before I explicitly called await bot.register_commands() which means it happened before on_ready() which is why the guild is not found.
TL;DR: @client.slash_command(guild_ids=[]) seems to be attempting to register commands as soon as the code is executed, rather than adding a flag to be registered after the bot is ready. As such, the guild doesn't exist in the bot's cache, and it errors out and breaks.