简体   繁体   中英

Discord.js v13: Slash commands are duplicated

That seems like some bug or I don't know, but I'm sure this is fixable. All my slash commands are duplicated, first is the newest version of the command, second is some outdated one.

预习

I'm assuming it is related to slash command registering, so here it is:

const guild = await client.guilds.cache
            .get("836212492769165363")

        guild.commands.set(arrayOfSlashCommands).then((cmd) => {
          const getRoles = (commandName) => {
            const permissions = arrayOfSlashCommands.find(x => x.name === commandName).userPermissions;

            if(!permissions) return null;
            return guild.roles.cache.filter(x => x.permissions.has(permissions) && !x.managed)
          }

          const fullPermissions = cmd.reduce((accumulator, x) => {
            const roles = getRoles(x.name);
            if(!roles) return accumulator;

            const permissions = roles.reduce((a, v) => {
              return [
                ...a,
                {
                  id: v.id, 
                  type: 'ROLE',
                  permission: true,
                },
              ]
            }, [])
            return [
              ...accumulator,
              {
                id: x.id,
                permissions,
              }
            ]
          }, [])
          guild.commands.permissions.set({ fullPermissions }).catch((e) => console.log(e))
        })

Global and guild commands are not the same.

Explanation


Global and guild commands are 2 different kinds of slash commands stored in different places.
They both have the limit of 100 and have their own ratelimits.

So what's the difference?


  • Global commands are visible to all guilds and users (including DMs).
  • Guild commands on the other hand, is server-specific.

Demonstration


Imagine you have 2 servers, one called A and one called B .

Now, you registered 2 guild commands inside A . Those 2 commands would not appear on B .

But if you register another 2 global commands, this would appear on both servers and including DMs.

What happened to my code then?


You registered both global and guild commands and they both have the same configuration.
That's the reason they all appeared duplicated.

A fix?


There's a way to reset all your guild and global commands.
By using the following code below, it will reset guild commands immediately.
However, you'll have to wait ~1 hour for global commands to update.

    const guild = client.guilds.cache.get("<guild id>");

    // This takes ~1 hour to update
    client.application.commands.set([]);
    // This updates immediately
    guild.commands.set([]);

Try restarting your bot using this code:

client.application.commands.set([])

Or if you have the guild you can do this:

guild.commands.set([])

This might take some time to finish but it will work. It will clear all the slash commands so you can put them back without duplicating. From what I see, you have both Guild commands and application commands

Simple fix: Kick your bot then re-invite it, worked for me:D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM