【问题标题】:Discord.js - Check list botDiscord.js - 检查列表机器人
【发布时间】:2020-07-02 12:00:56
【问题描述】:

我目前正在开发一个不和谐的机器人,使用 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);

        };
    };
});

我会感谢我得到的每一个帮助:)

【问题讨论】:

    标签: node.js bots discord.js


    【解决方案1】:

    你做错了几件事,你从.cache得到的RoleManager返回一个CollectionSee here

    集合基本上是一个数组元组数组,看起来像[key, value],键通常是一个 ID(字符串,您在代码示例中用作数字),值代表实际值,在这种情况下一个角色,据我了解,您在获取具有该角色的关联成员和具有该角色的未关联成员时遇到了麻烦,我已经调整了您的代码,希望它能给您一个大致的想法

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

    【讨论】:

    • 您好,感谢您的调整。但它似乎包含一些错误,当我连接到语音通道时,voiceChannel 结果为未定义,并且在修复它之后,最后一部分什么也没给我。我认为 .cache 在集合之后是必需的(就像在您提供的链接中一样),但没有任何改变。 eventMembers 给我这个错误(message.guild.members.filter 不是函数),然后无法检查最后两个集合,因为它们依赖于它。
    • 改用message.guild.members.cache
    • 另一个错误,但使用message.guild.members.cache.filter 代替它可以工作,但在m.role.some 出现另一个错误(不是函数),如果我在角色和一些之间放置 .cache 结果似乎是空:Map(2) {_array: null, _keyArray: null} no debug adapter.
    • 我认为那里不需要缓存,但它是角色而不是角色
    • 我的评论有误,但代码中没有,抱歉。如果没有 .cache,调试器会告诉我 m.roles.some 不是函数。
    猜你喜欢
    • 2021-07-08
    • 2021-09-09
    • 2021-10-14
    • 2021-01-08
    • 2020-12-21
    • 2020-09-02
    • 2021-08-18
    • 2021-04-24
    • 1970-01-01
    相关资源
    最近更新 更多