简体   繁体   English

如何让我的 Discord 机器人自动更改状态

[英]How do I make my Discord bot change status auto

How do I make my Discord bot change status every second如何让我的 Discord 机器人每秒更改一次状态

This code does not work此代码不起作用

TypeError: client.user.setActivity is not a function类型错误:client.user.setActivity 不是 function

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.
});

I pasted your code and it totally worked as expected, a little thing: The setInterval function waits before executing, means in your case: it waits 10 seconds and only after that it sets the status the first time, so you might want to change your code a little bit so that it already sets a status when starting and not after the first interval is over:我粘贴了您的代码,它完全按预期工作,有一点: setInterval function 在执行前等待,这意味着在您的情况下:它等待 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);
});

Edit: My Discord.js version is v11.5.1, it is different in v12.编辑:我的 Discord.js 版本是 v11.5.1,在 v12 中有所不同。 Here is a link to the docs for v12 这是 v12 文档的链接

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

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