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



我有一个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);
}

但这只会在我重新启动机器人时更改它。有人可以在这里帮助我吗?

程序描述和目的: 当机器人上线时,每十秒从活动列表中随机选择一个随机活动,并使用新活动更新机器人:

const activities = [
"with the &help command.",
"with the developers console.",
"with some code.",
"with JavaScript."
];
client.on("ready", () => {
// run every 10 seconds
setInterval(() => {
// generate random number between 0 and length of array.
const randomIndex = Math.floor(Math.random() * activities.length);
const newActivity = activities[randomIndex];
client.user.setActivity(newActivity);
}, 10_000);
});

要考虑的材料:

  • https://discord.js.org/#/docs/discord.js/main/class/ClientUser?scrollTo=setActivity
  • https://developer.mozilla.org/en-US/docs/Web/API/setInterval
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random

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

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

这也适用于 v13。

const statuses = [
{ name: "to nothing", type: "LISTENING" },
{ name: "something", type: "PLAYING" },
{ name: "your commands", type: "WATCHING" },
];
client.on("ready", () => {
setInterval(() => {
var randomStatus = statuses[Math.floor(Math.random() * statuses.length)];
client.user.setActivity(randomStatus);
}, 10000);
});

考虑到查看的频率,我想我会提供更新和更清晰的响应。

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

没有测试过,但它应该在理论上工作。它不是,试着找出问题所在,这是很好的做法。否则,请告诉我

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

每 10 秒更改一次,用于播放、观看、收听活动

const activities_list = [
{ type: 'PLAYING',  message: 'a game'  },
{ type: 'WATCHING', message: 'a video' },
{ type: 'LISTENING', message: 'a music' }
];
client.on('ready', () => {
setInterval(() => {
const index = Math.floor(Math.random() * (activities_list.length - 1) + 1);
client.user.setActivity(activities_list[index].message, { type: activities_list[index].type });
}, 10000);
});

我正在使用不和谐.js v12。

client.on("ready", () => {
console.log(`ok`);
const who = ["hi", "hello"];
setInterval(() => {
const burh = Math.floor(Math.random() * who.length);    
client.user.setPresence({ activity: { name: who[burh]}, status: 'dnd'}); 
}, 5000);  
});

最新更新