如何使Discord机器人每隔10秒左右更改其状态? (包括“听”,“播放”和“观看”之类的内容)

时间:2018-09-23 10:48:03

标签: javascript discord discord.js

请告诉我该怎么做。 这是我的无效代码:

let activity = ["`${client.users.size} commands | ><help`, {type: "LISTENING"}", "`on ${client.guilds.size} servers | ><help`, {type: "PLAYING"}
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);
});

1 个答案:

答案 0 :(得分:0)

我想您对数组中的引号有疑问,请尝试以下操作:

let activityTypes = ['PLAYING','STREAMING','LISTENING','WATCHING']
let randomType = activityTypes[Math.floor((Math.random()*activityTypes.length))]

setInterval(async ()=>{
    await client.user.setActivity('Your text here.', { type: randomType })
},10000)

但是请记住,10秒可能会达到不一致的API速率限制。