如何使我的Discord bot每秒更改一次状态
此代码无效
TypeError:client.user.setActivity不是函数
const activities_list = [
"with the &help command.",
"with the developers console",
"with some code",
"with JavaScript"
]; // 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]); // sets bot's activities to one of the phrases in the arraylist.
}, 10000); // Runs this every 10 seconds.
});
尝试仅使用索引,您有一个对象而不是数组,所以它应该可以工作!希望这有效!祝你有美好的一天!
我粘贴了您的代码,它完全按预期工作,有一件事:setInterval
函数在执行之前等待,这意味着您的情况:它等待10秒,只有在这之后它才第一次设置状态,因此您可能想要稍微更改代码,以便在启动时(而不是在第一个间隔结束之后)已经设置了状态: