[英]Discord.js How to make a command to get every member in my voice channel?
I'm trying to make a command in discord.js v12
to move every member connected to a voice channel in my guild into a different voice channel.我正在尝试在
discord.js v12
发出命令,将连接到我公会中的语音频道的每个成员移动到不同的语音频道。 This is my current code:这是我当前的代码:
// 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!");
}
}
}
});
You would have to use forEach()
, so you would get members from every single channel and every single one would be moved您将不得不使用
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!")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.