Discord.js - Check list bot

早过忘川 提交于 2020-04-17 21:31:00

问题


I'm currently developing a discord bot using discord.js for a private usage.

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.

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.

Here's my "code":

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

        };
    };
});

I will appreciate every help I'll get :)


回答1:


There are a few things you are doing wrong, the RoleManager you get from .cache returns a Collection See here

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


来源:https://stackoverflow.com/questions/60789578/discord-js-check-list-bot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!