自定义骑行存在(discord.js v14)

问题描述 投票:0回答:1
const mongoose = require("mongoose")
require("dotenv").config()
const {Client,ActivityType} = require("discord.js")
module.exports = {
    name: 'ready',
    /**
     * 
     * @param {Client} client 
     */
    async execute(client) {
        console.log(`✅ ${client.user.tag} is Ready!`);
        const activities = [{name: "Made By andrexdev",type: ActivityType.Playing},
        {name: `${client.guilds.cache.size} Servers`,type: ActivityType.Watching},
        {name: `${client.users.cache.size} People`,type: ActivityType.Watching}]
        counter = 0
        setTimeout(() => {
            if(counter == activities.length+1) counter = 0
            client.user.setActivity({
                activities: [activities[counter]],
                status: "online"
            })
            counter+=1;
        }, 5000);
        
        await mongoose.connect(process.env.MONGOOSE_CONNECTION_STRING).then(() => {
            console.log("✅ Connected to MongoDB!")
        })
    }
}

它给出了一个错误: It gives this error

我希望它循环遍历数组中的所有存在 无论我为什么尝试,我都无法弄清楚

node.js discord.js
1个回答
0
投票

您正在检查计数器何时高于

length+1
,这意味着您在
activities[activities.length]
处遇到错误,即
undefined

通过活动进行轮换的一种更简单的方法如下所示,使用模 (

%
) 运算来保持
counter
循环:0、1、2,然后返回 0 并重复。

        const activities = [{name: "Made By andrexdev",type: ActivityType.Playing},
        {name: `${client.guilds.cache.size} Servers`,type: ActivityType.Watching},
        {name: `${client.users.cache.size} People`,type: ActivityType.Watching}]
        counter = 0
        setTimeout(() => {
            client.user.setActivity({
                activities: [activities[counter]],
                status: "online"
            })
            counter = (counter + 1) % activities.length;
        }, 5000);
© www.soinside.com 2019 - 2024. All rights reserved.