OptionalcommandQuả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.
Khi được cung cấp, CommandManager sẽ:
directoryprefix để nhận diện commandonLoad hoặc onError tùy theo trạng thái loadCác tùy chọn quan trọng:
directory: Đường dẫn tới thư mục chứa các file commandprefix: Tiền tố dùng để kích hoạt command (ví dụ: "!")onLoad: Callback khi load command thành côngonError: Callback khi xảy ra lỗi khi load commandCơ chế hoạt động:
CommandManager sẽ quét toàn bộ file trong directoryonLoadonError với thông tin chi tiếtprefix hay khôngPhù hợp khi:
Không nên dùng khi:
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);
}
})
});
OptionalconfigCấ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.
Khi được cung cấp, config sẽ:
botToken để đăng nhập DiscordCấ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 customCơ chế hoạt động:
botTokenclient.loginconfigPhù hợp khi:
Không nên dùng khi:
OptionaldiscordTù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:
OptionaleventQuả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.
Khi được cung cấp, EventManager sẽ:
directoryonLoad hoặc onError tùy theo trạng thái loadCác tùy chọn quan trọng:
directory: Đường dẫn tới thư mục chứa các file eventscopes: Phạm vi áp dụng event (ví dụ: "client")onLoad: Callback khi load event thành côngonError: Callback khi xảy ra lỗi khi load eventCơ chế hoạt động:
EventManager sẽ quét toàn bộ file trong directoryonLoadonError với thông tin chi tiếtPhù hợp khi:
Không nên dùng khi:
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);
}
})
});
OptionalslashQuả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.
Khi được cung cấp, SlashCommandManager sẽ:
directorySlashCommandBuilder)onLoad hoặc onError tùy theo trạng tháiCác tùy chọn quan trọng:
directory: Thư mục chứa các file slash commandguildID: Nếu có → đăng ký dạng guild command (deploy nhanh)reset: Xóa command cũ trước khi đăng ký lạionLoad: Callback khi load thành côngonError: Callback khi có lỗiCơ chế hoạt động:
SlashCommandManager quét toàn bộ file trong directoryreset:
reset.global → xóa toàn bộ global commandsreset.guild → xóa toàn bộ guild commands (cần guildID)onLoad cho từng command thành côngonErrorLưu ý quan trọng:
reset nên dùng khi thay đổi cấu trúc command lớnPhù hợp khi:
Không nên dùng khi:
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"
})
});
Options khởi tạo Client.