问题描述
我目前正在使用discord.js
开发一个discord机器人供私人使用.
I'm currently developing a discord bot using discord.js
for a private usage.
这里有一个小上下文::在我的服务器中,我们通过语音渠道与30至40名成员一起组织活动(所有角色都具有与事件相对应的角色),我们需要检查谁丢失了.因此,基本上,该漫游器需要比较两个列表,其中具有事件角色的成员已连接到语音通道,而另一个成员与具有角色但未连接到指定的语音通道的成员.
Here's a little context: in my server we organize events with 30 - 40 members in a voice channel (all of them have roles corresponding to events), and we need to check who's missing. So basically the bot need to compare 2 list, members with event role who's are connected on the voice channel and another one with those whose have the role but are not connected on the designated voice channel.
我已经做过一些研究,我掌握了它应该如何工作的基础(检索管理员所在的语音通道ID,并从命令中获取角色).但是,它比我想象的要复杂得多,我需要帮助.
I've done some research, I have the bases of how it should work (retrieving the voice channel id where the admin is, and getting the role from the command). However, it's more complicated than I've thought and I require assistance.
这是我的代码":
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);
};
};
});
我将不胜感激:)
推荐答案
您做错了一些事情,从.cache
获得的RoleManager
返回Collection
请参见此处
There are a few things you are doing wrong, the RoleManager
you get from .cache
returns a Collection
See here
集合基本上是一个数组元组的数组,看起来像[key, value]
,键通常是一个ID(字符串,在代码示例中用作数字),该值表示实际值,在这种情况下一个角色,据我了解,您很难让具有该角色的关联成员和没有关联的具有角色的成员遇到麻烦,我已经调整了您的代码,希望它能为您提供一个总体思路
A collection is basically an array of array tuples that look like [key, value]
, the key is usually a ID (string, which you used as a number in your code example) and the value represents the actual value, in this case a role, from what I understand you are having troubles getting the connected members with the role and the members who have the role who are not connected, I have adjusted your code, I hope it gives you a general idea
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)
);
}
});
这篇关于Discord.js-检查列表机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!