node.js - Discord.js - 检查列表机器人
问题描述
我目前正在开发一个discord.js
用于私人用途的不和谐机器人。
这是一个小背景:在我的服务器中,我们在一个语音频道中组织了 30 到 40 名成员的活动(他们都有与活动相对应的角色),我们需要检查谁失踪了。所以基本上机器人需要比较2个列表,具有事件角色的成员在语音通道上连接,另一个与具有角色但未在指定语音通道上连接的成员。
我已经做了一些研究,我有它应该如何工作的基础(检索管理员所在的语音通道 ID,并从命令中获取角色)。但是,它比我想象的要复杂,我需要帮助。
这是我的“代码”:
client.on('message', message => {
if(message.content == "check"){
//role restriction
if(!!message.member.roles.cache.has("admin")) return console.log("Fail from "+message.author.username);
else{
//retreiving the role from command
var messageArray = message.content.split(" ");
var command = messageArray[0];
var args = messageArray.slice(1)
//finding the correct channel with the gaved ID
console.log(message.guild.channels.cache
.find(VoiceChannel => VoiceChannel.id == 618882800950706189))
//voice channel ID where admin is connected
//console.log(message.member.voice.channelID);
};
};
});
我会感谢我得到的每一个帮助:)
解决方案
你做错了几件事,RoleManager
你从.cache
返回Collection
看到这里
一个集合基本上是一个数组元组的数组,看起来[key, value]
像了解您在获取具有角色的已连接成员和具有未连接角色的成员时遇到问题,我已经调整了您的代码,希望它能给您一个大致的想法
client.on("message", message => {
// This prevents the bot from responding to bots
if (message.autor.bot) {
return;
}
// ES6 array destructuring + rest spread operator allows us to easily
// get the first word from an array and keep the rest as an array
const [command, ...args] = message.content.split(/ +/);
if (command === "check") {
// Check if the member has the admin role using Collection#some
if (!message.member.roles.cache.some(r => r.name === "admin")) {
return console.log(`Fail from ${message.author.username}`);
}
// Attempt to get the channel from the collection using the ID from user input
// Note that this could be undefined!
const voiceChannel = message.guild.channels.cache.get(args[0]);
// Collection of members who have the event role
const eventMembers = message.guild.members.cache.filter(m =>
m.roles.cache.some(r => r.name === "event")
);
// Collection of members with the event role who are connected
// Using Collection#filter and Collection#has
const connectedMembers = eventMembers.members.filter(m =>
voiceChannel.members.has(m.id)
);
// Collection of members with the event role but are not connected
const disconnectedMembers = eventMembers.members.filter(
m => !voiceChannel.members.has(m.id)
);
}
});