0

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);
    },
};
QuazArxx
  • 150
  • 2
  • 15
  • 1
    Does this answer your question? [None of my discord.js guildmember events are emitting and my functions are timing out](https://stackoverflow.com/questions/64559390/none-of-my-discord-js-guildmember-events-are-emitting-and-my-functions-are-timin) – Lioness100 Oct 28 '20 at 13:52
  • That has nothing to do with my concern – QuazArxx Oct 28 '20 at 15:30
  • Yeah it does. Your user cache is starting empty, which has to do with intents. – Lioness100 Oct 28 '20 at 15:49
  • It was working before last night perfectly fine, picking random people when I first introduced the command. Why would intents now be a problem. Again this doesn’t pertain to my issue – QuazArxx Oct 28 '20 at 16:55
  • 1
    Again, it does. Discord just recently started enforcing the two intents outlined in the answer to the linked question. As recently as last night for some people such as yourself. – Lioness100 Oct 28 '20 at 18:15

1 Answers1

0

I had the same problem that "message.guild.members.cache" starts empty even though I hadn't done any code changes related to it. It was due to changes made by Discord.

It is easy to fix though. You need to log in to the Discord developer site > Applications, choose your application/bot, got to the "Bot" tab, scroll down to "Privileged Gateway Intents" and tick off the "PRESENCE INTENT" and "SERVER MEMBERS INTENT ":

enter image description here

Note that if your bot is in 100 or more servers you need to whitelist it.

After that I restarted the bot and now it works fine again.

Jovee
  • 23
  • 5
  • Hello and welcome to SO! Please take the [tour], and read [answer]. Don't include "I have the same problem" section as part of your answer. it is not helpful. The rest looks good ☺️ – Tomer Shetah Dec 20 '20 at 07:56