Discord.js 机器人更新 activity
Discord.js bot update activity
我想在我的 BOT 上创建一个包含各种 activity 的更新状态。这是我到目前为止所做的代码:
const activities = [
{
type : "PLAYING",
activity : "with the ball"
},
{
type : "WATCHING",
activity : "your soul"
}
];
for (var i = 0; i < activities.length; i++) {
setInterval(() => client.user.setActivity(`${activities[i].activity}`, { type: `${activities[i].type}` }));
};
当我尝试启动BOT时,终端显示
TypeError: Cannot read property 'activity' of undefined
可能代码完全错误,但我不太擅长javascript。有什么建议吗?
setInterval 将强制执行异步操作,但堆栈值仅在循环上下文中起作用,因为它是一个指针。你可以先创建另一个变量,javascripts 垃圾收集器会注意到“哦,我不能清理这个,我稍后需要它”,所以 javascript 在他的堆栈中仍然有这个值
for (var i = 0; i < activities.length; i++) {
var putonstack = activities[i];
setInterval(() => client.user.setActivity(`${putonstack.activity}`, { type: `${putonstack.type}` }));
};
您也可以通过这样的迭代来完全避免这种情况:
// for (var i = 0, value; value = activities[i]; i++)
for (var value of activities) {
setInterval(() => client.user.setActivity(`${value.activity}`, { type: `${value.type}` }));
};
并解决您的问题:
const activities = [
{
type : "PLAYING",
activity : "with the ball"
},
{
type : "WATCHING",
activity : "your soul"
}
];
const timeoutForNms = 10000; // 10 seocnds
let currentActivity = 0;
setInterval(() => {
console.log('set activity to %s type to %s', activities[currentActivity].activity, activities[currentActivity].type);
client.user.setActivity(`${activities[currentActivity].activity}`, { type: `${activities[currentActivity].type}` });
currentActivity++;
if (currentActivity === activities.length) {
currentActivity = 0;
}
}, timeoutForNms);
我想在我的 BOT 上创建一个包含各种 activity 的更新状态。这是我到目前为止所做的代码:
const activities = [
{
type : "PLAYING",
activity : "with the ball"
},
{
type : "WATCHING",
activity : "your soul"
}
];
for (var i = 0; i < activities.length; i++) {
setInterval(() => client.user.setActivity(`${activities[i].activity}`, { type: `${activities[i].type}` }));
};
当我尝试启动BOT时,终端显示
TypeError: Cannot read property 'activity' of undefined
可能代码完全错误,但我不太擅长javascript。有什么建议吗?
setInterval 将强制执行异步操作,但堆栈值仅在循环上下文中起作用,因为它是一个指针。你可以先创建另一个变量,javascripts 垃圾收集器会注意到“哦,我不能清理这个,我稍后需要它”,所以 javascript 在他的堆栈中仍然有这个值
for (var i = 0; i < activities.length; i++) {
var putonstack = activities[i];
setInterval(() => client.user.setActivity(`${putonstack.activity}`, { type: `${putonstack.type}` }));
};
您也可以通过这样的迭代来完全避免这种情况:
// for (var i = 0, value; value = activities[i]; i++)
for (var value of activities) {
setInterval(() => client.user.setActivity(`${value.activity}`, { type: `${value.type}` }));
};
并解决您的问题:
const activities = [
{
type : "PLAYING",
activity : "with the ball"
},
{
type : "WATCHING",
activity : "your soul"
}
];
const timeoutForNms = 10000; // 10 seocnds
let currentActivity = 0;
setInterval(() => {
console.log('set activity to %s type to %s', activities[currentActivity].activity, activities[currentActivity].type);
client.user.setActivity(`${activities[currentActivity].activity}`, { type: `${activities[currentActivity].type}` });
currentActivity++;
if (currentActivity === activities.length) {
currentActivity = 0;
}
}, timeoutForNms);