Discord.js如何发出命令以获取语音通道中的每个成员?

时间:2020-09-03 14:30:24

标签: javascript node.js discord discord.js

我正在尝试在discord.js v12中发出命令,以将与行会中的语音通道连接的每个成员移动到另一个语音通道。这是我当前的代码:

// Move All
client.on("message", (message) => {
  if (message.content.startsWith(prefix + "moveall")) {
    if (message.member.hasPermission("MUTE_MEMBERS")) {
      const channels = message.guild.channels.filter((c) => c.type === "voice");
      message.channel.send(
        "**Moved all members in your channel** :white_check_mark:"
      );
      if (message.member.voice.channel) {
        for (const [memberID, member] of channels.members) {
          member.voice.setChannel(message.member.voice.channel);
        }
      } else {
        message.reply("You need to be in a voice channel first!");
      }
    }
  }
});

1 个答案:

答案 0 :(得分:0)

您将必须使用forEach(),这样您才能从每个频道中获得成员,并且每个频道都将被移动

//we are getting every channel from the server, then filter it so our outcome is going to only include voice channels
  message.guild.channels.cache.filter((c) => c.type == "voice").forEach((voicechannel) => {

//now we are getting members from each single channel
  voicechannel.members.forEach((x) => {
    try {
    
      //we set the voice channel for every member
      x.voice.setChannel(message.member.voice.channel);

      //Let's log to check if everything is moving nicely
      console.log(x.id + "Was moved!");

    } catch (err) {

      //let's catch, inform about error and log it
      message.channel.send("Something went wrong")
      return console.log(err);
    }

  });

});

//we return a message that operation went successful
return message.channel.send("Everyone was moved!")