From 418c5085492e4138c7ec09a100dd8e318a7c6592 Mon Sep 17 00:00:00 2001 From: MatthieuCoder Date: Tue, 3 Jan 2023 14:28:40 +0400 Subject: [PATCH] add a bit of documentation --- src/handler/builder.ts | 3 +++ src/handler/index.ts | 19 ++++++++++++++++--- src/index.ts | 42 ++++++------------------------------------ src/register.ts | 3 +++ src/rest.ts | 3 +++ 5 files changed, 31 insertions(+), 39 deletions(-) diff --git a/src/handler/builder.ts b/src/handler/builder.ts index 148542a..feb5780 100644 --- a/src/handler/builder.ts +++ b/src/handler/builder.ts @@ -1,6 +1,9 @@ import { SlashCommandBuilder } from "@discordjs/builders"; import { Command, HandlerFn } from "."; +/** + * Simple wrapper around the SlashCommandBuilder provided by Discord.js + */ export class CommandBuilder extends SlashCommandBuilder { private _handler: HandlerFn; diff --git a/src/handler/index.ts b/src/handler/index.ts index 8126717..16a56ed 100644 --- a/src/handler/index.ts +++ b/src/handler/index.ts @@ -6,22 +6,30 @@ import { InteractionType, RESTPostAPIApplicationCommandsJSONBody, RESTPostAPIChatInputApplicationCommandsJSONBody, - RESTPostAPIWebhookWithTokenJSONBody, Routes, } from "discord-api-types/v10"; -import { rest } from "../rest"; export * from "./builder"; +export type PromiseLike = T | Promise; +/** + * A simple function that executes a slash command. + */ export type HandlerFn = ( data: APIApplicationCommandInteraction ) => PromiseLike; -export type PromiseLike = T | Promise; + export type Command = { json: RESTPostAPIChatInputApplicationCommandsJSONBody; handler: HandlerFn; }; +/** + * Register all the commands to discord + * @param commands List of commands to register + * @param rest Rest api instance + * @param applicationId Current application id + */ export const registerCommands = async ( commands: Iterable, rest: REST, @@ -34,6 +42,11 @@ export const registerCommands = async ( } }; +/** + * Creates a new handler to handle the slash commands. + * @param commands List of commands to handle + * @returns Handler function + */ export const buildHandler = (commands: Iterable) => { let internal: Map = new Map(); for (const command of commands) { diff --git a/src/index.ts b/src/index.ts index eb8707f..7e5a5db 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,46 +1,16 @@ import { EventClient } from "./events/index"; -import { - RESTPostAPIChannelMessageResult, - Routes, -} from "discord-api-types/v10"; -import { rest } from "./rest"; import { buildHandler } from "./handler"; import { commands } from "./commands"; +/** + * We instanciate our nova broken client. + */ const emitter = new EventClient(); -emitter.on("interactionCreate", buildHandler(commands)); - -emitter.on("messageCreate", async (message) => { - console.log(message); - if (message.content.toLowerCase() == "salut") { - await rest.post(Routes.channelMessages(message.channel_id), { - body: { - content: `Salut <@${message.author.id}> :wink:`, - }, - }); - } else if (message.content.toLocaleLowerCase() == "~ping") { - let t1 = new Date().getTime(); - let sentMessage = await rest.post( - Routes.channelMessages(message.channel_id), - { - body: { - content: `Calcul du ping...`, - }, - } - ); - let time = new Date().getTime() - t1; - await rest.patch( - Routes.channelMessage(message.channel_id, sentMessage.id), - { - body: { - content: `Le ping de <@${sentMessage.author.id}> est de \`${time}ms\``, - }, - } - ); - } -}); +// We register our slash command handler. +emitter.on("interactionCreate", buildHandler(commands)); +// We connect ourselves to the nova nats broker. emitter .start({ additionalEvents: [], diff --git a/src/register.ts b/src/register.ts index 4672585..af01e76 100644 --- a/src/register.ts +++ b/src/register.ts @@ -2,4 +2,7 @@ import { commands } from "./commands"; import { registerCommands } from "./handler"; import { rest } from "./rest"; +/** + * We register the commands with discord + */ registerCommands(commands, rest, "807188335717384212"); \ No newline at end of file diff --git a/src/rest.ts b/src/rest.ts index 9bee0cb..ab9ff54 100644 --- a/src/rest.ts +++ b/src/rest.ts @@ -1,6 +1,9 @@ require('source-map-support').install(); import { REST } from "@discordjs/rest"; +/** + * Rest client used to communicate with discord + */ export const rest = new REST({ version: "10", headers: { Authorization: "" }, -- 2.39.5