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

    Class SlashCommandBuilder<TOptions, TSubs, TGroups>

    Builder chính dùng để định nghĩa một Slash Command.

    Class này chịu trách nhiệm:

    • Khai báo metadata của command
    • Lưu root options
    • Lưu subcommands
    • Lưu subcommand groups
    • Định nghĩa hàm execute xử lý interaction

    Đây là class trung tâm của hệ thống slash command trong framework.

    Command đơn giản

    const pingCommand = new SlashCommandBuilder({
    commandName: "ping",
    description: "Kiểm tra bot",

    execute: async (client, interaction) => {
    await interaction.reply("Pong!");
    }
    });

    Command có options

    const avatarCommand = new SlashCommandBuilder({
    commandName: "avatar",
    description: "Xem avatar user",

    options: (opt) => ({
    user: opt.user({
    description: "User cần xem avatar",
    required: false
    })
    }),

    execute: async (client, interaction, options) => {
    const user = options.user ?? interaction.user;
    await interaction.reply(user.displayAvatarURL());
    }
    });

    Command có subcommand

    const musicCommand = new SlashCommandBuilder({
    commandName: "music",
    description: "Lệnh nhạc",

    subcommands: {
    play: new SlashSubCommandBuilder({
    description: "Phát nhạc",
    options: (opt) => ({
    query: opt.string({
    description: "Tên bài hát",
    required: true
    })
    })
    }),

    stop: new SlashSubCommandBuilder({
    description: "Dừng nhạc"
    })
    },

    execute: async (client, interaction, options) => {

    if (options.payload?.type === "sub") {

    if (options.payload.sub === "play") {
    const { query } = options.payload.options;
    await interaction.reply(`Playing: ${query}`);
    }

    if (options.payload.sub === "stop") {
    await interaction.reply("Stopped music.");
    }

    }
    }
    });

    Command có subcommand group

    Cấu trúc lệnh:

    /admin user ban
    /admin user kick
    /admin role add
    /admin role remove
    const adminCommand = new SlashCommandBuilder({
    commandName: "admin",
    description: "Lệnh quản trị",

    groups: {

    user: new SlashSubCommandGroupBuilder({
    description: "Quản lý user",

    subcommands: {

    ban: new SlashSubCommandBuilder({
    description: "Ban user",
    options: (opt) => ({
    target: opt.user({
    description: "User cần ban",
    required: true
    }),

    reason: opt.string({
    description: "Lý do ban",
    required: false
    })
    })
    }),

    kick: new SlashSubCommandBuilder({
    description: "Kick user",
    options: (opt) => ({
    target: opt.user({
    description: "User cần kick",
    required: true
    })
    })
    })

    }
    }),

    role: new SlashSubCommandGroupBuilder({
    description: "Quản lý role",

    subcommands: {

    add: new SlashSubCommandBuilder({
    description: "Thêm role",
    options: (opt) => ({
    user: opt.user({
    description: "User",
    required: true
    }),

    role: opt.role({
    description: "Role",
    required: true
    })
    })
    }),

    remove: new SlashSubCommandBuilder({
    description: "Gỡ role",
    options: (opt) => ({
    user: opt.user({
    description: "User",
    required: true
    }),

    role: opt.role({
    description: "Role",
    required: true
    })
    })
    })

    }
    })

    },

    async execute(client, interaction, options) {

    if (options.payload?.type !== "group") return;

    const { group, sub, options: opts } = options.payload;

    if (group === "user" && sub === "ban") {
    await interaction.reply(`Ban ${opts.target?.tag}`);
    }

    if (group === "user" && sub === "kick") {
    await interaction.reply(`Kick ${opts.target?.tag}`);
    }

    }
    });

    Type Parameters

    • TOptions extends Record<string, any> | undefined = undefined

      kiểu options gốc của command

    • TSubs extends Record<string, ISlashSubCommandBuilder<any>> = {}

      danh sách subcommand trực tiếp

    • TGroups extends Record<string, ISlashSubCommandGroup<any>> = {}

      danh sách subcommand group

    Index

    Constructors

    Properties

    commandName: string

    Tên slash command.

    Ví dụ: /ping /admin

    cooldown?: CooldownConfig<"interaction">

    Cấu hình cooldown để tránh spam command.

    description: string

    Mô tả command hiển thị trong Discord.

    userPermission?: SlashPermissionConfig

    Cấu hình quyền người dùng cần có để sử dụng command.

    Accessors

    • get options(): TOptions | undefined

      Lấy options đã được build.

      Returns TOptions | undefined

    Methods

    • Build Discord SlashCommandBuilder để đăng ký với Discord API.

      Method này:

      1. Kiểm tra command name và description
      2. Build root options
      3. Build subcommands
      4. Build subcommand groups

      Returns SlashCommandBuilder

      DiscordSlashCommandBuilder

      Error nếu thiếu command name hoặc description

      const data = command.build();

      await rest.put(
      Routes.applicationCommands(clientId),
      { body: [data.toJSON()] }
      );
    • Set function xử lý khi command được execute.

      Parameters

      • handler: (
            client: Client,
            interaction: ChatInputCommandInteraction,
            options: ExecuteOptions<TOptions, TSubs, TGroups>,
        ) => any

        function handler

          • (
                client: Client,
                interaction: ChatInputCommandInteraction,
                options: ExecuteOptions<TOptions, TSubs, TGroups>,
            ): any
          • Hàm thực thi khi slash command được gọi.

            Parameters

            • client: Client

              Discord Client

            • interaction: ChatInputCommandInteraction

              interaction của slash command

            • options: ExecuteOptions<TOptions, TSubs, TGroups>

              object đã được parse và type-safe

              options bao gồm:

              • root options
              • payload subcommand hoặc group

              Ví dụ payload:

              options.payload
              

              có thể là:

              { type: "sub", sub: "ping", options: {} }
              

              hoặc

              {
              type: "group",
              group: "admin",
              sub: "ban",
              options: { user: User }
              }

            Returns any

      Returns SlashCommandBuilder<TOptions, TSubs, TGroups>

      .setExecute(async (client, interaction, options) => {
      await interaction.reply("Hello");
      })
    • Định nghĩa options cho command.

      Type Parameters

      • T extends Record<string, any>

      Parameters

      • options: (opt: OptionFactory) => T

        function tạo options

      Returns SlashCommandBuilder<T, TSubs, TGroups>

      .setOptions((opt) => ({
      user: opt.user({
      description: "Target user",
      required: true
      }),
      reason: opt.string({
      description: "Ban reason"
      })
      }))