如何使Discord机器人自动更改状态

时间:2020-04-15 22:56:37

标签: javascript node.js nodes discord discord.js

如何使我的Discord bot每秒更改一次状态

此代码无效

TypeError:client.user.setActivity不是函数

const activities_list = [
"with the &help command.", 
"with the developers console",
"with some code", 
"with JavaScript"
]; // creates an arraylist containing phrases you want your bot to switch through.

client.on('ready', () => {
    setInterval(() => {
        const index = Math.floor(Math.random() * (activities_list.length - 1) + 1); // generates a random number between 1 and the length of the activities array list (in this case 5).
        client.user.setActivity(activities_list[index]); // sets bot's activities to one of the phrases in the arraylist.
    }, 10000); // Runs this every 10 seconds.
});

1 个答案:

答案 0 :(得分:1)

我粘贴了您的代码,它完全按预期工作,有一件事:setInterval函数在执行之前等待,这对您而言意味着:它等待10秒,只有在它第一次设置状态后,因此您可能需要稍微更改代码,以便在启动时(而不是在第一个间隔结束之后)已经设置了状态:

const actvs = [
    "with the &help command.",
    "with the developers console",
    "with some code",
    "with JavaScript"
];

client.on('ready', () => {
    client.user.setActivity(actvs[Math.floor(Math.random() * (actvs.length - 1) + 1)]);
    setInterval(() => {
        client.user.setActivity(actvs[Math.floor(Math.random() * (actvs.length - 1) + 1)]);
    }, 10000);
});

编辑:我的Discord.js版本是v11.5.1,在v12中有所不同。 Here is a link to the docs for v12