0
votes

I have a discord.js bot running on Heroku, when it gets added or removed from a guild it sends a message to a specific channel in my server. This was working fine a week ago, and now when I update it (push update through GitHub to Heroku) it sends the message saying that it was removed from a guild. There is an error:

UnhandledPromiseRejectionWarning: Error: 504 Gateway Time-out

This is the code:

client.on('guildCreate', guild => {
    try{
        //Check for system channel
        if(!guild.systemChannel) return false;
        //Sends message to system channel
        guild.systemChannel.send('Thank you for adding me to your server. Run ``-setup`` to begin setup.')
        //My server and channel
        const server = client.guilds.cache.get('guildID')
        const channel = server.channels.cache.get('channelID')
        //The embed which sends to channel
        const joinEmbed = new Discord.MessageEmbed()
        .setTitle("Joined")
        .setDescription("Optic was added to a server")
        .addFields(
            { name: 'Name', value: guild.name, inline: false },
            { name: 'GuildId', value: guild.id,inline: false },
            { name: 'Guild OwnerId', value: guild.ownerID, inline: false },
            { name: 'Member Count', value: guild.memberCount, inline: false },
            { name: 'Total Guilds', value: client.guilds.cache.size, inline: true },
        )
        channel.send(joinEmbed)
    }catch(error){
        console.log("There was an error sending join embed to channel")
    }
});

client.on('guildDelete', guild => {
    try{
        //My server and channel:
    const server = client.guilds.cache.get('guildID')
    const channel = server.channels.cache.get('channelID')
    //The embed which sends to channel
    const leaveEmbed = new Discord.MessageEmbed()
    .setTitle("Removed")
    .setDescription("Optic was removed from a server")
    .addFields(
        { name: 'Name', value: guild.name, inline: false },
        { name: 'GuildId', value: guild.id,inline: false },
        { name: 'Guild OwnerId', value: guild.ownerID, inline: false },
        { name: 'Member Count', value: guild.memberCount, inline: false },
        { name: 'Total Guilds', value: client.guilds.cache.size, inline: true },
    )
    channel.send(leaveEmbed)
    }catch(error){
        console.log("There was an error sending leave embed to channel.")
    }
    
  });

When it is updated, as I stated earlier, it sends the leave embed with the following showing as undefined:

  • Name
  • Guild Owner ID
  • Member Count

Image of embed

Any help will be appreciated on what is happening. Thanks :)

Update (22/02/21): This problem still happens, if it were to turn off and on by itself for a few seconds, it will still send the embed. Another Update (23/02/21): There have been errors, this is it:

UnhandledPromiseRejectionWarning: Error: 504 Gateway Time-out

Comment if you need the whole error as I'm not sure if the rest is helpful

Bounty Reason: Draw attention This question has not received enough attention.

Code where I declare the client:

const Discord = require('discord.js');

const client = new Discord.Client();
2
Can you show the full error?Cursed
I have no idea why, but it only does that error sometimes. The leave embed is still sending by itself though.beans
can you update you question with the code where you declare the clientCursed

2 Answers

2
votes

You need to enable intents, you can read more about it here, and list of intents here and there are two Privileged Gateway Intents PRESENCE INTENT and SERVER MEMBERS INTENT

PRESENCE INTENT:If your bot tracks presence data, you may need the presence intent to receive presence event data.

SERVER MEMBERS INTENT: If your bot tracks server members or downloads the entire member list, you may need the server members intent to receive member events and the member list.

Privileged Gateway intents can be enabled on discord.com/developers/applications/ -> Settings/Bot.

In your case you will need the intent GUILDS which provides the access to guildDelete and guildCreate:

const { Client } = require('discord.js');
const client = new Client({ ws: { intents: ['GUILDS'] } });
0
votes

guildDelete is not guaranteed to have the full data, only if it was cached you can get it, sometimes the data was cached and it executed with 0 errors, and other times when it wasn't cached fully, so you get that error.