简体   繁体   中英

How do i loop through for each guild member in database (quick.db)

Im trying to make a leaderboard command, i figured out how to do it for all of my bots users, but i cant figure out how to do it for just one guild i keep getting this error: TypeError: message.guild.members.forEach is not a function

Code:

const db = require('quick.db')
const ms = require('parse-ms')
const Discord = require(`discord.js`);
module.exports = {
    name: 'lb',
    execute(message) {
    const client = message.client
let prefix = db.get(`prefix_${message.guild.id}`)
let defaultprefix = "$$"
if(prefix === null) prefix = defaultprefix
let money = message.guild.members.forEach(member => db.get(`money_${member.id}`)).sort((a, b) => b.data - a.data)
money.length = 15;
var finalLb = ``;
for (var i in money) {
finalLb += `**#${money.indexOf(money[i])+1}** ${client.users.cache.get(money[i].ID.split('_')[1]) ? client.users.cache.get(money[i].ID.split('_')[1]).tag : `Deleted User#0000`} - ${money[i].data}\n`;
}

      const embed = new Discord.MessageEmbed()
      .setAuthor(`Top 15 richest users in this server`)
      .setDescription(finalLb)
      .setThumbnail(message.guild.iconURL({ dynamic: true }))
      .setColor(`990000`)      
      .setTimestamp()
  
      message.channel.send(embed);
    }
}```

The property .members of a Guild refers to the GuildMemberManager . To get the actual collection of members, you need to get the GuildMemberManager.cache property.

Change your code to the following and give it a try:

let money = message.guild.members.cache.forEach(<Code here>);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM