首页 > 解决方案 > Why is message.client.users.cache.random() only getting people that have messaged?

问题描述

The command I set up just grabs a random user to spice up the message a bit. I had to fix something last night and when I restarted the bot, it is now only getting anyone that has messaged. Before, it was getting any random person in the server like it's supposed to. I have also tried to use

message.guild.members.cache.random();

with the same results. Here is the rest of the code from that command

const Discord = require('discord.js');
const fs = require('fs');
const colors = require('../../colors.json');

const candyAmount = require('../../candyheld.json');

const client = new Discord.Client();

module.exports = {
    name: 'trickortreat',
    description: 'Special Halloween command',
    execute(message, args) {

        if (!candyAmount[message.author.id]) {
            candyAmount[message.author.id] = {
                candyStored: 5
            }
            
            fs.writeFile('./candyheld.json', JSON.stringify(candyAmount), err => {
                if (err) console.error(err);
            });
            
            message.delete();
            return message.channel.send('For starting the event, you have been given 5 pieces of candy!');
        }
        
        // Stores a random number of candy from 1-3
        let candyGiven = Math.floor(Math.random() * 3 + 1);
        let jackpot = Math.floor(Math.random() * 10 + 20 );
        let highJackpot = Math.floor(Math.random() * 100 + 200);

        let randomMember = message.client.users.cache.random();
        
        // Sets the possible messages to be received
        let trickortreatmessage = [
            'The scarecrow standing behind you jumps out at you and makes you drop all your candy!',
            `${randomMember} was nice enough to give you Candy! ${message.author} got ${candyGiven} pieces of candy!`,
            `Oh no! ${message.author} asked ${randomMember} for Candy and they decided to egg you instead!`,
            `The Headless Horseman rides and slashes his way to every bin of candy all just to give you everything he got!\n\n${message.author} got ${jackpot} Candy!`,
            `The wolves howl in sync as Candy rains from the sky! The adrenaline makes you move at lightning speed to grab every piece of Candy!\n\n ${message.author} got ${highJackpot} Candy!!!`
        ]

        // Store one of the random messages
        let trickortreat;
        let odds = Math.floor(Math.random() * 5000);
        if (odds <= 25) trickortreat = trickortreatmessage[4]; // .5% chance
        else if (odds <= 49) trickortreat = trickortreatmessage[0]; // .5% chance
        else if (odds <= 499) trickortreat = trickortreatmessage[3]; // 9% chance
        else if (odds <= 1999) trickortreat = trickortreatmessage[2]; // 30% chance
        else trickortreat = trickortreatmessage[1]; // 60% chance

        if (trickortreat == trickortreatmessage[0]) {
            candyAmount[message.author.id].candyStored = 0;
        } else if (trickortreat == trickortreatmessage[1]) {
            candyAmount[message.author.id].candyStored += candyGiven;
        } else if (trickortreat == trickortreatmessage[3]) {
            candyAmount[message.author.id].candyStored += jackpot;
        } else if (trickortreat == trickortreatmessage[4]) {
            candyAmount[message.author.id].candyStored += highJackpot;
        }
        
        fs.writeFile('./candyheld.json', JSON.stringify(candyAmount), err => {
            if (err) console.error(err);
        });

        const embed = new Discord.MessageEmbed()
        .setColor(colors.serverRed)
        .addField('\u200B', `**${trickortreat}**`)

        message.delete();
        message.channel.send(embed);
    },
};

标签: javascriptnode.jsdiscorddiscord.js

解决方案


我遇到了同样的问题,即“message.guild.members.cache”开始为空,即使我没有进行任何与之相关的代码更改。这是由于 Discord 所做的更改。

不过很容易修复。您需要登录Discord 开发者站点 > Applications,选择您的应用程序/机器人,进入“Bot”选项卡,向下滚动到“Privileged Gateway Intents”并勾选“PRESENCE INTENT”和“SERVER MEMBERS INTENT”:

在此处输入图像描述

请注意,如果您的机器人位于 100 台或更多服务器中,则需要将其列入白名单。

之后我重新启动了机器人,现在它又可以正常工作了。


推荐阅读