From 83cf65c3b877367e168d22e3ccfed3ba5feaff1f Mon Sep 17 00:00:00 2001 From: GRZ4NA Date: Mon, 21 Jun 2021 18:13:05 +0200 Subject: [PATCH] System messages --- src/Bot.ts | 3 -- src/Command.ts | 2 +- src/SystemMessage.ts | 110 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 src/SystemMessage.ts diff --git a/src/Bot.ts b/src/Bot.ts index 33d3c29..ca25ed3 100644 --- a/src/Bot.ts +++ b/src/Bot.ts @@ -7,7 +7,6 @@ import * as http from 'http'; //TYPE DEFINITIONS interface ConfigurationOptions { prefix: string, - accentColor: string, token?: string } interface ConstructorOptions { @@ -16,7 +15,6 @@ interface ConstructorOptions { helpCommand?: boolean, clientOptions?: ClientOptions, token?: string, - accentColor?: string } //MAIN CLASS @@ -39,7 +37,6 @@ class Bot { this.commands = new CommandsManager(); this.config = { prefix: options.prefix, - accentColor: "#000", token: options.token } this.messages = { diff --git a/src/Command.ts b/src/Command.ts index f9c3097..5597980 100644 --- a/src/Command.ts +++ b/src/Command.ts @@ -151,4 +151,4 @@ class CommandsManager { } //EXPORTS -export { Command, CommandsManager, CommandMessageStructure } \ No newline at end of file +export { Command, CommandsManager, CommandMessageStructure, PermissionsError } \ No newline at end of file diff --git a/src/SystemMessage.ts b/src/SystemMessage.ts new file mode 100644 index 0000000..75ffc85 --- /dev/null +++ b/src/SystemMessage.ts @@ -0,0 +1,110 @@ +import { ColorResolvable, GuildMember, MessageEmbed, TextChannel, User } from 'discord.js'; +import { Command, PermissionsError } from './Command.js'; + +type MessageType = 'PERMISSION' | 'ERROR' | 'NOT_FOUND'; +interface SystemMessageAppearance { + title: string, + bottomText?: string, + accentColor?: ColorResolvable, + showTimestamp?: boolean, + footer?: string +} +interface SystemMessageData { + command?: Command, + phrase?: string, + user?: GuildMember | User, + error?: Error | PermissionsError | string +} + +class SystemMessageManager { + PERMISSION: SystemMessageAppearance; + ERROR: SystemMessageAppearance; + NOT_FOUND: SystemMessageAppearance; + deleteTimeout: number; + constructor(botName?: string) { + this.PERMISSION = { + title: '👮‍♂️ Insufficient permissions', + bottomText: "You don't have enough permissions to run this command", + accentColor: '#1d1dc4', + showTimestamp: true + } + this.ERROR = { + title: '❌ An error occurred', + bottomText: 'Something went wrong while processing your request.', + accentColor: '#f00', + showTimestamp: true + } + this.NOT_FOUND = { + title: '🔍 Command not found', + accentColor: '#ff5500', + showTimestamp: false, + footer: botName + } + this.deleteTimeout = Infinity; + } + async send(type: MessageType, data?: SystemMessageData, channel?: TextChannel) : Promise { + if(this[type]) { + const embed = new MessageEmbed(); + embed.setTitle(this[type].title); + if(this[type].bottomText) embed.setDescription(this[type].bottomText); + embed.setColor(this[type].accentColor || '#000'); + if(this[type].showTimestamp) embed.setTimestamp(); + if(this[type].footer) embed.setFooter(this[type].footer); + if(data) { + switch(type) { + case 'ERROR': + if(data.command) { + embed.addField('Command name:', data.command.name, false); + } + if(data.error) { + if(data.error instanceof Error || data.error instanceof PermissionsError) { + embed.addField('Error details:', data.error.toString(), false); + } + else if(typeof data.error == 'string') { + embed.addField('Error details:', data.error, false); + } + } + if(data.user) { + embed.addField('User:', data.user.toString(), false); + } + break; + case 'NOT_FOUND': + if(data.phrase) { + embed.addField('Phrase:', data.phrase, false); + } + break; + case 'PERMISSION': + if(data.user) { + embed.addField('User:', data.user.toString(), false); + } + if(data.command) { + embed.addField('Command name:', data.command.name, false); + if(data.command.permissions) { + let permList: string = ''; + data.command.permissions.toArray(false).map(p => { + permList += p + '\n'; + }); + embed.addField('Required permissions:', permList, false); + } + } + break; + } + } + if(channel) { + const message = await channel.send(embed); + if(this.deleteTimeout != Infinity && message.deletable) { + await message.delete({ timeout: this.deleteTimeout }); + } + } + else { + return embed; + } + } + else { + console.error('ERROR! System message: Incorrect parameter'); + return; + } + } +} + +export { MessageType, SystemMessageAppearance, SystemMessageManager }; \ No newline at end of file