included prefix into CommandsManager

This commit is contained in:
mmacierzanka committed 2021-06-23 16:21:50 +02:00
1 parent 3fa174ae70
commit b138679041
3 files changed
+17 -14

No files matched your search

+5 -6
View File
@@ -7,13 +7,13 @@ import { SystemMessageManager } from './SystemMessage.js';
//TYPE DEFINITIONS //TYPE DEFINITIONS
interface ConfigurationOptions { interface ConfigurationOptions {
prefix: string,
token?: string, token?: string,
helpCommand: boolean helpCommand: boolean
} }
interface ConstructorOptions { interface ConstructorOptions {
name: string, name: string,
prefix: string, prefix: string,
argumentSeparator?: string
helpCommand?: boolean, helpCommand?: boolean,
clientOptions?: ClientOptions, clientOptions?: ClientOptions,
token?: string, token?: string,
@@ -37,9 +37,8 @@ class Bot {
constructor(options: ConstructorOptions) { constructor(options: ConstructorOptions) {
this.name = options.name; this.name = options.name;
this.client = new Client(options.clientOptions); this.client = new Client(options.clientOptions);
this.commands = new CommandsManager(); this.commands = new CommandsManager(options.prefix, options.argumentSeparator);
this.config = { this.config = {
prefix: options.prefix,
token: options.token, token: options.token,
helpCommand: options.helpCommand != undefined ? options.helpCommand : true helpCommand: options.helpCommand != undefined ? options.helpCommand : true
} }
@@ -61,7 +60,7 @@ class Bot {
async start(port?: number, token?: string) : Promise<boolean> { async start(port?: number, token?: string) : Promise<boolean> {
try { try {
console.log(`Bot name: ${this.name}`); console.log(`Bot name: ${this.name}`);
console.log(`Prefix: ${this.config.prefix} \n`); console.log(`Prefix: ${this.commands.prefix} \n`);
const loginToken: string = token || this.config.token || ''; const loginToken: string = token || this.config.token || '';
if(loginToken === '') { 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'); throw new ReferenceError('No token specified. Please pass your Discord application token as an argument to the "start" method or in the constructor');
@@ -72,7 +71,7 @@ class Bot {
} }
console.log('Starting modules...'); console.log('Starting modules...');
if(this.config.helpCommand) { if(this.config.helpCommand) {
const helpMsg: Command = new HelpMessage(this.commands, this.messages.help, this.config.prefix, this.name); const helpMsg: Command = new HelpMessage(this.commands, this.messages.help, this.name);
this.commands.add(helpMsg); this.commands.add(helpMsg);
} }
console.log('Connecting to Discord...'); console.log('Connecting to Discord...');
@@ -80,7 +79,7 @@ class Bot {
console.log('BOT IS READY!\n'); console.log('BOT IS READY!\n');
this.client.on('message', async m => { this.client.on('message', async m => {
this.on.message(m); this.on.message(m);
const cmdMsg: CommandMessageStructure | null = this.commands.fetch(m, this.config.prefix); const cmdMsg: CommandMessageStructure | null = this.commands.fetch(m);
if(cmdMsg?.command) { if(cmdMsg?.command) {
this.on.command(m, cmdMsg); this.on.command(m, cmdMsg);
try { try {
+9 -5
View File
@@ -93,9 +93,13 @@ class Command {
} }
class CommandsManager { class CommandsManager {
list: Command[]; list: Command[];
prefix: string;
argumentSeparator: string;
constructor() { constructor(prefix: string, argumentSeparator?: string) {
this.list = []; this.list = [];
this.prefix = prefix;
this.argumentSeparator = argumentSeparator || ',';
} }
get(phrase: string, mode?: GetMode) : Command | null { get(phrase: string, mode?: GetMode) : Command | null {
if(!mode) mode = 'ALL'; if(!mode) mode = 'ALL';
@@ -167,13 +171,13 @@ class CommandsManager {
return false; return false;
} }
} }
fetch(message: Message, prefix: string) : CommandMessageStructure | null { fetch(message: Message) : CommandMessageStructure | null {
if(!message.author.bot && message.content.startsWith(prefix)) { if(!message.author.bot && message.content.startsWith(this.prefix)) {
let msgContent = message.content.replace(prefix, ''); let msgContent = message.content.replace(this.prefix, '');
const cmdName = msgContent.split(' ')[0]; const cmdName = msgContent.split(' ')[0];
const cmd: Command | null = this.get(cmdName, 'PREFIX'); const cmd: Command | null = this.get(cmdName, 'PREFIX');
msgContent = msgContent.replace(cmdName, ''); msgContent = msgContent.replace(cmdName, '');
let cmdArguments: string[] = msgContent.split(','); let cmdArguments: string[] = msgContent.split(this.argumentSeparator);
cmdArguments = cmdArguments.map((a) => { cmdArguments = cmdArguments.map((a) => {
return a.replace(' ', ''); return a.replace(' ', '');
}); });
+3 -3
View File
@@ -10,7 +10,7 @@ interface HelpMessageParams {
} }
class HelpMessage extends Command { class HelpMessage extends Command {
constructor(cmdManager: CommandsManager, params: HelpMessageParams, prefix: string, botName?: string) { constructor(cmdManager: CommandsManager, params: HelpMessageParams, botName?: string) {
super({ super({
name: 'help', name: 'help',
usage: params.usage, usage: params.usage,
@@ -27,7 +27,7 @@ class HelpMessage extends Command {
if(cmd) { if(cmd) {
helpMsg.setTitle(`${cmd.name} ${cmd.visible ? '' : '[HIDDEN]'}`); helpMsg.setTitle(`${cmd.name} ${cmd.visible ? '' : '[HIDDEN]'}`);
helpMsg.setDescription(cmd.description); helpMsg.setDescription(cmd.description);
if(cmd.usage) helpMsg.addField('Usage:', `${prefix}${cmd.name} ${cmd.usage}`, false); if(cmd.usage) helpMsg.addField('Usage:', `${cmdManager.prefix}${cmd.name} ${cmd.usage}`, false);
if(cmd.permissions.toArray(false).length > 0) { if(cmd.permissions.toArray(false).length > 0) {
let permList: string = ''; let permList: string = '';
cmd.permissions.toArray(false).map(p => { cmd.permissions.toArray(false).map(p => {
@@ -61,7 +61,7 @@ class HelpMessage extends Command {
cmdManager.list.map((c) => { cmdManager.list.map((c) => {
if(c.visible) { if(c.visible) {
helpMsg.addField( helpMsg.addField(
`${prefix}${c.name} ${c.usage}`, `${cmdManager.prefix}${c.name} ${c.usage}`,
c.description, c.description,
false false
) )