This is my first discord bot and I'm trying to mute someone. Based on my understanding I think I need to invoke the [.setMute()][1] function of the VoiceState and I'm trying to create a voiceState object where I dont understand what the data means.
Heres my execute function for the command. [ /mute <@USER> <SECONDS(optional)> ]
async execute(interaction) {
const targetUser = interaction.options.getUser('target');
if (interaction.options.getNumber('seconds') !== null) {
const seconds = interaction.options.getNumber('seconds');
} else {
const seconds = 60;
}
console.log(targetUser);
const data = {} // APIVoiceState but idk what to put here
const voiceState = new VoiceState(interaction.guild, data );
console.log(voiceState);
voiceState.selfMute(true);
await interaction.reply('supposed to mute!');
Command Builder code if anyone needs it:
const data = new SlashCommandBuilder()
.setName('mute')
.setDescription('mute @user <seconds> server mutes the specified user for specified number of seconds')
.addUserOption(option =>
option.setName('target')
.setDescription('user to mute')
.setRequired(true))
.addNumberOption(option =>
option.setName('seconds')
.setDescription('Time in seconds the user stays muted (default: 60)'));
No need to create a new VoiceState object, once a GuildMember joins a voice channel the API automatically offers the object, we just need to edit the GuildMember's data using the GuildMemberEditData by passing the property mute() to it, so to utilize GuildMemberEditData#mute() method we can just go like:
const targetUser = interaction.options.getMember('target');
targetUser.edit({mute : true})
Please note that you can only use it on a GuildMember object and not on an User object which you are getting targetUser as, I strongly recommend to edit that.
You can go ahead and get the GuildMember's voiceState and use setMute() on that via the GuildMember#voice method then further use the VoiceState#setMute() method on it, in that case our code would be something like this:
const targetUser = interaction.options.getMember('target');
targetUser.voice.setMute(true);