From 15be754d3c698fdc20065fa79d01c55c09b66a49 Mon Sep 17 00:00:00 2001 From: GRZ4NA Date: Mon, 21 Jun 2021 18:22:15 +0200 Subject: [PATCH] system messages implementation --- src/Bot.ts | 19 +++++++++++++++---- src/Command.ts | 2 +- src/SystemMessage.ts | 4 ++-- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/Bot.ts b/src/Bot.ts index ca25ed3..6dfed1a 100644 --- a/src/Bot.ts +++ b/src/Bot.ts @@ -1,8 +1,9 @@ //IMPORTS import { Client, ClientOptions, Message } from 'discord.js'; -import { Command, CommandsManager, CommandMessageStructure } from './Command.js'; +import { Command, CommandsManager, CommandMessageStructure, PermissionsError } from './Command.js'; import { HelpMessage, HelpMessageParams } from './Help.js'; import * as http from 'http'; +import { SystemMessageManager } from './SystemMessage.js'; //TYPE DEFINITIONS interface ConfigurationOptions { @@ -24,7 +25,8 @@ class Bot { commands: CommandsManager; config: ConfigurationOptions; messages: { - help: HelpMessageParams + help: HelpMessageParams, + system: SystemMessageManager } on: { message: (m: Message) => any, @@ -46,7 +48,8 @@ class Bot { color: '#000', usage: '[command name (optional)]', bottomText: 'List of all available commands' - } + }, + system: new SystemMessageManager(this.name) } this.on = { message: (m: Message) => {}, @@ -82,11 +85,19 @@ class Bot { await calledCommand.start(m, cmdMsg.arguments); } catch (e) { - console.error(`ERROR! ${e.toString()}`); + if(e instanceof PermissionsError) { + this.messages.system.send('PERMISSION', { user: m.member || undefined, command: calledCommand }, m.channel); + } + else { + this.messages.system.send('ERROR', { command: calledCommand, user: m.member || undefined, error: e }, m.channel); + } return; } } } + else { + this.messages.system.send('NOT_FOUND', { phrase: m.content, user: m.member || undefined }, m.channel); + } }); } else { diff --git a/src/Command.ts b/src/Command.ts index 5597980..52a4e4c 100644 --- a/src/Command.ts +++ b/src/Command.ts @@ -66,7 +66,7 @@ class Command { throw new PermissionsError(this, message?.member); } } - static processPhrase(phrase?: string | string[]) : string[] { + private static processPhrase(phrase?: string | string[]) : string[] { if(Array.isArray(phrase)) { const buff = phrase.map((p) => { return p.split(' ').join('_'); diff --git a/src/SystemMessage.ts b/src/SystemMessage.ts index 75ffc85..3be9032 100644 --- a/src/SystemMessage.ts +++ b/src/SystemMessage.ts @@ -1,4 +1,4 @@ -import { ColorResolvable, GuildMember, MessageEmbed, TextChannel, User } from 'discord.js'; +import { ColorResolvable, DMChannel, GuildMember, MessageEmbed, NewsChannel, TextChannel, User } from 'discord.js'; import { Command, PermissionsError } from './Command.js'; type MessageType = 'PERMISSION' | 'ERROR' | 'NOT_FOUND'; @@ -42,7 +42,7 @@ class SystemMessageManager { } this.deleteTimeout = Infinity; } - async send(type: MessageType, data?: SystemMessageData, channel?: TextChannel) : Promise { + async send(type: MessageType, data?: SystemMessageData, channel?: TextChannel | DMChannel | NewsChannel) : Promise { if(this[type]) { const embed = new MessageEmbed(); embed.setTitle(this[type].title);