admin管理员组

文章数量:1335418

When I send a message with a mand, the bot must give a user with a special ID a different role. How do I do that if I only have the user's ID.

the mand message.guild.members.get(ID) is undefined

This is the code that I use. const account = message.guild.members.get(key); doesn't work...

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

  const axios = require('axios');
  // Call a Axios Request
  axios.get('/' + 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(".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(".png");
      message.channel.send({
        embed
      }); // Send error embed
  });

}

When I send a message with a mand, the bot must give a user with a special ID a different role. How do I do that if I only have the user's ID.

the mand message.guild.members.get(ID) is undefined

This is the code that I use. const account = message.guild.members.get(key); doesn't work...

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

  const axios = require('axios');
  // Call a Axios Request
  axios.get('https://api.guildwars2./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
  });

}

Someone a solution?

Share Improve this question asked Jun 17, 2018 at 15:51 Pixel BrothersPixel Brothers 111 gold badge1 silver badge4 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

For people returning to this question: Recent changes have made this slightly different.

Now you must get the user via the guild's members cache, as such:

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

.get('id', ID) doesn't exist as far as I know, but it seems like you were going for the old overload for Collection.find, which is now deprecated.

You need to specify how you’re going to get the member. For example:

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

None of the other solutions worked for me, so I had to use the fetch function. The problem is that the fetch function returns a promise, and the way JS works caused the code that was relying on the result to execute before the promise was fulfilled.

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

本文标签: javascriptDiscordjs get user with only his ID whats saved inside a databaseStack Overflow