-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(modules): add modules for message middlware
- Loading branch information
1 parent
6dcd12c
commit 46f2407
Showing
4 changed files
with
141 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const Discord = require('discord.js'); | ||
|
||
/** | ||
* Discord bot's middleware, or module | ||
*/ | ||
class Module { | ||
/** | ||
* @param {Client} bot - discord bot | ||
*/ | ||
constructor(bot) { | ||
this.bot = bot; | ||
this._path = Log._path; | ||
this.embed = Discord.RichEmbed; | ||
} | ||
|
||
/** | ||
* Middleware's priority | ||
*/ | ||
get priority() { | ||
return 0; | ||
} | ||
|
||
/** | ||
* Init module | ||
*/ | ||
init() { | ||
// eslint pls | ||
} | ||
|
||
/** | ||
* Bot's message middleware function | ||
* @param {Message} msg - the message | ||
* @param {string[]} args - message split by spaces | ||
* @param {function} next - next middleware pls <3 | ||
*/ | ||
run() { | ||
throw new Error(`No middleware method was set up in module ${this.constructor.name}`); | ||
} | ||
|
||
/** | ||
* Function to shorten sending error messages | ||
* @param {Message} msg - message sent by user (for channel) | ||
* @param {string} str - error message to send user | ||
*/ | ||
moduleError(msg, str) { | ||
return msg.channel.sendMessage(`❌ ${str}`); | ||
} | ||
|
||
/** | ||
* Convert normal text to an embed object | ||
* @param {string} [title = 'Auto Generated Response'] - embed title | ||
* @param {string|string[]} text - embed description, joined with newline if array | ||
* @param {color} [color = '#84F139'] - embed color | ||
* @return {RichEmbed} | ||
*/ | ||
textToEmbed(title = 'Auto Generated Response', text, color = '#84F139') { | ||
if (Array.isArray(text)) text = text.join('\n'); | ||
return new this.embed() | ||
.setColor(color) | ||
.setTitle(title) | ||
.setDescription(text) | ||
.setFooter(this.bot.user.username, this.bot.user.avatarURL); | ||
} | ||
} | ||
|
||
|
||
module.exports = Module; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const Module = require('../Module'); | ||
|
||
class RunCommandModule extends Module { | ||
get priority() { | ||
return 10; | ||
} | ||
|
||
run(msg, args, next, command) { | ||
let bot = this.bot; | ||
let perms = bot.permissions(msg); | ||
let cmd; | ||
|
||
if (bot.commands.has(command)) { | ||
cmd = bot.commands.get(command); | ||
} else if (bot.aliases.has(command)) { | ||
cmd = bot.commands.get(bot.aliases.get(command)); | ||
} else { | ||
return next(); | ||
} | ||
|
||
if (perms < cmd.conf.permLevel) return cmd.commandError(msg, `Insufficient permissions! Must be **${cmd._permLevelToWord(cmd.conf.permLevel)}** or higher`); | ||
|
||
try { | ||
let commandRun = cmd.run(msg, args); | ||
if (commandRun && commandRun.catch) { | ||
commandRun.catch(e => bot.commandError(msg, cmd, e)); | ||
} | ||
} catch (e) { | ||
bot.commandError(msg, cmd, e); | ||
} | ||
} | ||
} | ||
|
||
module.exports = RunCommandModule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters