【问题标题】:I'm looking to mute someone in discord.js v13 and i have problem in creating VoiceState object我希望在 discord.js v13 中使某人静音,但在创建 VoiceState 对象时遇到问题
【发布时间】:2021-11-06 22:13:37
【问题描述】:

这是我的第一个不和谐机器人,我正试图让某人静音。根据我的理解,我认为我需要调用 VoiceState[.setMute()][1] 函数,并且我正在尝试创建一个 voiceState 对象,但我不明白数据的含义。

这是我的命令执行功能。 [ /mute ]

    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 代码:

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)'));

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    无需创建新的VoiceState 对象,一旦GuildMember 加入语音通道API 会自动提供该对象,我们只需要使用GuildMemberEditData 编辑GuildMember 的数据,通过传递属性mute() 给它,所以要使用GuildMemberEditData#mute() 方法,我们可以这样:

      const targetUser = interaction.options.getMember('target');
      targetUser.edit({mute : true})
    

    请注意,您只能在 GuildMember 对象上使用它,而不能在您获得 targetUserUser 对象上使用它,我强烈建议您对其进行编辑。

    升力

    您可以继续获取GuildMembervoiceState 并通过GuildMember#voice 方法在其上使用setMute(),然后进一步对其使用VoiceState#setMute() 方法,在这种情况下,我们的代码将是一些东西像这样:

     const targetUser = interaction.options.getMember('target');
     targetUser.voice.setMute(true);
    

    【讨论】:

    • 感谢您致电 getMember 工作!我还了解到 .edit() 应该这样调用:targetUser.edit({mute: true})
    • 没问题!很高兴我能帮上忙!如果这回答了您的问题,请确保单击 ✓ 按钮,以便将来可能访问此的其他人知道这是正确的解决方案并使用 ? 按钮为我和您自己赢得一些声誉:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    相关资源
    最近更新 更多