Add a command to a Discord bot

https://discord.com/developers/docs/interactions/application-commands#making-a-global-command https://www.windmill.dev/blog/knowledge-base-discord-bot

Script discord

by henri186 ยท 6/27/2023

  • Submitted by henri186 Deno
    Created 1055 days ago
    1
    import { SlashCommandBuilder } from "npm:@discordjs/[email protected]";
    2
    
    
    3
    export async function main(
    4
      applicationId: string,
    5
      botToken: string,
    6
      commandName: string = "windmill-help",
    7
      commandDescription: string = "Ask a question about Windmill",
    8
      optionDescription: string = "Ask a question about Windmill"
    9
    ) {
    10
      const command = new SlashCommandBuilder()
    11
        .setName(commandName)
    12
        .setDescription(commandDescription)
    13
        .addStringOption((option) =>
    14
          option.setName("question").setDescription(optionDescription)
    15
        );
    16
    
    
    17
      const url = `https://discord.com/api/v10/applications/${applicationId}/commands`;
    18
    
    
    19
      await fetch(url, {
    20
        method: "POST",
    21
        headers: {
    22
          Authorization: `Bot ${botToken}`,
    23
          "Content-Type": "application/json",
    24
        },
    25
        body: JSON.stringify(command.toJSON()),
    26
      });
    27
    }
    28