如何使我的Discord机器人每10秒更改一次状态?

时间:2018-07-04 21:27:12

标签: discord discord.js

我有一个smol Discord机器人(带有discord.js-commando),我有以下代码:

var activevar = ["with the &help command.", "with the developers console", "with some code", "with JavaScript"];
var activities = activevar[Math.floor(Math.random()*activevar.length)];
client.on('ready', () => {
    client.user.setActivity(activities);
}

但是只有当我重新启动机器人时,它才会改变。有人可以帮我吗?

4 个答案:

答案 0 :(得分:2)

我更改了它,以便您可以将状态从播放更改为观看或收听。

const activities_list = [
    "For Rule Breakers", 
    "The purple names",
    "#general", 
    "The mods do their job"
    ]; // 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], { type: 'WATCHING' }); // sets bot's activities to one of the phrases in the arraylist.
    }, 10000); // Runs this every 10 seconds.
});

答案 1 :(得分:0)

尚未测试,但理论上应该可以工作。不会,尝试找出问题所在,这是一种好习惯。否则,让我知道

client.on("ready", function() {
    setInterval(function() {
        var actID = Math.floor(Math.random() * Math.floor(activevar.length));
        client.user.setActivity(activities);
    }, 10000)
});

答案 2 :(得分:0)

更简单:

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

答案 3 :(得分:0)

考虑到此内容的查看频率,我认为我会提供更新和更清晰的回复。

const state = 0;
const presences = [
    { type: 'PLAYING',  message: 'a game'  },
    { type: 'WATCHING', message: 'a video' }
];

setInterval(() => {
    state = (state + 1) % presences.length;
    const presence = presences[state];

    client.user.setActivity(presence.message, { type: presence.type });
}, 10000);