blackcat.js-discord - v1.0.17
    Preparing search index...

    Interface CommandManagerOptions

    Cấu hình khởi tạo CommandManager.

    Interface này quyết định cách command được load, prefix được xử lý, event messageCreate có được đăng ký tự động hay không, và cách xử lý lỗi khi load command.

    const manager = new CommandManager({
    directory: "./commands",
    prefix: "!",
    registerMessageCreateEvent: true,
    mentionPrefix: true,
    onLoad(command) {
    console.log(`Loaded: ${command.commandName}`);
    },
    onError({ file, error }) {
    console.error(`Không thể load ${file}`, error);
    },
    });
    interface CommandManagerOptions {
        directory: string;
        mentionPrefix?: boolean;
        mentionPrefixResponse?: (prefix: string[]) => string | MessageReplyOptions;
        onError?: (data: { error: Error; file: string }) => void;
        onLoad?: (command: CommandBuilder, manager: CommandManager) => void;
        prefix: Prefix;
        registerMessageCreateEvent?: boolean;
    }
    Index

    Properties

    directory: string

    Đường dẫn tới thư mục chứa các file command.

    CommandManager sẽ quét đệ quy toàn bộ thư mục này và tự động load các file .js hoặc .ts, trừ file .d.ts.

    Category của command sẽ được lấy từ thư mục cấp đầu tiên bên trong directory.

    const manager = new CommandManager({
    directory: "./commands",
    prefix: "!",
    });
    commands/
    ├── admin/
    │   └── ban.ts      // category: "admin"
    └── info/
        └── help.ts     // category: "info"
    
    mentionPrefix?: boolean

    Có cho phép dùng mention bot như một prefix hay không.

    Khi bật, người dùng có thể gọi command bằng cú pháp: <@bot_id> help.

    true
    
    const manager = new CommandManager({
    directory: "./commands",
    prefix: "!",
    mentionPrefix: true,
    });
    mentionPrefixResponse?: (prefix: string[]) => string | MessageReplyOptions

    Hàm tạo nội dung phản hồi khi người dùng chỉ mention bot hoặc nhập prefix nhưng không nhập tên command.

    Hàm nhận danh sách prefix hiện tại và trả về chuỗi hoặc MessageReplyOptions.

    mentionPrefixResponse: (prefixes) => {
    return `Prefix hiện tại: ${prefixes.join(", ")}`;
    }
    mentionPrefixResponse: (prefixes) => ({
    content: `Bạn có thể dùng: ${prefixes.map((p) => `\`${p}\``).join(", ")}`,
    allowedMentions: { repliedUser: false },
    })
    onError?: (data: { error: Error; file: string }) => void

    Callback được gọi khi một file command load thất bại.

    Nếu không truyền onError, lỗi khi load command sẽ bị bỏ qua trong hàm loadFile.

    onError({ file, error }) {
    console.error(`Lỗi khi load file ${file}:`, error.message);
    }
    onLoad?: (command: CommandBuilder, manager: CommandManager) => void

    Callback được gọi sau khi một command được load thành công.

    Hữu ích để log, thống kê hoặc kiểm tra command trong quá trình khởi động bot.

    onLoad(command) {
    console.log(`Đã load command: ${command.commandName}`);
    }
    prefix: Prefix

    Prefix của bot.

    Prefix là chuỗi đứng đầu tin nhắn để bot biết tin nhắn đó là command. Có thể dùng một prefix, nhiều prefix, hoặc hàm trả về prefix theo từng guild.

    prefix: "!"
    
    prefix: ["!", "?"]
    
    prefix: async (guildID) => {
    const config = await getGuildConfig(guildID);
    return config.prefix;
    }
    registerMessageCreateEvent?: boolean

    Có tự động đăng ký listener cho event messageCreate hay không.

    Mặc định là true. Nếu đặt là false, bạn cần tự xử lý message hoặc gọi các API resolve command thủ công.

    true
    
    const manager = new CommandManager({
    directory: "./commands",
    prefix: "!",
    registerMessageCreateEvent: false,
    });