Discord.js只向用户提供他在数据库中保存的ID

时间:2018-06-17 15:51:01

标签: javascript discord discord.js

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

命令message.guild.members.get(ID)未定义

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

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

}

有人解决方案吗?

3 个答案:

答案 0 :(得分:3)

对于回到此问题的人们:最近的更改使这一点有所不同。

现在,您必须通过公会的成员缓存来吸引用户,例如:

message.guild.members.cache.get('id');

.get('id', ID)据我所知不存在,但似乎您正在为Collection.find(现在为deprecated)使用旧的重载。

答案 1 :(得分:0)

您需要指定如何获得该成员。例如:

message.guild.members.get("id", ID)

答案 2 :(得分:0)

其他解决方案都不适合我,所以我不得不使用 fetch 函数。问题是 fetch 函数返回了一个 promise,而 JS 的工作方式导致依赖结果的代码在 promise 完成之前就执行了。

const client = new Discord.Client();
let thanos = client.users.fetch('IDHERE');
thanos.then(function(result1) {
    //put your code that uses the result1 here
});