Client.guilds.get() not working as intended

Goblin 01 picture Goblin 01 · Nov 10, 2018 · Viewed 13.6k times · Source

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.

Answer

Y. Georgiev picture Y. Georgiev · Mar 5, 2020

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!