如何在 discord.js 中更改状态?

How to make a changing status in discord.js?

我遵循了更改状态的教程,当我 运行 机器人出现此错误时:

/home/runner/Nilex-1/index.js:16
                let status = arrayOfStatus(index);
                             ^

TypeError: arrayOfStatus is not a function
    at Timeout._onTimeout (/home/runner/Nilex-1/index.js:16:30)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7)
repl process died unexpectedly: exit status 1

我的代码是:

client.on("ready", () => {
    console.log(`${client.user} has booted up!`);
    console.log(`Bot user ID: ${client.user.id}`);

    let arrayOfStatus = [`over ${client.guilds.cache.size} servers/guilds!`, `myself being developed!`];

    let index = 0;
    setInterval(() => {
        if (index === arrayOfStatus.legth) index = 0;
        let status = arrayOfStatus(index);
        client.user.setActivity(status, { type: "WATCHING" }).catch(console.error);
        index++;
    }, 3000);
});

有什么帮助吗?

您可以通过 [] 而不是 () 访问数组元素,正如 futur 在他直接对此 post 的评论中提到的那样,我想详细说明一下。

你的辅导员想说的基本上是这样的:

// Making an array of status
const array = ['hello','bonjour','konichiwa']; 
/* This array has certain values 
- at array[0] being hello, 
- array [1] being bonjour and 
- array[2] being konichiwa,
 so now you may declare a variable to access them,
and further increment it as you're doing ,
well great! I'd just make it simpler and break it down for you :) */
let i = 0
setInterval(() => { // setInterval function to pass in a timeout for changing
                if (i == array.legth) index = 0; // says if index reaches 2 then reset the value of i so we can go to the first status from last again 
                let status = array[i] // states let status be the ith value of the array 
                client.user.setActivity(status); // setting the status :)
                i++; // adding one ( incrementing ) the variable i so we can move to the next status!
        }, 3000) // timeout ( would change status every 3 seconds )

Zero 所以,这是现在的代码:

let array = {  0: {  name: "Minecraft", type: "PLAYING", }, 1: { name: "to <help", type: "WATCHING" } }

let i = 0
setInterval(() => {
                if (i == array.legth) index = 0;
                let status = array[i]
                client.user.setActivity(array[i].name, { type: array[i].type });
                i++;
        }, 3000)

})

我收到一个错误:

/home/runner/Nilex-1/index.js:14
                client.user.setActivity(array[i].name, { type: array[i].type });
                                                 ^

TypeError: Cannot read property 'name' of undefined
    at Timeout._onTimeout (/home/runner/Nilex-1/index.js:14:50)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7)
repl process died unexpectedly: exit status 1