按用户ID获取用户

时间:2018-06-05 07:16:34

标签: javascript discord discord.js

当我使用命令发送消息时,机器人必须为具有特殊ID的用户提供不同的角色。如果我只有用户的ID,我该怎么做?

命令message.guild.members.get(ID)返回undefined

这是我使用的代码。 const account = message.guild.members.get(key);无效...

module.exports.run = (Discord, bot, firebase, message, args, data) => {

  const axios = require('axios');
  // Call a Axios Request
  axios.get('https://api.guildwars2.com/v2/guild/' + data.GuildID + '/members?access_token=' + data.APIToken).then(response => {
    const data = response.data; // Put all response inside a variable
    const database = firebase.database().ref('discord/accounts/'); // Connect to firebase
    var int = 0; // Int for check if all uers are checked
    var insideguild = 0; // Int for how many are inside the guild
    var notinsideguild = 0; // Int for how many are remove from discord
    database.orderByChild("guild").equalTo(true).once('value', function(snapshot) { // Get all accounts from Firebase
      snapshot.forEach(function(snap) { // for each account do something
        const key = snap.key; // Get the name of the snapshot
        const value = snap.val(); // Get value of all accounts

        let guildwarsaccount = value.guildwarsaccount; // Get user Guild Wars Account
        let match = false; // True if user account is inside the Guild
        for (var i = 0; i <= data.length; i++) { // do soming for each users inside the Guild
          if (i == data.length && match == false) { // Do something when user is not inside the guild

            // Change database
            database.child(key).update({ // Set player status on guild false
              guild: false
            });

            // Reset nickname and rank
            const role_pixel_brother = message.guild.roles.find("name", "Pixel Brother"); // Check the Role ID for Pixel Brother
            const role_guest = message.guild.roles.find("name", "Guest"); // Check the Role ID for Guest
            const account = message.guild.members.get(key);
            account.setNickname(account.user.username);
            account.addRole(role_guest).catch(); // Give the Role Guest
            account.removeRole(role_pixel_brother).catch(); // Remove the Pixel Brother

            notinsideguild++; // +1 If it is remove from guild

          } else if (i < data.length) { // Loop each variable
            if (data[i].name == guildwarsaccount) { // Do something when user is inside the guild
              match = true;
              insideguild++; // +1 If it is the guild
            }
          }
        }
        int++; // +1 If the user is checked
        if(int == snapshot.numChildren()){
          const embed = new Discord.RichEmbed() // Make a success embed
            .setTitle("Success")
            .setDescription("Alle users zijn gechecked")
            .addField("Aantal gechecked", snapshot.numChildren(), true)
            .addField("Aantal verwijderd", notinsideguild, true)
            .setColor(65280)
            .setThumbnail("https://image.ibb.co/m49Ycn/success.png");
          message.channel.send({
            embed
          }); // Send success embed
        }
      });
    });
    }).catch(error => { // If an error occurs, this wil run
      const embed = new Discord.RichEmbed()
        .setTitle("Error")
        .setDescription("Helaas is er iets fout gegaan tijdens users check.")
        .setColor(16711680)
        .setThumbnail("https://image.ibb.co/eUF907/error.png");
      message.channel.send({
        embed
      }); // Send error embed
  });

}

1 个答案:

答案 0 :(得分:0)

您写错了,应该是这个

const account = message.member.guild.members.get(id)