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 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.
3 Answers
Reset to default 16I 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
console.log(client.guilds.keyArray())
and tell my what you get – Federico Grandi Commented Nov 10, 2018 at 18:27