shardlab / discordrb

Discord API for Ruby
MIT License
538 stars 99 forks source link

bot.delete_application_command fails with Invalid Form Body #257

Closed joshbuker closed 6 months ago

joshbuker commented 6 months ago

Summary

When trying to delete an application command (both global and server specific), I get the following error:

Invalid Form Body
        - command_id: Value "exit" is not snowflake.
/workspaces/discord-bot/vendor/bundle/ruby/3.3.0/gems/discordrb-3.5.0/lib/discordrb/api.rb:124:in `rescue in request': Invalid Form Body (Discordrb::Errors::InvalidFormBody)
        from /workspaces/discord-bot/vendor/bundle/ruby/3.3.0/gems/discordrb-3.5.0/lib/discordrb/api.rb:113:in `request'
        from /workspaces/discord-bot/vendor/bundle/ruby/3.3.0/gems/discordrb-3.5.0/lib/discordrb/api/application.rb:140:in `delete_guild_command'
        from /workspaces/discord-bot/vendor/bundle/ruby/3.3.0/gems/discordrb-3.5.0/lib/discordrb/bot.rb:881:in `delete_application_command'

Replicate with:

bot = Discordrb::Bot.new(
  token: '<your_token_here>',
  intents: :all
)
server_id = '<your_server_id_here>'
bot.register_application_command(:exit, 'Shutdown the bot', server_id: server_id)
bot.run(true)
bot.delete_application_command(:exit, server_id: server_id)

Environment

Ruby version: 3.3.0

Discordrb version: 3.5.0

MCausc78 commented 6 months ago

The discordrb expects a command ID, not name.

Discord docs:

Discordrb

joshbuker commented 6 months ago

@MCausc78 Is there a way to fetch command IDs for all commands currently registered?

Edit: Found it - bot.get_application_commands(server_id: server_id)

joshbuker commented 6 months ago

So we don't end up with an xkcd 979, the solution I found was:

server_id = 'your server id here'
commands = bot.get_application_commands(server_id: server_id)
commands.each do |command|
  bot.delete_application_command(command.id, server_id: server_id)
end