Discord.js V12机器人每30分钟间隔编辑一个角色

时间:2020-09-20 05:11:21

标签: javascript node.js discord discord.js

我希望我的机器人每30分钟编辑一次角色。我不确定该怎么做,所以我看了另一个线程。这是代码:

client.on('ready', () => {
var colors = ['#8585ff','#fff681','#a073fd','#fd73b9'];
    var random = Math.floor(Math.random() * colors.length);
    var role = message.guild.roles.find("name", "Admin");
    setInterval(() => {
        role.edit({
            color: colors[random]
        })
    }, 1800000);

})

我尝试运行此代码,但是出现以下错误:

(node:95) UnhandledPromiseRejectionWarning: ReferenceError: message is not defined
(node:95) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:95) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

你能帮帮我吗?提前谢谢

1 个答案:

答案 0 :(得分:0)

Client#ready事件中,默认情况下没有Message对象,因此您将无法使用Message.guild属性。

相反,您必须从Guild获取GuildManager。 (Client#guilds


Discord JS v12:

client.on("ready", () => {
    const Guild = client.guilds.cache.get("GuildID");

    if (!Guild) return console.error("Couldn't find the guild.");

    const Role = Guild.roles.cache.find(role => role.name == "Admin");
    // You should really use the ES6 syntax for this.
    // Instead of: Guild.roles.find("name", "Admin");

    if (!Role) return console.error("Couldn't find the role.");

    // Your code here.
});

Discord JS v11:

client.on("ready", () => {
    const Guild = client.guilds.get("GuildID");

    if (!Guild) return console.error("Couldn't find the guild.");

    const Role = Guild.roles.find(role => role.name == "Admin");

    if (!Role) return console.error("Couldn't find the role.");
});