working slash commands

This commit is contained in:
mmacierzanka committed 2021-08-07 16:50:53 +02:00
1 parent b1b981e3ac
commit 06d9a90057
5 files changed
+116 -27

No files matched your search

+38 -2
View File
@@ -1,4 +1,4 @@
import { Client, Intents, Message, TextChannel } from "discord.js"; import { Client, GuildMember, Intents, Message, TextChannel } from "discord.js";
import { Command } from "./Command.js"; import { Command } from "./Command.js";
import { CommandManager } from "./CommandManager.js"; import { CommandManager } from "./CommandManager.js";
import { import {
@@ -119,7 +119,7 @@ export class Bot extends EventEmitter {
this.emit("READY"); this.emit("READY");
}); });
this.client.on("message", async (m) => { this.client.on("message", async (m) => {
const cmdMsg = this.commands.fetch(m); const cmdMsg = this.commands.fetchFromMessage(m);
if (cmdMsg) { if (cmdMsg) {
this.emit("COMMAND", m, cmdMsg); this.emit("COMMAND", m, cmdMsg);
try { try {
@@ -160,6 +160,42 @@ export class Bot extends EventEmitter {
this.emit("MESSAGE", m); this.emit("MESSAGE", m);
} }
}); });
this.client.on("interactionCreate", async (i) => {
if (!i.isCommand()) return;
const cmd = this.commands.fetchFromInteraction(i);
if (cmd) {
this.emit("COMMAND", i, cmd);
try {
await cmd.command.start(i, cmd.parameters);
} catch (e) {
if (e instanceof PermissionsError) {
this.messages.system.send(
"PERMISSION",
{
user:
(i.member as GuildMember) || undefined,
command: cmd.command,
},
i
);
} else {
this.messages.system.send(
"ERROR",
{
command: cmd.command,
user:
(i.member as GuildMember) || undefined,
error: e,
},
i
);
console.error(e);
}
this.emit("ERROR", e);
return;
}
}
});
return true; return true;
} catch (e) { } catch (e) {
console.log("❌"); console.log("❌");
+45 -8
View File
@@ -1,5 +1,12 @@
//IMPORTS //IMPORTS
import { Permissions, Message, MessageEmbed, Guild } from "discord.js"; import {
Permissions,
Message,
MessageEmbed,
Guild,
CommandInteraction,
GuildMember,
} from "discord.js";
import { import {
CommandBuilder, CommandBuilder,
PermissionCheckTypes, PermissionCheckTypes,
@@ -21,7 +28,7 @@ export class Command {
guilds?: Guild[]; guilds?: Guild[];
visible: boolean; visible: boolean;
private function: ( private function: (
message?: Message, interaction?: Message | CommandInteraction,
cmdParams?: ParameterResolvable[] cmdParams?: ParameterResolvable[]
) => void | string | MessageEmbed | Promise<void | string | MessageEmbed>; ) => void | string | MessageEmbed | Promise<void | string | MessageEmbed>;
@@ -72,9 +79,13 @@ export class Command {
* @param {string[]} [cmdParams] - list of processed parameters passed in a Discord message * @param {string[]} [cmdParams] - list of processed parameters passed in a Discord message
* @returns *Promise<void>* * @returns *Promise<void>*
*/ */
async start(message?: Message, cmdParams?: string[]): Promise<void> { async start(
interaction?: Message | CommandInteraction,
cmdParams?: ParameterResolvable[]
): Promise<void> {
if (interaction instanceof Message) {
const memberPermissions: Readonly<Permissions> = const memberPermissions: Readonly<Permissions> =
message?.member?.permissions || new Permissions(); interaction?.member?.permissions || new Permissions();
if ( if (
!this.permissions || !this.permissions ||
(this.permissionCheck == "ALL" (this.permissionCheck == "ALL"
@@ -94,14 +105,40 @@ export class Command {
} }
}); });
} }
const fnResult = await this.function(message, cmdParams); const fnResult = await this.function(interaction, cmdParams);
if (typeof fnResult == "string") { if (typeof fnResult == "string") {
await message?.reply(fnResult); await interaction?.reply(fnResult);
} else if (fnResult instanceof MessageEmbed) { } else if (fnResult instanceof MessageEmbed) {
await message?.channel.send({ embeds: [fnResult] }); await interaction?.channel.send({ embeds: [fnResult] });
} }
} else { } else {
throw new PermissionsError(this, message?.member); throw new PermissionsError(this, interaction?.member);
}
} else if (interaction instanceof CommandInteraction) {
const memberPermissions = interaction.member?.permissions;
if (memberPermissions instanceof Permissions) {
if (
!this.permissions ||
(this.permissionCheck === "ALL"
? memberPermissions.has(this.permissions, true)
: memberPermissions.any(this.permissions, true))
) {
const fnResult = await this.function(
interaction,
cmdParams
);
if (typeof fnResult == "string") {
await interaction.reply(fnResult);
} else if (fnResult instanceof MessageEmbed) {
await interaction.reply({ embeds: [fnResult] });
}
}
} else {
throw new PermissionsError(
this,
interaction.member as GuildMember
);
}
} }
} }
+19 -2
View File
@@ -1,4 +1,4 @@
import { Message } from "discord.js"; import { CommandInteraction, Message } from "discord.js";
import { Command } from "./Command.js"; import { Command } from "./Command.js";
import { import {
CommandMessageStructure, CommandMessageStructure,
@@ -121,7 +121,7 @@ export class CommandManager {
* @param {Message} message - *Message* object * @param {Message} message - *Message* object
* @returns *CommandMessagesStructure* | *null* * @returns *CommandMessagesStructure* | *null*
*/ */
fetch(message: Message): CommandMessageStructure | null { fetchFromMessage(message: Message): CommandMessageStructure | null {
if (!message.author.bot) { if (!message.author.bot) {
let prefix = false; let prefix = false;
if (message.content.startsWith(this.prefix)) { if (message.content.startsWith(this.prefix)) {
@@ -156,6 +156,23 @@ export class CommandManager {
return null; return null;
} }
} }
fetchFromInteraction(
interaction: CommandInteraction
): CommandMessageStructure | null {
const cmd = this.get(interaction.commandName);
if (cmd) {
if (interaction.options?.data) {
const params = interaction.options.data.map((o) => o.value);
return {
command: cmd,
parameters: params,
};
}
} else {
return null;
}
return null;
}
private findPhraseOccurrence(phrase?: string): PhraseOccurrenceData | null { private findPhraseOccurrence(phrase?: string): PhraseOccurrenceData | null {
let returnValue: PhraseOccurrenceData | null = null; let returnValue: PhraseOccurrenceData | null = null;
+10 -4
View File
@@ -1,5 +1,6 @@
import { import {
ColorResolvable, ColorResolvable,
CommandInteraction,
DMChannel, DMChannel,
GuildMember, GuildMember,
Message, Message,
@@ -64,13 +65,13 @@ export class SystemMessageManager {
* Generates and sends a system message * Generates and sends a system message
* @param {MessageType} type - 'ERROR' | 'PERMISSION' | 'NOT_FOUND' * @param {MessageType} type - 'ERROR' | 'PERMISSION' | 'NOT_FOUND'
* @param {SystemMessageData} [data] - additional data to include in the message * @param {SystemMessageData} [data] - additional data to include in the message
* @param {TextChannel | DMChannel | NewsChannel} [channel] - if specified, the generated message will be sent in this channel * @param {TextChannel | DMChannel | NewsChannel} [interaction] - if specified, the generated message will be sent in this channel
* @returns *Promise<MessageEmbed | Message | void>* * @returns *Promise<MessageEmbed | Message | void>*
*/ */
async send( async send(
type: MessageType, type: MessageType,
data?: SystemMessageData, data?: SystemMessageData,
channel?: TextChannel | DMChannel | NewsChannel interaction?: TextChannel | DMChannel | NewsChannel | CommandInteraction
): Promise<MessageEmbed | Message | void> { ): Promise<MessageEmbed | Message | void> {
if (this[type]) { if (this[type]) {
if (this[type].enabled === false) { if (this[type].enabled === false) {
@@ -152,14 +153,19 @@ export class SystemMessageManager {
break; break;
} }
} }
if (channel) { if (interaction && !(interaction instanceof CommandInteraction)) {
const message = await channel.send({ embeds: [embed] }); const message = await interaction.send({ embeds: [embed] });
if (this.deleteTimeout != Infinity && message.deletable) { if (this.deleteTimeout != Infinity && message.deletable) {
setTimeout(async () => { setTimeout(async () => {
await message.delete(); await message.delete();
}, this.deleteTimeout || 0); }, this.deleteTimeout || 0);
} }
return message; return message;
} else if (
interaction &&
interaction instanceof CommandInteraction
) {
interaction.reply({ embeds: [embed] });
} else { } else {
return embed; return embed;
} }
+4 -11
View File
@@ -1,6 +1,7 @@
import { import {
ClientOptions, ClientOptions,
ColorResolvable, ColorResolvable,
CommandInteraction,
Guild, Guild,
GuildMember, GuildMember,
NewsChannel, NewsChannel,
@@ -34,7 +35,7 @@ export interface CommandBuilder {
guilds?: Guild[]; guilds?: Guild[];
visible?: boolean; visible?: boolean;
function: ( function: (
message?: Message, interaction?: Message | CommandInteraction,
cmdParams?: ParameterResolvable[] cmdParams?: ParameterResolvable[]
) => void | string | MessageEmbed | Promise<void | string | MessageEmbed>; ) => void | string | MessageEmbed | Promise<void | string | MessageEmbed>;
} }
@@ -44,7 +45,7 @@ export interface PhraseOccurrenceData {
} }
export interface CommandMessageStructure { export interface CommandMessageStructure {
command: Command; command: Command;
parameters: string[]; parameters: ParameterResolvable[];
} }
export interface HelpMessageParams { export interface HelpMessageParams {
enabled: boolean; enabled: boolean;
@@ -62,15 +63,7 @@ export type ParameterType =
| "role" | "role"
| "channel" | "channel"
| "mentionable"; | "mentionable";
export type ParameterResolvable = export type ParameterResolvable = string | boolean | number | undefined;
| string
| boolean
| number
| GuildMember
| Role
| TextChannel
| VoiceChannel
| NewsChannel;
export interface Choice { export interface Choice {
name: string; name: string;
value: string; value: string;