首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让我的不一致机器人每10秒改变一次状态?

如何让我的不一致机器人每10秒改变一次状态?
EN

Stack Overflow用户
提问于 2018-07-05 05:27:12
回答 6查看 40.7K关注 0票数 3

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

代码语言:javascript
复制
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);
}

但这只会在我重启机器人时改变它。有人能帮帮我吗?

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2018-07-10 22:50:22

为v12上的用户编辑,它现在使用bot而不是客户端

代码语言:javascript
复制
const activities = [
  "with the &help command.",
  "with the developers console.",
  "with some code.",
  "with JavaScript."
];

bot.on("ready", () => {
  // run every 10 seconds
  setInterval(() => {
    // generate random number between 1 and list length.
    const randomIndex = Math.floor(Math.random() * (activities.length - 1) + 1);
    const newActivity = activities[randomIndex];

    bot.user.setActivity(newActivity);
  }, 10000);
});
票数 5
EN

Stack Overflow用户

发布于 2021-01-14 18:33:23

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

代码语言:javascript
复制
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.
});
票数 2
EN

Stack Overflow用户

发布于 2021-07-02 09:54:47

考虑到这篇文章的浏览频率,我想我应该提供一个更新更清晰的回复。

代码语言:javascript
复制
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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51180927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档