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;
InteractionType,
RESTPostAPIApplicationCommandsJSONBody,
RESTPostAPIChatInputApplicationCommandsJSONBody,
- RESTPostAPIWebhookWithTokenJSONBody,
Routes,
} from "discord-api-types/v10";
-import { rest } from "../rest";
export * from "./builder";
+export type PromiseLike<T> = T | Promise<T>;
+/**
+ * A simple function that executes a slash command.
+ */
export type HandlerFn = (
data: APIApplicationCommandInteraction
) => PromiseLike<APIInteractionResponse>;
-export type PromiseLike<T> = T | Promise<T>;
+
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<Command>,
rest: REST,
}
};
+/**
+ * Creates a new handler to handle the slash commands.
+ * @param commands List of commands to handle
+ * @returns Handler function
+ */
export const buildHandler = (commands: Iterable<Command>) => {
let internal: Map<String, Command> = new Map();
for (const command of commands) {
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 = <RESTPostAPIChannelMessageResult>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: [],
import { registerCommands } from "./handler";
import { rest } from "./rest";
+/**
+ * We register the commands with discord
+ */
registerCommands(commands, rest, "807188335717384212");
\ No newline at end of file
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: "" },