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

    Interface ClientOptions

    Options khởi tạo Client.

    interface ClientOptions {
        commandManager?: CommandManager;
        config?: Config;
        discordClientOptions?: ClientOptions;
        eventManager?: EventManager;
        slashCommandManager?: SlashCommandManager;
    }
    Index

    Properties

    commandManager?: CommandManager

    Quản lý và tự động load các command cho ứng dụng

    Tùy chọn này cho phép bạn cấu hình một instance của CommandManager để tự động quét, import và đăng ký các command từ thư mục chỉ định.

    undefined
    

    Khi được cung cấp, CommandManager sẽ:

    1. Duyệt qua thư mục directory
    2. Tự động import các file command
    3. Đăng ký command vào hệ thống xử lý message
    4. Áp dụng prefix để nhận diện command
    5. Gọi callback onLoad hoặc onError tùy theo trạng thái load

    Các tùy chọn quan trọng:

    • directory: Đường dẫn tới thư mục chứa các file command
    • prefix: Tiền tố dùng để kích hoạt command (ví dụ: "!")
    • onLoad: Callback khi load command thành công
    • onError: Callback khi xảy ra lỗi khi load command

    Cơ chế hoạt động:

    1. Khi khởi tạo hệ thống
    2. CommandManager sẽ quét toàn bộ file trong directory
    3. Mỗi file sẽ được import và validate cấu trúc command
    4. Nếu hợp lệ → đăng ký vào collection và gọi onLoad
    5. Nếu lỗi → gọi onError với thông tin chi tiết
    6. Khi người dùng gửi message:
      • Kiểm tra có bắt đầu bằng prefix hay không
      • Parse commandName và args
      • Tìm command tương ứng và thực thi

    Phù hợp khi:

    • Dự án có nhiều command cần tổ chức theo file
    • Muốn auto-load command thay vì đăng ký thủ công
    • Sử dụng message-based command với prefix

    Không nên dùng khi:

    • Dự án nhỏ, ít command
    • Chỉ sử dụng slash command (interaction-based)
    • Muốn tự kiểm soát toàn bộ flow xử lý message
    const client = new Client({
    commandManager: new CommandManager({
    directory: "./commands",
    prefix: "!",
    onLoad: (command) => {
    console.log(`commands: ${command.commandName} - ok`);
    },
    onError: (command) => {
    console.error(`${command.file}:`, command.error);
    }
    })
    });
    // Không sử dụng CommandManager (tự xử lý message)
    const client = new Client({
    // commandManager: undefined
    });

    CommandManager

    config?: Config

    Cấu hình tổng thể của bot

    Tùy chọn này cho phép bạn truyền vào các thiết lập cần thiết để khởi tạo và vận hành bot, bao gồm token và các cấu hình mở rộng.

    undefined
    

    Khi được cung cấp, config sẽ:

    1. Được sử dụng trong quá trình khởi tạo client
    2. Cung cấp botToken để đăng nhập Discord
    3. Cho phép mở rộng thêm các cấu hình tùy ý thông qua index signature

    Cấu trúc:

    • botToken: Token dùng để đăng nhập bot (bắt buộc)
    • [key: string]: unknown: Cho phép định nghĩa thêm các config custom

    Cơ chế hoạt động:

    1. Khi khởi tạo hệ thống
    2. Kiểm tra sự tồn tại của botToken
    3. Sử dụng token để gọi client.login
    4. Các giá trị khác có thể được truy cập ở bất kỳ đâu trong hệ thống thông qua object config

    Phù hợp khi:

    • Muốn tập trung toàn bộ cấu hình vào một nơi
    • Cần truyền các giá trị như API keys, prefix, options custom
    • Muốn dễ dàng mở rộng cấu hình mà không cần sửa type

    Không nên dùng khi:

    • Lưu trữ thông tin nhạy cảm trực tiếp trong source code (nên dùng env)
    const client = new Client({
    config: {
    botToken: process.env.BOT_TOKEN!,
    prefix: "!",
    apiKey: "your-api-key",
    debug: true
    }
    });

    // Sử dụng config trong hệ thống const token = client.config.botToken; const prefix = client.config.prefix;


    @see
    discordClientOptions?: ClientOptions

    Tùy chọn cấu hình nâng cao cho Discord.js Client.

    Thuộc tính này cho phép bạn truyền các options trực tiếp tới DiscordClient của discord.js khi khởi tạo client.

    Sử dụng khi bạn muốn:

    • Thay đổi intents mặc định
    • Cấu hình presence, partials
    • Điều chỉnh các option nâng cao khác của DiscordClient

    • Nếu không cung cấp, client sẽ sử dụng mặc định của blackcat.js-discord
    • Các thay đổi tại đây ảnh hưởng trực tiếp tới behavior của DiscordClient

    const client = new Client({
    discordClientOptions: {
    intents: ["Guilds", "GuildMessages", "MessageContent"],
    partials: ["CHANNEL"]
    }
    });
    eventManager?: EventManager

    Quản lý và tự động load các event cho ứng dụng

    Tùy chọn này cho phép bạn cấu hình một instance của EventManager để tự động quét, import và đăng ký các event từ thư mục chỉ định.

    undefined
    

    Khi được cung cấp, EventManager sẽ:

    1. Duyệt qua thư mục directory
    2. Tự động import các file event
    3. Đăng ký event vào các scope tương ứng (ví dụ: client, guild, ...)
    4. Gọi callback onLoad hoặc onError tùy theo trạng thái load

    Các tùy chọn quan trọng:

    • directory: Đường dẫn tới thư mục chứa các file event
    • scopes: Phạm vi áp dụng event (ví dụ: "client")
    • onLoad: Callback khi load event thành công
    • onError: Callback khi xảy ra lỗi khi load event

    Cơ chế hoạt động:

    1. Khi khởi tạo hệ thống
    2. EventManager sẽ quét toàn bộ file trong directory
    3. Mỗi file sẽ được import và validate cấu trúc event
    4. Nếu hợp lệ → đăng ký và gọi onLoad
    5. Nếu lỗi → gọi onError với thông tin chi tiết

    Phù hợp khi:

    • Muốn tách riêng logic event thành nhiều file
    • Dự án có nhiều event cần quản lý
    • Cần auto-load thay vì đăng ký thủ công

    Không nên dùng khi:

    • Dự án nhỏ, ít event
    • Muốn kiểm soát đăng ký event một cách thủ công
    import { Client, EventManager } from "blackcat.js-discord";

    const client = new Client({
    eventManager: new EventManager({
    directory: "./events",
    scopes: ["client"],
    onLoad: (event) => {
    console.log(`${event.file} - ok`);
    },
    onError: (event) => {
    console.error(`${event.file}:`, event.error);
    }
    })
    });
    // Không sử dụng EventManager (tự đăng ký thủ công)
    const client = new Client({
    // eventManager: undefined
    });

    EventManager

    slashCommandManager?: SlashCommandManager

    Quản lý và tự động load các slash command (application commands)

    Tùy chọn này cho phép bạn cấu hình một instance của SlashCommandManager để tự động quét, import và đăng ký các slash command lên Discord API.

    undefined
    

    Khi được cung cấp, SlashCommandManager sẽ:

    1. Duyệt qua thư mục directory
    2. Tự động import các file slash command
    3. Validate cấu trúc command (theo SlashCommandBuilder)
    4. (Tuỳ chọn) reset commands trước khi đăng ký
    5. Đăng ký commands lên Discord (global hoặc guild)
    6. Gọi callback onLoad hoặc onError tùy theo trạng thái

    Các tùy chọn quan trọng:

    • directory: Thư mục chứa các file slash command
    • guildID: Nếu có → đăng ký dạng guild command (deploy nhanh)
    • reset: Xóa command cũ trước khi đăng ký lại
    • onLoad: Callback khi load thành công
    • onError: Callback khi có lỗi

    Cơ chế hoạt động:

    1. Khi khởi tạo hệ thống
    2. SlashCommandManager quét toàn bộ file trong directory
    3. Import và validate từng command
    4. Nếu bật reset:
      • reset.global → xóa toàn bộ global commands
      • reset.guild → xóa toàn bộ guild commands (cần guildID)
    5. Đăng ký lại commands lên Discord API
    6. Gọi onLoad cho từng command thành công
    7. Nếu lỗi → gọi onError

    Lưu ý quan trọng:

    • Guild commands cập nhật gần như ngay lập tức
    • Global commands có thể mất vài phút để propagate
    • reset nên dùng khi thay đổi cấu trúc command lớn

    Phù hợp khi:

    • Sử dụng slash commands (interaction-based)
    • Cần auto-deploy commands khi khởi động bot
    • Dự án có nhiều command cần quản lý theo file

    Không nên dùng khi:

    • Muốn tự deploy command thủ công qua REST riêng
    • Dự án rất nhỏ (ít command)
    const client = new Client({
    slashCommandManager: new SlashCommandManager({
    directory: "./slashCommands",
    guildID: "1055150050357022840",
    reset: {
    guild: true
    },
    onLoad: (command) => {
    console.log(`slashCommands: ${command.commandName} - ok`);
    },
    onError: (command) => {
    console.error(`${command.file}:`, command.error);
    }
    })
    });
    // Đăng ký global commands (không cần guildID)
    const client = new Client({
    slashCommandManager: new SlashCommandManager({
    directory: "./slashCommands"
    })
    });
    // Reset toàn bộ commands trước khi đăng ký lại
    const client = new Client({
    slashCommandManager: new SlashCommandManager({
    directory: "./slashCommands",
    reset: {
    global: true
    }
    })
    });

    SlashCommandManager