system messages implementation

This commit is contained in:
mmacierzanka committed 2021-06-21 18:22:15 +02:00
1 parent 83cf65c3b8
commit 15be754d3c
3 files changed
+18 -7

No files matched your search

+15 -4
View File
@@ -1,8 +1,9 @@
//IMPORTS //IMPORTS
import { Client, ClientOptions, Message } from 'discord.js'; 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 { HelpMessage, HelpMessageParams } from './Help.js';
import * as http from 'http'; import * as http from 'http';
import { SystemMessageManager } from './SystemMessage.js';
//TYPE DEFINITIONS //TYPE DEFINITIONS
interface ConfigurationOptions { interface ConfigurationOptions {
@@ -24,7 +25,8 @@ class Bot {
commands: CommandsManager; commands: CommandsManager;
config: ConfigurationOptions; config: ConfigurationOptions;
messages: { messages: {
help: HelpMessageParams help: HelpMessageParams,
system: SystemMessageManager
} }
on: { on: {
message: (m: Message) => any, message: (m: Message) => any,
@@ -46,7 +48,8 @@ class Bot {
color: '#000', color: '#000',
usage: '[command name (optional)]', usage: '[command name (optional)]',
bottomText: 'List of all available commands' bottomText: 'List of all available commands'
} },
system: new SystemMessageManager(this.name)
} }
this.on = { this.on = {
message: (m: Message) => {}, message: (m: Message) => {},
@@ -82,11 +85,19 @@ class Bot {
await calledCommand.start(m, cmdMsg.arguments); await calledCommand.start(m, cmdMsg.arguments);
} }
catch (e) { 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; return;
} }
} }
} }
else {
this.messages.system.send('NOT_FOUND', { phrase: m.content, user: m.member || undefined }, m.channel);
}
}); });
} }
else { else {
+1 -1
View File
@@ -66,7 +66,7 @@ class Command {
throw new PermissionsError(this, message?.member); throw new PermissionsError(this, message?.member);
} }
} }
static processPhrase(phrase?: string | string[]) : string[] { private static processPhrase(phrase?: string | string[]) : string[] {
if(Array.isArray(phrase)) { if(Array.isArray(phrase)) {
const buff = phrase.map((p) => { const buff = phrase.map((p) => {
return p.split(' ').join('_'); return p.split(' ').join('_');
+2 -2
View File
@@ -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'; import { Command, PermissionsError } from './Command.js';
type MessageType = 'PERMISSION' | 'ERROR' | 'NOT_FOUND'; type MessageType = 'PERMISSION' | 'ERROR' | 'NOT_FOUND';
@@ -42,7 +42,7 @@ class SystemMessageManager {
} }
this.deleteTimeout = Infinity; this.deleteTimeout = Infinity;
} }
async send(type: MessageType, data?: SystemMessageData, channel?: TextChannel) : Promise<MessageEmbed | void> { async send(type: MessageType, data?: SystemMessageData, channel?: TextChannel | DMChannel | NewsChannel) : Promise<MessageEmbed | void> {
if(this[type]) { if(this[type]) {
const embed = new MessageEmbed(); const embed = new MessageEmbed();
embed.setTitle(this[type].title); embed.setTitle(this[type].title);