From 307a690d38b428380f4de080d73b7cf74472b8f9 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 13 Feb 2019 04:17:06 -0500 Subject: [PATCH 1/5] Bug Fix Fixed a bug where a user with multiple roles allowed to a plugin didn't work right, stupid typo. --- core/commands.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/commands.js b/core/commands.js index e2cfcb2..aac6dee 100644 --- a/core/commands.js +++ b/core/commands.js @@ -17,6 +17,7 @@ const checkCommand = (command, body) => { exports.register = (command, params, description, response) => { const compiled = params === '' ? `${command}` : `${command} ${params}`; + const commandRegex = compiled; if (checkCommand(command, params)) { registeredCommands.push({ @@ -25,6 +26,7 @@ exports.register = (command, params, description, response) => { description, response, compiled, + commandRegex, }); } }; @@ -70,7 +72,8 @@ client.on('message', async (msg) => { const plugin = loader.fromCommand(command); const userRoles = msg.member.roles.array(); const allowedRoles = getAllowedRoles(serverPermissions, userRoles, plugin); - if (pluginState && (`${serverPrefix}${command.compiled}` === message || match[1]) && (plugin.ignorePermissions || allowedRoles >= 1)) { + console.log(allowedRoles); + if (pluginState && (`${serverPrefix}${command.compiled}` === message || match[1]) && (plugin.ignorePermissions || allowedRoles.length >= 1)) { return command.response(msg, match); } } From 0d9b8042db45aeccfcb7a6eb0a91e8345ab3f52e Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 13 Feb 2019 04:24:59 -0500 Subject: [PATCH 2/5] Update/Remove Updated help messages with usage tags for commands, no more regex output, maybe implement param names inside of the param declaration and extract them out in replacement of regex? Updated all modules too. --- core/commands.js | 5 ++--- plugins/core/help.js | 6 +++--- plugins/core/permission-helper.js | 4 ++-- plugins/core/plugin-helper.js | 6 +++--- plugins/customize.js | 8 ++++---- plugins/moderation/user-management.js | 13 ++++++++++--- plugins/ping-pong.js | 28 --------------------------- 7 files changed, 24 insertions(+), 46 deletions(-) delete mode 100644 plugins/ping-pong.js diff --git a/core/commands.js b/core/commands.js index aac6dee..16a68ac 100644 --- a/core/commands.js +++ b/core/commands.js @@ -15,9 +15,8 @@ const checkCommand = (command, body) => { return true; }; -exports.register = (command, params, description, response) => { +exports.register = (command, params, usage, description, response) => { const compiled = params === '' ? `${command}` : `${command} ${params}`; - const commandRegex = compiled; if (checkCommand(command, params)) { registeredCommands.push({ @@ -26,7 +25,7 @@ exports.register = (command, params, description, response) => { description, response, compiled, - commandRegex, + usage, }); } }; diff --git a/plugins/core/help.js b/plugins/core/help.js index 82d57f3..a3d6432 100644 --- a/plugins/core/help.js +++ b/plugins/core/help.js @@ -3,7 +3,7 @@ const { discord } = require('@bot').client; exports.command = 'help'; -commands.register(this.command, '', 'Shows the help message', async (msg) => { +commands.register(this.command, '', 'help', 'Shows the help message', async (msg) => { const modules = loader.getPlugins(); const em = new discord.RichEmbed(); const prefix = await commands.getPrefix(msg.guild.id); @@ -15,7 +15,7 @@ commands.register(this.command, '', 'Shows the help message', async (msg) => { msg.reply(em); }); -commands.register(this.command, '(.*)', 'Shows the help message', async (msg, extra) => { +commands.register(this.command, '(.*)', 'help ', 'Shows the help message', async (msg, extra) => { const module = loader.getPlugin(extra[1]); if (module) { const em = new discord.RichEmbed(); @@ -23,7 +23,7 @@ commands.register(this.command, '(.*)', 'Shows the help message', async (msg, ex const prefix = await commands.getPrefix(msg.guild.id); em.setTitle(`${module.name} | Help`); moduleCommands.forEach((c) => { - em.addField(`${prefix}${c.command} ${c.params}`, `${c.description}`); + em.addField(`${prefix}${c.usage}`, `${c.description}`); }); return msg.channel.send(em); } diff --git a/plugins/core/permission-helper.js b/plugins/core/permission-helper.js index 3ff4180..437c2ba 100644 --- a/plugins/core/permission-helper.js +++ b/plugins/core/permission-helper.js @@ -3,7 +3,7 @@ const { discord } = require('@bot').client; exports.command = 'permissions'; -commands.register(this.command, 'add (.*) (.*)', 'Add permission to a plugin to a role', async (msg, extra) => { +commands.register(this.command, 'add (.*) (.*)', 'permissions add <@role>', 'Add permission to a plugin to a role', async (msg, extra) => { const plugin = loader.getPlugin(extra[1]); if (plugin) { const role = msg.mentions.roles.first(); @@ -16,7 +16,7 @@ commands.register(this.command, 'add (.*) (.*)', 'Add permission to a plugin to return msg.reply('Error plugin does not exist..'); }); -commands.register(this.command, '', 'Permissions help', async (msg) => { +commands.register(this.command, '', 'permissions', 'Permissions help', async (msg) => { const pluginCommands = commands.getCommands('permissions'); const em = new discord.RichEmbed(); const prefix = await commands.getPrefix(); diff --git a/plugins/core/plugin-helper.js b/plugins/core/plugin-helper.js index f261a6e..f9f1475 100644 --- a/plugins/core/plugin-helper.js +++ b/plugins/core/plugin-helper.js @@ -3,7 +3,7 @@ const { discord } = require('@bot').client; exports.command = 'plugins'; -commands.register(this.command, 'toggle (.*)', 'Toggle a plugin.', (msg, extra) => { +commands.register(this.command, 'toggle (.*)', 'plugins toggle ', 'Toggle a plugin.', (msg, extra) => { const module = loader.getPlugin(extra[1]); if (module) { module.toggle(); @@ -13,7 +13,7 @@ commands.register(this.command, 'toggle (.*)', 'Toggle a plugin.', (msg, extra) } }); -commands.register(this.command, 'status (.*)', 'Check the status of a plugin', (msg, extra) => { +commands.register(this.command, 'status (.*)', 'plugins status ', 'Check the status of a plugin', (msg, extra) => { const module = loader.getPlugin(extra[1]); if (module) { msg.reply(`Plugin status: ${module.state}`); @@ -22,7 +22,7 @@ commands.register(this.command, 'status (.*)', 'Check the status of a plugin', ( } }); -commands.register(this.command, '', 'Get a list of plugin discriminators', (msg) => { +commands.register(this.command, '', 'plugins', 'Get a list of plugin discriminators', (msg) => { const modules = loader.getPlugins(); const em = new discord.RichEmbed(); em.title = 'Plugins'; diff --git a/plugins/customize.js b/plugins/customize.js index d6614dc..53b272e 100644 --- a/plugins/customize.js +++ b/plugins/customize.js @@ -3,24 +3,24 @@ const { client, discord } = require('@bot').client; exports.command = 'customize'; -commands.register(this.command, '', 'Customize Help', async (msg) => { +commands.register(this.command, '', 'customize', 'Customize Help', async (msg) => { const pluginCommands = commands.getCommands('customize'); const em = new discord.RichEmbed(); const prefix = await commands.getPrefix(); em.setTitle('Customize | Help'); pluginCommands.forEach((c) => { - em.addField(`${prefix}${c.command} ${c.params}`, `${c.description}`); + em.addField(`${prefix}${c.usage}`, `${c.description}`); }); msg.channel.send(em); }); -commands.register(this.command, 'game (.*)', 'Change the bots game', (msg, extra) => { +commands.register(this.command, 'game (.*)', 'customize game ', 'Change the bots game', (msg, extra) => { client.user.setActivity(extra[1]); msg.reply(`Set game to: ${extra[1]}`); }); -commands.register(this.command, 'command prefix (.*)', 'Change the bots command Prefix', async (msg, extra) => { +commands.register(this.command, 'command prefix (.*)', 'customize command prefix ', 'Change the bots command Prefix', async (msg, extra) => { const changed = await commands.setPrefix(msg.guild.id, extra[1]); if (changed) { msg.reply(`Set prefix to: ${extra[1]}`); diff --git a/plugins/moderation/user-management.js b/plugins/moderation/user-management.js index 6abf2f1..ced1a9c 100644 --- a/plugins/moderation/user-management.js +++ b/plugins/moderation/user-management.js @@ -3,11 +3,18 @@ const { discord } = require('@bot').client; exports.command = 'mod'; -commands.register(this.command, '', 'Customize Help', (msg) => { - msg.reply('Customize the bot with other commands!'); +commands.register(this.command, '', 'mod', 'User Management Help', async (msg) => { + const pluginCommands = commands.getCommands('mod'); + const em = new discord.RichEmbed(); + const prefix = await commands.getPrefix(); + em.setTitle('Mod | Help'); + pluginCommands.forEach((c) => { + em.addField(`${prefix}${c.usage}`, `${c.description}`); + }); + msg.channel.send(em); }); -commands.register(this.command, 'info (.*)', 'Customize Help', (msg) => { +commands.register(this.command, 'info (.*)', 'mod info <@user>', 'Get a users information', (msg) => { const em = new discord.RichEmbed(); const user = msg.mentions.users.first(); em.setTitle(`User Information for [${user.username}]`); diff --git a/plugins/ping-pong.js b/plugins/ping-pong.js deleted file mode 100644 index eb4bf98..0000000 --- a/plugins/ping-pong.js +++ /dev/null @@ -1,28 +0,0 @@ -const { commands } = require('@bot'); - -exports.command = 'ping'; - -commands.register(this.command, '', 'Ping the bot', (msg) => { - msg.reply('PONG'); -}); - -commands.register(this.command, 'pong', 'Pong the bot', (msg) => { - msg.reply('PING'); -}); - -commands.register(this.command, 'pong ping pong ping', 'Pong the bot', (msg) => { - msg.reply('PONG'); -}); - -commands.register(this.command, 'pong ping pong', 'Ping the bot', (msg) => { - msg.reply('PING'); -}); - -exports.name = 'Ping-Pong'; -exports.version = '1.0.0'; -exports.description = 'Basic Ping Pong Plugin'; -exports.discrim = 'pingpong'; -exports.state = true; -exports.toggle = () => { - this.state = !this.state; -}; From c0724c144bffe3c7c73fbff6fb30a6a54f25617b Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 13 Feb 2019 04:29:50 -0500 Subject: [PATCH 3/5] Remove Removed uneeded print. --- core/commands.js | 1 - 1 file changed, 1 deletion(-) diff --git a/core/commands.js b/core/commands.js index 16a68ac..916fc42 100644 --- a/core/commands.js +++ b/core/commands.js @@ -71,7 +71,6 @@ client.on('message', async (msg) => { const plugin = loader.fromCommand(command); const userRoles = msg.member.roles.array(); const allowedRoles = getAllowedRoles(serverPermissions, userRoles, plugin); - console.log(allowedRoles); if (pluginState && (`${serverPrefix}${command.compiled}` === message || match[1]) && (plugin.ignorePermissions || allowedRoles.length >= 1)) { return command.response(msg, match); } From 8f06bc7a806cd7a50b59a508aa58cbdf4a015d8c Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 13 Feb 2019 05:29:08 -0500 Subject: [PATCH 4/5] Update Added warning system. --- core/commands.js | 4 +- core/database.js | 4 +- plugins/moderation/warn.js | 79 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 plugins/moderation/warn.js diff --git a/core/commands.js b/core/commands.js index 916fc42..6d4740a 100644 --- a/core/commands.js +++ b/core/commands.js @@ -61,6 +61,8 @@ const getAllowedRoles = (serverPermissions, userRoles, plugin) => { client.on('message', async (msg) => { const message = msg.content; + if (!msg.guild && msg.author.id !== client.user.id) return msg.reply('I do not work in DMs'); + if (msg.author.id === client.user.id) return false; const serverPrefix = await this.getPrefix(msg.guild.id); const serverPermissions = await permissions.getServerPermissions(msg.guild.id); for (let i = 0; i < registeredCommands.length; i += 1) { @@ -75,5 +77,5 @@ client.on('message', async (msg) => { return command.response(msg, match); } } - return null; + return false; }); diff --git a/core/database.js b/core/database.js index 6160f75..b31c7c9 100644 --- a/core/database.js +++ b/core/database.js @@ -19,13 +19,13 @@ const Server = mongoose.model('Server', { const User = mongoose.model('User', { server: { type: Schema.Types.ObjectId, ref: 'Server' }, - descrim: Number, + discrim: String, }); const UserWarning = mongoose.model('UserWarnings', { user: { type: Schema.Types.ObjectId, ref: 'User' }, warning: String, - warner: Number, + warner: String, }); const Configuration = mongoose.model('Configuration', { diff --git a/plugins/moderation/warn.js b/plugins/moderation/warn.js new file mode 100644 index 0000000..5eee80b --- /dev/null +++ b/plugins/moderation/warn.js @@ -0,0 +1,79 @@ +const { commands } = require('@bot'); +const { discord } = require('@bot').client; +const { User, Server, UserWarning } = require('@bot').database; + +exports.command = 'warn'; + +commands.register(this.command, '', 'warn', 'Warning Help', async (msg) => { + const pluginCommands = commands.getCommands('warn'); + const em = new discord.RichEmbed(); + const prefix = await commands.getPrefix(); + em.setTitle('Warnings | Help'); + pluginCommands.forEach((c) => { + em.addField(`${prefix}${c.usage}`, `${c.description}`); + }); + msg.channel.send(em); +}); + +/* + user: { type: Schema.Types.ObjectId, ref: 'User' }, + warning: String, + warner: Number, +*/ + +commands.register(this.command, '([^s]+) (.*)', 'warn <@user> ', 'Warn a user for something', async (msg, extra) => { + const serverID = msg.guild.id; + const server = await Server.findOne({ serverID }).exec(); + const warnedUser = msg.mentions.users.first(); + console.log(warnedUser.id); + const warning = extra[2]; + const warner = msg.author.id; + if (server && warnedUser) { + const user = await User.findOne({ server, discrim: warnedUser.id }); + if (!user) { + const newUser = await User.create({ server, discrim: warnedUser.id }); + UserWarning.create({ user: newUser, warning, warner }, (warningError) => { + if (warningError) throw warningError; + msg.reply(`Warned {${extra[1]}} for {${extra[2]}}`); + warnedUser.send(`You have been warned for {${extra[2]}}`); + }); + return true; + } + UserWarning.create({ user, warning, warner }, (warningError) => { + if (warningError) throw warningError; + msg.reply(`Warned {${extra[1]}} for {${extra[2]}}`); + warnedUser.send(`You have been warned for {${extra[2]}}`); + }); + return true; + } +}); + +commands.register(this.command, 'list (.*)', 'warn list <@user>', 'List a users warnings', async (msg, extra) => { + const serverID = msg.guild.id; + const server = await Server.findOne({ serverID }).exec(); + if (server) { + const targetUser = msg.mentions.users.first(); + console.log(targetUser.id); + const user = await User.findOne({ server, discrim: targetUser.id }).exec(); + console.log(`user: ${user}`); + const warnings = await UserWarning.find({ user }).exec(); + if (warnings) { + const em = new discord.RichEmbed(); + em.setTitle(`${targetUser.username}'s | Warnings`); + warnings.forEach(w => em.addField(w.warning, `Issuer: ${msg.guild.members.find(u => u.id === w.warner)}`)); + em.setFooter(`Total Warnings: ${warnings.length}`); + return msg.channel.send(em); + } + return msg.channel.send('Nothing for that user'); + } + return msg.channel.send('Nothing for that user'); +}); + +exports.name = 'Warnings'; +exports.version = '1.0.0'; +exports.description = 'Warnings Plugin'; +exports.discrim = 'warn'; +exports.state = true; +exports.toggle = () => { + this.state = !this.state; +}; From 9699418601a891a4661bbb6a5412e60737d9bb1f Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 13 Feb 2019 05:30:47 -0500 Subject: [PATCH 5/5] Cleanup --- plugins/moderation/warn.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/plugins/moderation/warn.js b/plugins/moderation/warn.js index 5eee80b..5fd853f 100644 --- a/plugins/moderation/warn.js +++ b/plugins/moderation/warn.js @@ -25,7 +25,6 @@ commands.register(this.command, '([^s]+) (.*)', 'warn <@user> ', 'Warn a const serverID = msg.guild.id; const server = await Server.findOne({ serverID }).exec(); const warnedUser = msg.mentions.users.first(); - console.log(warnedUser.id); const warning = extra[2]; const warner = msg.author.id; if (server && warnedUser) { @@ -34,28 +33,25 @@ commands.register(this.command, '([^s]+) (.*)', 'warn <@user> ', 'Warn a const newUser = await User.create({ server, discrim: warnedUser.id }); UserWarning.create({ user: newUser, warning, warner }, (warningError) => { if (warningError) throw warningError; - msg.reply(`Warned {${extra[1]}} for {${extra[2]}}`); - warnedUser.send(`You have been warned for {${extra[2]}}`); + msg.channel.send(`Warned {${extra[1]}} for {${extra[2]}}`); + return warnedUser.send(`You have been warned for {${extra[2]}}`); }); - return true; } UserWarning.create({ user, warning, warner }, (warningError) => { if (warningError) throw warningError; - msg.reply(`Warned {${extra[1]}} for {${extra[2]}}`); - warnedUser.send(`You have been warned for {${extra[2]}}`); + msg.channel.send(`Warned {${extra[1]}} for {${extra[2]}}`); + return warnedUser.send(`You have been warned for {${extra[2]}}`); }); - return true; } + return msg.channel.send('Cannot warn, error?'); }); -commands.register(this.command, 'list (.*)', 'warn list <@user>', 'List a users warnings', async (msg, extra) => { +commands.register(this.command, 'list (.*)', 'warn list <@user>', 'List a users warnings', async (msg) => { const serverID = msg.guild.id; const server = await Server.findOne({ serverID }).exec(); if (server) { const targetUser = msg.mentions.users.first(); - console.log(targetUser.id); const user = await User.findOne({ server, discrim: targetUser.id }).exec(); - console.log(`user: ${user}`); const warnings = await UserWarning.find({ user }).exec(); if (warnings) { const em = new discord.RichEmbed();