简体   繁体   中英

Client.guilds.get() not working as intended

I was writing a Discord bot for my friend, and when I was making a rainbow color role feature for him, I stopped on a big error.

First of all, this is my code:

var guild = client.guilds.get("493432486148177923")
var role = guild.roles.get("501752627709870080");
var role2 = guild.roles.get("493436150019784704");
setInterval(() => {
  role.setColor([Math.floor(Math.random() * 255), Math.floor(Math.random() * 255), Math.floor(Math.random() * 255)])
  role2.setColor([Math.floor(Math.random() * 255), Math.floor(Math.random() * 255), Math.floor(Math.random() * 255)])
}, 8000)

All stopped on the guild variable. It was giving out me null/undefined, and when going to guild.roles.get() , it caused my program to crash. I tried using .find() instead of .get() for finding the guild, but this also didn't work.

I don't know if you are still searching for an answer but I just encountered the same problem. After a bit of investigation I came up with a solution:

var server = client.guilds.cache.get(serverID);

This works for me. Hope it helps!

I can't give a reason as to why that happens, but the current method I'm using is this:

var g = client.guilds.get("GUILD-ID");
var c = g.channels.get("CHANNEL-ID");

Or in one line:

var c = client.guilds.get("GUILD-ID").channels.get("CHANNEL-ID");

Discord.js v13

This works as Client.guilds.get() :

var guild = undefined;
client.guilds.cache.forEach(g => { //Every guild
    if (g.id === "493432486148177923") { //Verify the guild's ID
        return guild = c;
    }
})

//If guild doesn't exist: guild = undefined

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