简体   繁体   English

Client.guilds.get() 未按预期工作

[英]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.我正在为我的朋友写一个 Discord 机器人,当我为他制作彩虹色的角色功能时,我遇到了一个大错误。

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.一切都停在guild变量上。 It was giving out me null/undefined, and when going to guild.roles.get() , it caused my program to crash.它给了我空/未定义,当去guild.roles.get()时,它导致我的程序崩溃。 I tried using .find() instead of .get() for finding the guild, but this also didn't work.我尝试使用.find()而不是.get()来查找公会,但这也没有用。

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不和谐.js v13

This works as Client.guilds.get() :这作为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM