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

+1 -3
View File
@@ -3,9 +3,7 @@
"version": "0.0.0", "version": "0.0.0",
"description": "A wrapper that helps you create your own Discord bot easier.", "description": "A wrapper that helps you create your own Discord bot easier.",
"main": "index.js", "main": "index.js",
"scripts": { "type": "module",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://github.com/GRZ4NA/DiscordCommands.git" "url": "git+https://github.com/GRZ4NA/DiscordCommands.git"
+69 -4
View File
@@ -1,18 +1,23 @@
//IMPORTS //IMPORTS
import { Client, ClientOptions } from 'discord.js'; import { Client, ClientOptions, Message } from 'discord.js';
import { Command, CommandsManager } from './Commands'; import { Command, CommandsManager } from './Commands';
import { HelpMessage, HelpMessageParams } from './Help';
import { CommandMessage } from './CommandMessage';
import * as http from 'http';
//TYPE DEFINITIONS //TYPE DEFINITIONS
interface ConfigurationOptions { interface ConfigurationOptions {
prefix: string, prefix: string,
accentColor: string accentColor: string,
token?: string
} }
interface ConstructorOptions { interface ConstructorOptions {
name: string, name: string,
prefix: string, prefix: string,
helpCommand?: boolean, helpCommand?: boolean,
clientOptions?: ClientOptions, clientOptions?: ClientOptions,
token?: string token?: string,
accentColor?: string
} }
//MAIN CLASS //MAIN CLASS
@@ -21,6 +26,13 @@ class Bot {
client: Client; client: Client;
commands: CommandsManager; commands: CommandsManager;
config: ConfigurationOptions; config: ConfigurationOptions;
messages: {
help: HelpMessageParams
}
on: {
message: (m: Message) => any,
command: (m: CommandMessage) => any
}
constructor(options: ConstructorOptions) { constructor(options: ConstructorOptions) {
this.name = options.name; this.name = options.name;
@@ -28,7 +40,60 @@ class Bot {
this.commands = new CommandsManager(); this.commands = new CommandsManager();
this.config = { this.config = {
prefix: options.prefix, 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;
} }
} }
} }
+32
View File
@@ -0,0 +1,32 @@
import { Message } from 'discord.js';
interface CommandMessageStructure {
name: string,
arguments: string[]
}
class CommandMessage extends Message {
command: CommandMessageStructure | null
constructor(message: Message, prefix: string) {
super(message.client, message, message.channel);
if(!message.author.bot && message.content.startsWith(prefix)) {
let msgContent = message.content.replace(prefix, '');
const cmdName = msgContent.split(' ')[0];
msgContent = msgContent.replace(cmdName, '');
let cmdArguments: string[] = msgContent.split(',');
cmdArguments = cmdArguments.map((a) => {
return a.replace(' ', '');
});
this.command = {
name: cmdName,
arguments: cmdArguments
}
}
else {
this.command = null;
}
}
}
export { CommandMessage }
+14 -14
View File
@@ -1,5 +1,5 @@
//IMPORTS //IMPORTS
import { Permissions, PermissionResolvable, Message, GuildMember } from 'discord.js'; import { Permissions, PermissionResolvable, Message, GuildMember, MessageEmbed } from 'discord.js';
//TYPE DEFINITIONS //TYPE DEFINITIONS
interface CommandBuilder { interface CommandBuilder {
@@ -9,7 +9,8 @@ interface CommandBuilder {
description?: string, description?: string,
usage?: string, usage?: string,
permissions?: PermissionResolvable, permissions?: PermissionResolvable,
function: (message?: Message) => void | string; visible?: boolean
function: (message?: Message) => void | string | MessageEmbed;
} }
//ERROR CLASSES //ERROR CLASSES
@@ -33,21 +34,17 @@ class Command {
description: string; description: string;
usage: string; usage: string;
permissions: Permissions; permissions: Permissions;
private function: (message?: Message) => void | string; visible: boolean;
private function: (message?: Message) => void | string | MessageEmbed;
constructor(options: CommandBuilder) { constructor(options: CommandBuilder) {
this.name = options.name.split(' ').join('_'); this.name = options.name.split(' ').join('_');
this.aliases = []; this.aliases = ProcessPhrase(options.aliases);
if(options.aliases) { this.keywords = ProcessPhrase(options.keywords);
this.aliases = ProcessPhrase(options.aliases);
}
this.keywords = [];
if(options.keywords) {
this.keywords = ProcessPhrase(options.keywords);
}
this.description = options.description || "No description"; this.description = options.description || "No description";
this.usage = options.usage || "-"; this.usage = options.usage || "";
this.permissions = new Permissions(options.permissions || 0); this.permissions = new Permissions(options.permissions || 0);
this.visible = options.visible || true;
this.function = options.function; this.function = options.function;
} }
async start(message?: Message) { async start(message?: Message) {
@@ -107,18 +104,21 @@ class CommandsManager {
} }
//FUNCTIONS //FUNCTIONS
const ProcessPhrase = (phrase: string | string[]) : string[] => { const 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('_');
}); });
return buff; return buff;
} }
else { else if(typeof phrase == 'string') {
const buff = []; const buff = [];
buff.push(phrase.split(' ').join('_')); buff.push(phrase.split(' ').join('_'));
return buff; return buff;
} }
else {
return [];
}
} }
//EXPORTS //EXPORTS
+44
View File
@@ -0,0 +1,44 @@
import { Message, MessageEmbed } from 'discord.js';
import { Command, CommandsManager } from './Commands';
interface HelpMessageParams {
title: string,
bottomText: string
color: string,
description: string,
usage: string
}
class HelpMessage extends Command {
constructor(cmdManager: CommandsManager, params: HelpMessageParams, prefix: string, botName?: string) {
super({
name: 'help',
usage: params.usage,
permissions: 0,
description: params.description,
function: (m?: Message) => {
const helpMsg = new MessageEmbed()
if(helpMsg != null) {
helpMsg.setColor(params.color);
helpMsg.setDescription(params.bottomText);
helpMsg.setTimestamp();
helpMsg.setTitle(params.title);
helpMsg.setFooter(botName || '');
cmdManager.list.map((c) => {
if(c.visible) {
helpMsg.addField(
`${prefix}${c.name} ${c.usage}`,
c.description,
false
)
}
});
}
return helpMsg;
}
});
}
}
export { HelpMessage, HelpMessageParams };