如何让我的 Discord 机器人每 10 秒更改一次状态? [英] How do I make my Discord bot change status every 10 seconds?

查看:10
本文介绍了如何让我的 Discord 机器人每 10 秒更改一次状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a smol Discord bot (with discord.js-commando), I have this code:

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

但这只会在我重新启动机器人时改变.有人可以帮我吗?

But that only changes it when I restart the bot. Can someone help me out here?

推荐答案

为 v12 上的用户编辑,现在使用机器人而不是客户端

Edited for users on v12 which now uses bot instead of client

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

这篇关于如何让我的 Discord 机器人每 10 秒更改一次状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆