Search code examples
discorddiscord.js

Discord using slash commands to do "setMute()"


My code is this.

 client.on('interactionCreate', async (interaction) => {
  if(!interaction.isCommand()) {
    return
  }
  const {commandName, options} = interaction
  if(commandName == 'mute') {
    const name = interaction.options.getUser('target')
    //await interaction.guild.members.voice.setMute(target);
    //name.voice.setMute(true)
    interaction.reply({
      content: 'muted',
      ephemeral: true,
    })
  }
})

I am inspired by the guide. It took me 4 hours to try fixing it, but it still went wrong. Can someone help ?

enter image description here


Solution

  • The getUser method returns a User object but the setMute function is a method of GuildMember The Guild.ban method can take a User object as parameter

    const User = interaction.options.getUser('target')
    interaction.guild.members.fetch(User)
        .then(Member => {
            Member.voice.mute()
               .then(() => {
                    interaction.reply() // Success: Your message here
                })
               .catch(err => {
                    interaction.reply() // Failed: Your message here
               })
        })
        .catch(err => {
            interaction.reply() // Member not found: Your message here
        })