Archived
included prefix into CommandsManager
This commit is contained in:
1 parent
3fa174ae70
commit
b138679041
3 files changed
+17
-14
No files matched your search
+5
-6
@@ -7,13 +7,13 @@ import { SystemMessageManager } from './SystemMessage.js';
|
||||
|
||||
//TYPE DEFINITIONS
|
||||
interface ConfigurationOptions {
|
||||
prefix: string,
|
||||
token?: string,
|
||||
helpCommand: boolean
|
||||
}
|
||||
interface ConstructorOptions {
|
||||
name: string,
|
||||
prefix: string,
|
||||
argumentSeparator?: string
|
||||
helpCommand?: boolean,
|
||||
clientOptions?: ClientOptions,
|
||||
token?: string,
|
||||
@@ -37,9 +37,8 @@ class Bot {
|
||||
constructor(options: ConstructorOptions) {
|
||||
this.name = options.name;
|
||||
this.client = new Client(options.clientOptions);
|
||||
this.commands = new CommandsManager();
|
||||
this.commands = new CommandsManager(options.prefix, options.argumentSeparator);
|
||||
this.config = {
|
||||
prefix: options.prefix,
|
||||
token: options.token,
|
||||
helpCommand: options.helpCommand != undefined ? options.helpCommand : true
|
||||
}
|
||||
@@ -61,7 +60,7 @@ class Bot {
|
||||
async start(port?: number, token?: string) : Promise<boolean> {
|
||||
try {
|
||||
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 || '';
|
||||
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');
|
||||
@@ -72,7 +71,7 @@ class Bot {
|
||||
}
|
||||
console.log('Starting modules...');
|
||||
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);
|
||||
}
|
||||
console.log('Connecting to Discord...');
|
||||
@@ -80,7 +79,7 @@ class Bot {
|
||||
console.log('BOT IS READY!\n');
|
||||
this.client.on('message', async 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) {
|
||||
this.on.command(m, cmdMsg);
|
||||
try {
|
||||
|
||||
+9
-5
@@ -93,9 +93,13 @@ class Command {
|
||||
}
|
||||
class CommandsManager {
|
||||
list: Command[];
|
||||
prefix: string;
|
||||
argumentSeparator: string;
|
||||
|
||||
constructor() {
|
||||
constructor(prefix: string, argumentSeparator?: string) {
|
||||
this.list = [];
|
||||
this.prefix = prefix;
|
||||
this.argumentSeparator = argumentSeparator || ',';
|
||||
}
|
||||
get(phrase: string, mode?: GetMode) : Command | null {
|
||||
if(!mode) mode = 'ALL';
|
||||
@@ -167,13 +171,13 @@ class CommandsManager {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
fetch(message: Message, prefix: string) : CommandMessageStructure | null {
|
||||
if(!message.author.bot && message.content.startsWith(prefix)) {
|
||||
let msgContent = message.content.replace(prefix, '');
|
||||
fetch(message: Message) : CommandMessageStructure | null {
|
||||
if(!message.author.bot && message.content.startsWith(this.prefix)) {
|
||||
let msgContent = message.content.replace(this.prefix, '');
|
||||
const cmdName = msgContent.split(' ')[0];
|
||||
const cmd: Command | null = this.get(cmdName, 'PREFIX');
|
||||
msgContent = msgContent.replace(cmdName, '');
|
||||
let cmdArguments: string[] = msgContent.split(',');
|
||||
let cmdArguments: string[] = msgContent.split(this.argumentSeparator);
|
||||
cmdArguments = cmdArguments.map((a) => {
|
||||
return a.replace(' ', '');
|
||||
});
|
||||
|
||||
+3
-3
@@ -10,7 +10,7 @@ interface HelpMessageParams {
|
||||
}
|
||||
|
||||
class HelpMessage extends Command {
|
||||
constructor(cmdManager: CommandsManager, params: HelpMessageParams, prefix: string, botName?: string) {
|
||||
constructor(cmdManager: CommandsManager, params: HelpMessageParams, botName?: string) {
|
||||
super({
|
||||
name: 'help',
|
||||
usage: params.usage,
|
||||
@@ -27,7 +27,7 @@ class HelpMessage extends Command {
|
||||
if(cmd) {
|
||||
helpMsg.setTitle(`${cmd.name} ${cmd.visible ? '' : '[HIDDEN]'}`);
|
||||
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) {
|
||||
let permList: string = '';
|
||||
cmd.permissions.toArray(false).map(p => {
|
||||
@@ -61,7 +61,7 @@ class HelpMessage extends Command {
|
||||
cmdManager.list.map((c) => {
|
||||
if(c.visible) {
|
||||
helpMsg.addField(
|
||||
`${prefix}${c.name} ${c.usage}`,
|
||||
`${cmdManager.prefix}${c.name} ${c.usage}`,
|
||||
c.description,
|
||||
false
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user