Message object, help message

This commit is contained in:
mmacierzanka committed 2021-06-20 00:43:38 +02:00
1 parent be20c57974
commit f0f3921c09
5 files changed
+160 -21

No files matched your search

+69 -4
View File
@@ -1,18 +1,23 @@
//IMPORTS
import { Client, ClientOptions } from 'discord.js';
import { Client, ClientOptions, Message } from 'discord.js';
import { Command, CommandsManager } from './Commands';
import { HelpMessage, HelpMessageParams } from './Help';
import { CommandMessage } from './CommandMessage';
import * as http from 'http';
//TYPE DEFINITIONS
interface ConfigurationOptions {
prefix: string,
accentColor: string
accentColor: string,
token?: string
}
interface ConstructorOptions {
name: string,
prefix: string,
helpCommand?: boolean,
clientOptions?: ClientOptions,
token?: string
token?: string,
accentColor?: string
}
//MAIN CLASS
@@ -21,6 +26,13 @@ class Bot {
client: Client;
commands: CommandsManager;
config: ConfigurationOptions;
messages: {
help: HelpMessageParams
}
on: {
message: (m: Message) => any,
command: (m: CommandMessage) => any
}
constructor(options: ConstructorOptions) {
this.name = options.name;
@@ -28,7 +40,60 @@ class Bot {
this.commands = new CommandsManager();
this.config = {
prefix: options.prefix,
accentColor: "#000"
accentColor: "#000",
token: options.token
}
this.messages = {
help: {
title: 'Help',
description: 'List of all available commands',
color: '#000',
usage: '[command name (optional)]',
bottomText: 'List of all available commands'
}
}
this.on = {
message: (m: Message) => {},
command: (m: CommandMessage) => { console.log(m.command); }
}
}
async start(port?: number, token?: string) : Promise<boolean> {
try {
console.log(`Bot name: ${this.name}`);
const loginToken: string = token || this.config.token || '';
if(loginToken === '') {
throw new ReferenceError('No token specified. Please pass your Discord application token as an argument to the "start" method or in the constructor');
}
if(port) {
console.log(`Creating http server on port ${port}...`);
http.createServer().listen(port);
}
console.log('Starting modules...');
const helpMsg: Command = new HelpMessage(this.commands, this.messages.help, this.config.prefix, this.name);
this.commands.add(helpMsg);
console.log('Connecting to Discord...');
if(await this.client.login(token)) {
console.log('BOT IS READY!\n');
this.client.on('message', (m) => {
this.on.message(m);
const cmdMsg: CommandMessage = new CommandMessage(m, this.config.prefix);
if(cmdMsg.command) {
this.on.command(cmdMsg);
const calledCommand = this.commands.get(cmdMsg.command.name);
if(calledCommand) {
calledCommand.start(cmdMsg);
}
}
});
}
else {
return false;
}
return true;
}
catch (e) {
console.error(`ERROR! ${e.toString()}`);
return false;
}
}
}