This commit is contained in:
Marius Begby committed 2023-08-30 22:42:40 +02:00
1 parent 3be362f096
commit 6d7a931f16
6 files changed
+28 -43

No files matched your search

+5 -2
View File
@@ -1,8 +1,9 @@
import { ChatInputCommandInteraction } from 'discord.js'; import { ChatInputCommandInteraction } from 'discord.js';
import { Logger } from 'pino'; import { Logger } from 'pino';
import loggerModule from '../services/logger'; import loggerModule from '../services/logger';
import { Command, ExtendedClient } from '../types/clientTypes'; import { ExtendedClient } from '../types/clientTypes';
import { cannotSendMessageInChannel } from '../utils/validation/permissionValidator'; import { cannotSendMessageInChannel } from '../utils/validation/permissionValidator';
import { BaseSlashCommandInteraction } from '../types/interactionTypes';
export const handleCommand = async ( export const handleCommand = async (
interaction: ChatInputCommandInteraction, interaction: ChatInputCommandInteraction,
@@ -20,7 +21,9 @@ export const handleCommand = async (
logger.debug('Interaction deferred.'); logger.debug('Interaction deferred.');
// TODO: Update TS Type for command // TODO: Update TS Type for command
const command: Command = client.commands?.get(interaction.commandName) as Command; const command: BaseSlashCommandInteraction = client.commands!.get(
interaction.commandName
) as BaseSlashCommandInteraction;
if (!command) { if (!command) {
logger.warn(`Interaction created but command '${interaction.commandName}' was not found.`); logger.warn(`Interaction created but command '${interaction.commandName}' was not found.`);
return; return;
+15 -5
View File
@@ -1,5 +1,11 @@
import config from 'config'; import config from 'config';
import { EmbedBuilder, SlashCommandBuilder } from 'discord.js'; import {
EmbedBuilder,
SharedSlashCommandOptions,
SlashCommandBuilder,
SlashCommandNumberOption,
SlashCommandStringOption
} from 'discord.js';
import { Logger } from 'pino'; import { Logger } from 'pino';
import loggerModule from '../../../services/logger'; import loggerModule from '../../../services/logger';
import { BotOptions, EmbedOptions } from '../../../types/configTypes'; import { BotOptions, EmbedOptions } from '../../../types/configTypes';
@@ -27,16 +33,20 @@ const command: BaseSlashCommandInteraction = {
}); });
// TODO: Create interface for command list // TODO: Create interface for command list
const commandList = client!.commands! const commandList = client!
.filter((command) => { .commands!.filter((command: BaseSlashCommandInteraction) => {
// don't include system commands // don't include system commands
if (command.isSystemCommand) { if (command.isSystemCommand) {
return false; return false;
} }
return true; return true;
}) })
.map((command) => { .map((command: BaseSlashCommandInteraction) => {
const params: string = command.data.options[0] ? `**\`${command.data.options[0].name}\`**` + ' ' : ''; let params: string = '';
const option = command.data.options[0];
if (option instanceof SlashCommandNumberOption || option instanceof SlashCommandStringOption) {
params = `**\`${option.name}\`**` + ' ';
}
const beta: string = command.isBeta ? `${embedOptions.icons.beta} ` : ''; const beta: string = command.isBeta ? `${embedOptions.icons.beta} ` : '';
const newCommand: string = command.isNew ? `${embedOptions.icons.new} ` : ''; const newCommand: string = command.isNew ? `${embedOptions.icons.new} ` : '';
return `- **\`/${command.data.name}\`** ${params}- ${beta}${newCommand}${command.data.description}`; return `- **\`/${command.data.name}\`** ${params}- ${beta}${newCommand}${command.data.description}`;
+2 -2
View File
@@ -1,7 +1,7 @@
import { EmbedBuilder, Message, SlashCommandBuilder } from 'discord.js'; import { EmbedBuilder, Message, SlashCommandBuilder } from 'discord.js';
import { BaseCommandInteraction, BaseSlashCommandParams } from '../../../types/interactionTypes'; import { BaseSlashCommandInteraction, BaseSlashCommandParams } from '../../../types/interactionTypes';
export class TestCommandInteraction extends BaseCommandInteraction { export class TestCommandInteraction extends BaseSlashCommandInteraction {
constructor() { constructor() {
const data = new SlashCommandBuilder().setName('test').setDescription('Test command'); const data = new SlashCommandBuilder().setName('test').setDescription('Test command');
super(data); super(data);
+2 -17
View File
@@ -1,24 +1,9 @@
import { Client, Collection } from 'discord.js'; import { Client, Collection } from 'discord.js';
import { BaseCommandInteraction } from './interactionTypes'; import { BaseSlashCommandInteraction } from './interactionTypes';
type RegisterClientCommandsFunction = (params: { client: Client; executionId: string }) => void; type RegisterClientCommandsFunction = (params: { client: Client; executionId: string }) => void;
/*
export interface Command {
isNew?: boolean;
isBeta?: boolean;
isSystemCommand?: boolean;
data: {
name: string;
description: string;
options: SharedNameAndDescription[]
};
execute: (params: { interaction: object; client: ExtendedClient | undefined; executionId: string }) => void;
autocomplete?: (params: { interaction: object; executionId: string }) => void;
}
*/
export interface ExtendedClient extends Client { export interface ExtendedClient extends Client {
registerClientCommands?: RegisterClientCommandsFunction; registerClientCommands?: RegisterClientCommandsFunction;
commands?: Collection<string, BaseCommandInteraction>; commands?: Collection<string, BaseSlashCommandInteraction>;
} }
+3 -10
View File
@@ -22,23 +22,16 @@ export interface BaseSlashCommandParams extends BaseInteractionParams {
client?: ExtendedClient; client?: ExtendedClient;
} }
interface BaseAutocompleteParams { interface BaseAutocompleteParams extends BaseInteractionParams {
interaction: AutocompleteInteraction; interaction: AutocompleteInteraction;
} }
interface BaseComponentParams { interface BaseComponentParams extends BaseInteractionParams {
interaction: MessageComponentInteraction; interaction: MessageComponentInteraction;
referenceId?: string; referenceId?: string;
} }
// Interaction types // Interaction types
export interface BaseSlashCommandInteraction {
isNew: boolean;
isBeta: boolean;
isSystemCommand?: boolean;
data: Omit<SlashCommandBuilder, 'addSubcommand' | 'addSubcommandGroup'>;
execute(params: BaseSlashCommandParams): Promise<Message<boolean> | void>;
}
export interface BaseAutocompleteInteraction { export interface BaseAutocompleteInteraction {
execute(params: BaseAutocompleteParams): Promise<ApplicationCommandOptionChoiceData | void>; execute(params: BaseAutocompleteParams): Promise<ApplicationCommandOptionChoiceData | void>;
@@ -87,7 +80,7 @@ abstract class BaseInteraction {
): Promise<Message<boolean> | ApplicationCommandOptionChoiceData | void>; ): Promise<Message<boolean> | ApplicationCommandOptionChoiceData | void>;
} }
export abstract class BaseCommandInteraction extends BaseInteraction { export abstract class BaseSlashCommandInteraction extends BaseInteraction {
data: Omit<SlashCommandBuilder, 'addSubcommand' | 'addSubcommandGroup'>; data: Omit<SlashCommandBuilder, 'addSubcommand' | 'addSubcommandGroup'>;
isSystemCommand: boolean; isSystemCommand: boolean;
isNew: boolean; isNew: boolean;
+1 -7
View File
@@ -1,13 +1,7 @@
import 'dotenv/config'; import 'dotenv/config';
import config from 'config'; import config from 'config';
import { import { REST, RESTPostAPIChatInputApplicationCommandsJSONBody, RouteLike, Routes } from 'discord.js';
REST,
RESTPostAPIChatInputApplicationCommandsJSONBody,
RouteLike,
Routes,
SlashCommandBuilder
} from 'discord.js';
import fs from 'node:fs'; import fs from 'node:fs';
import path from 'node:path'; import path from 'node:path';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';