【问题标题】:When I run my bot, the `ready` event doesn't fire but the bot is online当我运行我的机器人时,`ready` 事件不会触发,但机器人在线
【发布时间】:2022-01-27 13:24:55
【问题描述】:

当我运行我的机器人时,ready 事件不会触发,但机器人在线。我的事件处理程序在 Client.ts 类的 start 方法中, 然后我在index.ts 文件中执行start 方法。

我的客户端类: ./classes/Client.ts

import { Client as DiscordClient, ClientOptions, Collection } from 'discord.js';
import fs from 'fs';
import path from 'path';

export class Client extends DiscordClient {
    commandarray: any[] = [];
    commands: Collection<string, any> = new Collection();
    constructor (options: ClientOptions, token: string) {
        super(options);

        this.login(token);
            
        }

    async start() {
        //Event Handler
        const eventDirectories = await fs.readdirSync('./events');
        for (const dir of eventDirectories) {
            const eventFiles = await fs.readdirSync(`./events/${dir}`).filter(file => file.endsWith(".ts"));

            if (eventFiles.length <= 0) 
                return console.log("[EVENT HANDLER] - Cannot find any events!");
    
    

            for (const file of eventFiles) {
                const event = require(`../events/${dir}/${file}`);

                if (event.once) {
                    this.once(event.name, (...args) => event.execute(...args, this));
                } else {
                    this.on(event.name, (...args) => event.execute(...args));
                }

            }
        }

        // Slash Command Handler
        const cmdDirectories = await fs.readdirSync('./commands');
        for (const dir of cmdDirectories) {
            const cmdFiles = await fs.readdirSync(`./commands/${dir}`).filter(file => file.endsWith(".ts"));

            if (cmdFiles.length <= 0)
                return console.log("[COMMAND HANDLER] - Cannot find any commands!");

            for (const file of cmdFiles) {
                const command = require(`../commands/${dir}/${file}`)
                await this.commandarray.push(command);
                await this.commands.set(command.name, command);
            }
        }
    }
};

我的 index.ts 文件: ./index.ts

import { Client } from "./classes/Client";
import { config } from "dotenv";

config();

export const client: Client = new Client({ intents: 515 }, process.env.token!);

client.start();

我的就绪事件: ./events/Client/ready.ts

import { Client } from '../../classes/Client';

export default {
    name: 'ready',
    once: true,
    async execute(client: Client) {
        await console.log(`Logged in as ${client.user?.tag}`);
        await client.application?.commands.set(client.commandarray);
    }
}

编辑,感谢 Rahuletto,我解决了第一个问题,但发现了一个新问题并相应地更新了问题。

【问题讨论】:

    标签: node.js typescript discord.js


    【解决方案1】:

    这是一条错误的道路。您应该显示您的路径结构(文件格式)。

    我想是../events/Client/ready.ts

    【讨论】:

    • 非常感谢!它帮助解决了第一个错误,但现在发现了一个新问题。
    【解决方案2】:

    我使用export default 导出事件 所以,我应该使用event.default.&lt;property/function&gt;,而不是在处理程序中使用event.&lt;property/function&gt;

    【讨论】:

      猜你喜欢
      • 2023-02-08
      • 2018-05-11
      • 2022-11-15
      • 2021-04-14
      • 2021-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-20
      相关资源
      最近更新 更多