最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Client.guilds.get() not working as intended - Stack Overflow

programmeradmin2浏览0评论

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.

Share Improve this question edited Nov 10, 2018 at 21:37 Federico Grandi 6,8165 gold badges33 silver badges51 bronze badges asked Nov 10, 2018 at 18:10 user10633853user10633853 14
  • You're using the method correctly. The only thing you can do is checking the guild ID. Please note that the bot needs to be in the guild for it to work. – Federico Grandi Commented Nov 10, 2018 at 18:18
  • It is in the guild. – user10633853 Commented Nov 10, 2018 at 18:19
  • Are you sure the ID is the right one? How did you get it? – Federico Grandi Commented Nov 10, 2018 at 18:20
  • Right click on the guild and copy ID – user10633853 Commented Nov 10, 2018 at 18:22
  • Try using console.log(client.guilds.keyArray()) and tell my what you get – Federico Grandi Commented Nov 10, 2018 at 18:27
 |  Show 9 more comments

3 Answers 3

Reset to default 16

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
发布评论

评论列表(0)

  1. 暂无评论