首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Discord.js仅使用其ID获取用户保存在数据库中的内容

Discord.js仅使用其ID获取用户保存在数据库中的内容
EN

Stack Overflow用户
提问于 2018-06-17 23:51:01
回答 2查看 11.9K关注 0票数 1

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

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

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

代码语言:javascript
复制
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
  });

}

有人有解决方案吗?

EN

回答 2

Stack Overflow用户

发布于 2018-06-18 10:22:13

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

代码语言:javascript
复制
message.guild.members.get("id", ID)
票数 1
EN

Stack Overflow用户

发布于 2021-03-04 02:41:32

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

代码语言:javascript
复制
const client = new Discord.Client();
let thanos = client.users.fetch('IDHERE');
thanos.then(function(result1) {
    //put your code that uses the result1 here
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50898201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档