简体   繁体   中英

How can i use global slash command in discord.js v13?

I've created a simple slash command. It works on the only guild. before that, I was using v12. Now I wanna switch to v13. Now how can I make these global slash commands?

Code

client.on ("ready", () => {
 
    console.log(`${client.user.tag} Has logged in`)
  client.user.setActivity(`/help | ${client.user.username}`, { type: "PLAYING" })

    const guildId = "914573324485541928"
    const guild = client.guilds.cache.get(guildId)
    let commands

    if (guild) {
      commands = guild.commands
    } else {
      commands = client.application.commands
    }

    commands.create({
      name: 'ping',
      description: 'Replies with pong'
    })
    commands.create({
      name: 'truth',
      description: 'Replies with truth'
    })

  }); 
  client.on('interactionCreate', async (interaction) => {
    if(!interaction.isCommand()){
      return
    }

    const { commandName, options } = interaction 

    if (commandName === 'ping'){
      interaction.reply({
        content: 'pong'
      })
    }
    ```

I'm new in v13 so please explain it simply :|  

Simply change the guild declaration. Your code checks if guild is truthy, and uses the guild if it is. Otherwise, it will use global commands

const guild = null 
// any falsy value is fine (undefined, false, 0, '', etc.)

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