From ceaa0dd803ddb8b3d77ac093971ce168b9e58c41 Mon Sep 17 00:00:00 2001 From: GRZ4NA Date: Sat, 7 Aug 2021 02:26:19 +0200 Subject: [PATCH] slash register test --- src/Bot.ts | 9 ++++++++- src/Command.ts | 11 +++++++++++ src/Help.ts | 2 ++ src/Parameter.ts | 5 +++-- src/types.ts | 4 ++++ 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Bot.ts b/src/Bot.ts index cad8c67..cfdb115 100644 --- a/src/Bot.ts +++ b/src/Bot.ts @@ -11,6 +11,7 @@ import { HelpMessage } from "./Help.js"; import * as http from "http"; import { SystemMessageManager } from "./SystemMessage.js"; import { EventEmitter } from "events"; +import axios from "axios"; export declare interface Bot { on(event: "READY", listener: Function): this; @@ -103,7 +104,13 @@ export class Bot extends EventEmitter { this.client.on("ready", async () => { console.log("✔"); process.stdout.write("Registering commands... "); - + this.commands.list.map(async (c) => { + await axios.post( + `https://discord.com/api/v8/applications/${this.applicationId}/commands`, + c.toCommandObject(), + { headers: { Authorization: `Bot ${this.token}` } } + ); + }); console.log("✔\n"); this.emit("READY"); }); diff --git a/src/Command.ts b/src/Command.ts index 8cf9d85..fbcb3b3 100644 --- a/src/Command.ts +++ b/src/Command.ts @@ -54,6 +54,9 @@ export class Command { this.guilds = options.guilds || []; this.visible = options.visible != undefined ? options.visible : true; this.function = options.function; + /*if (!new RegExp("^[w-]{1,32}$").test(this.name)) { + throw new Error(`Incorrect command name: ${this.name}`); + }*/ } /** @@ -94,6 +97,14 @@ export class Command { } } + toCommandObject() { + return { + name: this.name, + description: this.description, + options: [], + }; + } + private static processPhrase(phrase?: string | string[]): string[] { if (Array.isArray(phrase)) { const buff = phrase.map((p) => { diff --git a/src/Help.ts b/src/Help.ts index 57dae6a..b315a94 100644 --- a/src/Help.ts +++ b/src/Help.ts @@ -17,6 +17,8 @@ export class HelpMessage extends Command { parameters: [ { name: "Command name", + description: + "Name of the command that you want to get details about", optional: true, type: "string", }, diff --git a/src/Parameter.ts b/src/Parameter.ts index d8a2646..8f649ca 100644 --- a/src/Parameter.ts +++ b/src/Parameter.ts @@ -1,9 +1,10 @@ -import { ParameterTypeError } from "./errors.js"; -import { ParameterType, ParameterResolvable } from "./types.js"; +import { ParameterType, ParameterResolvable, Choice } from "./types.js"; export interface Parameter { name: string; + description?: string; optional: boolean; type: ParameterType; + choices?: Choice[]; } export const ProcessArgument = ( a: string, diff --git a/src/types.ts b/src/types.ts index 2d59399..8086ade 100644 --- a/src/types.ts +++ b/src/types.ts @@ -71,3 +71,7 @@ export type ParameterResolvable = | TextChannel | VoiceChannel | NewsChannel; +export interface Choice { + name: string; + value: string; +}