From 8ba49a228bdf178a7c4389eb4af9c5920bdb21cb Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Mon, 7 Dec 2020 19:04:21 -0700 Subject: [PATCH] Add ARs and decide.js and patch bugs --- bot.js | 3 ++- commands/eval.js | 6 +++--- commands/reload.js | 13 ++++++++++++- commands/warn.js | 29 +++++++++++++++++------------ events/message.js | 5 +++-- handle/response.js | 16 ++++++++++++++++ models/mod.js | 22 ++++++++++++++++++---- responses/decide.js | 29 +++++++++++++++++++++++++++++ 8 files changed, 100 insertions(+), 23 deletions(-) create mode 100644 handle/response.js create mode 100644 responses/decide.js diff --git a/bot.js b/bot.js index 65e3280..bfdec62 100644 --- a/bot.js +++ b/bot.js @@ -10,7 +10,8 @@ async function init() { client.config = auth; ['commands', 'aliases'].forEach(x => client[x] = new Discord.Collection()); - ['command', 'event'].forEach(x => require(`./handle/${x}`)(client)); + client.responses = {triggers: [], commands: new Discord.Collection()}; + ['command', 'event', 'response'].forEach(x => require(`./handle/${x}`)(client)); client.developers = ["330547934951112705", "673477059904929802"]; diff --git a/commands/eval.js b/commands/eval.js index cfdfa58..7bc4949 100644 --- a/commands/eval.js +++ b/commands/eval.js @@ -13,18 +13,18 @@ module.exports = { const result = new Promise((resolve) => resolve(eval(args.join(' ')))); return result.then((output) => { if (typeof output !== 'string') { - output = require('util').inspect(output, { depth: 0 }); + output = require('util').inspect(output, {depth: 0}); } output = output.replace(client.config.token, 'Client Token') .replace(client.config.database.password, 'Database Password') - .replace(client.config.database.cluster, 'Database Cluster') + .replace(client.config.database.cluster, 'Database Cluster'); return message.channel.send(new Discord.MessageEmbed() .setTitle('Client Evaluation') .setDescription(`\`\`\`js\n${output}\n\`\`\``) .setColor('c375f0') .setFooter(`Natsuki`, client.user.avatarURL()) - .setTimestamp()) + .setTimestamp()); }).catch(error => {return message.channel.send(`Error: \`${error}\`.`);}); } catch (error) { let date = new Date; date = date.toString().slice(date.toString().search(":") - 2, date.toString().search(":") + 6); diff --git a/commands/reload.js b/commands/reload.js index 9793385..a76eb8e 100644 --- a/commands/reload.js +++ b/commands/reload.js @@ -39,13 +39,24 @@ module.exports = { } console.log(`${chalk.gray('[LOG]')} >> ${chalk.blue('Loaded all Events')}`); + var responses = fs.readdirSync('./responses').filter(file => file.endsWith('.js')); + client.responses.triggers = []; + for (let responsef of responses) { + if (Object.keys(require.cache).includes(require.resolve(`../responses/${responsef}`))) {delete require.cache[require.resolve(`../responses/${responsef}`)];} + var response = require(`../responses/${responsef}`); + client.responses.triggers.push([response.name, response.condition]); + client.responses.commands.set(response.name, response); + } + console.log(`${chalk.gray('[LOG]')} >> ${chalk.blue('Loaded all Responses')}`); + console.log(`\n${chalk.gray('[INFO]')} >> ${chalk.hex('ff4fd0')(`Client refresh successful`)}\n`); return message.channel.send("Done!") } if (['l', 'log', 'ns', 'nosilent', 'notsilent'].includes(args[0].toLowerCase())) { ['commands', 'aliases'].forEach(x => client[x] = new Discord.Collection()); - ['command', 'event'].forEach(x => require(`../handle/${x}`)(client)); + client.responses = {triggers: [], commands: new Discord.Collection()}; + ['command', 'event', 'response'].forEach(x => require(`./${x}`)(client)); return message.channel.send("Done!"); } else {return message.channel.send("Oi! 'log' is the only valid arg to use. Use no args if you want a cleaner console output instead.");} diff --git a/commands/warn.js b/commands/warn.js index c2c1f2a..f70e18c 100644 --- a/commands/warn.js +++ b/commands/warn.js @@ -24,21 +24,20 @@ module.exports = { if (['check', 'c', 'list', 'l'].includes(args[0].toLowerCase())) { user = user ? user : message.member; let mh = await Mod.findOne({gid: message.guild.id}); - if (!mh || !mh.warnings.size) {return message.reply("There are no warnings available in this server.");} + if (!mh || !Object.keys(mh.warnings).length) {return message.reply("There are no warnings available in this server.");} - if (!mh.warnings.has(user.id) || !mh.warnings.get(user.id).length) {return message.reply(`${user.id === message.author.id ? 'You have' : 'That user has'} never been warned in this server.`);} - console.log(mh.cases, mh.warnings); + if (!mh.warnings[user.id] || !mh.warnings[user.id].length) {return message.reply(`${user.id === message.author.id ? 'You have' : 'That user has'} never been warned in this server.`);} + //console.log(mh.cases, mh.warnings); let ws = ''; let cwc = 0; - let warning; for (warning of mh.warnings.get(user.id)) { - let tcase = mh.cases.get(`${warning}`); - console.log(tcase.status, warning); + let warning; for (warning of mh.warnings[user.id]) { + let tcase = mh.cases[warning - 1]; if (tcase.status !== "Cleared") { ws += `\`Case #${warning}\` - Issued by <@${tcase.moderators[0]}>\n${tcase.reason}\n\n`; } else {cwc++;} } if (cwc > 0) {ws += '*Plus ' + cwc + ' other warnings that have been cleared.*';} - if (cwc === mh.warnings.get(user.id).length) {return message.reply("That user has no uncleared warnings.");} + if (cwc === mh.warnings[user.id].length) {return message.reply("That user has no uncleared warnings.");} return message.channel.send(new Discord.MessageEmbed() .setTitle("User Warnings") .setThumbnail(client.users.cache.get(user.id).avatarURL({size: 1024})) @@ -97,7 +96,7 @@ module.exports = { let mh = await Mod.findOne({gid: message.guild.id}) || new Mod({gid: message.guild.id}); let mhcases = mh.cases; - mhcases.set(`${mh.cases.size + 1}`, { + mhcases.push({ members: [user.id], punishment: "Warned", reason: reason, @@ -109,14 +108,20 @@ module.exports = { }); let mhwarnings = mh.warnings; + let mhwarningsk = Object.keys(mhwarnings); + console.log(mhwarnings); - console.log(mhcases.size); - if (mhwarnings.has(user.id)) {var uw = mhwarnings.get(user.id); uw[mhcases.size - 1] = mhcases.size;} - mhwarnings.set(user.id, mhwarnings.has(user.id) ? uw : [mhcases.size]); + + if (mhwarningsk.includes(user.id)) {let tw = mhwarnings[user.id]; tw.push(mhcases.length); mhwarnings[user.id] = tw;} + else {mhwarnings[user.id] = [mhcases.length];} + + console.log(mhwarnings); + mh.warnings = mhwarnings; + mh.warnings[user.id] = mhwarnings[user.id]; mh.cases = mhcases; - if (!options.silent) {message.channel.send(`Case ${mh.cases.size} - Member has been warned. Reason: \`${reason}\``);} + if (!options.silent) {message.channel.send(`Case ${mh.cases.length} - Member has been warned. Reason: \`${reason}\``);} if (!options.silent && !options.nodm) {client.users.cache.get(user.id).send(`\`${message.author.username}\` has warned you in \`${message.guild.name}\`. Reason: **${reason}**`);} mh.save(); diff --git a/events/message.js b/events/message.js index b58e5ad..a0220fa 100644 --- a/events/message.js +++ b/events/message.js @@ -49,13 +49,14 @@ module.exports = async (client, message) => { try { if (msg.startsWith(prefix) || msg.startsWith(`<@${client.user.id}>`) || msg.startsWith(`<@!${client.user.id}>`)) { let command = client.commands.get(cmd) || client.commands.get(client.aliases.get(cmd)); - if (!command) {return;} + if (!command) {let trigger; for (trigger of client.responses.triggers) {if (await trigger[1](message, msg, args, cmd, prefix, mention, client)) {await client.responses.commands.get(trigger[0]).execute(message, msg, args, cmd, prefix, mention, client); break;}} return;} message.channel.startTyping(); await wait(800); message.channel.stopTyping(); require('../util/oncommand')(message, msg, args, cmd, prefix, mention, client); - command.execute(message, msg, args, cmd, prefix, mention, client); + return command.execute(message, msg, args, cmd, prefix, mention, client); } + let trigger; for (trigger of client.responses.triggers) {if (await trigger[1](message, msg, args, cmd, prefix, mention, client)) {await client.responses.commands.get(trigger[0]).execute(message, msg, args, cmd, prefix, mention, client); break;}} } catch (e) { var date = new Date; date = date.toString().slice(date.toString().search(":") - 2, date.toString().search(":") + 6); console.error(`\n${chalk.red('[ERROR]')} >> ${chalk.yellow(`At [${date}] | In ${message.guild.name}\n`)}`, e); diff --git a/handle/response.js b/handle/response.js new file mode 100644 index 0000000..1ad9f94 --- /dev/null +++ b/handle/response.js @@ -0,0 +1,16 @@ +const Discord = require('discord.js'); +const fs = require('fs'); +const chalk = require('chalk'); + +module.exports = client => { + var responses = fs.readdirSync('./responses').filter(file => file.endsWith('.js')); + console.log(`\n${chalk.gray('[BOOT]')} >> ${chalk.blue('Getting Responses...')}\n`); + for (let responsef of responses) { + if (Object.keys(require.cache).includes(require.resolve(`../responses/${responsef}`))) {delete require.cache[require.resolve(`../responses/${responsef}`)];} + var response = require(`../responses/${responsef}`); + client.responses.triggers.push([response.name, response.condition]); + client.responses.commands.set(response.name, response); + console.log(`${chalk.gray('[LOG] ')} >> ${chalk.blueBright('Loaded Response')} ${chalk.white(response.name)}`); + } + console.log(`\n${chalk.gray('[BOOT]')} >> ${chalk.blue('Loaded all Responses')}`); +}; \ No newline at end of file diff --git a/models/mod.js b/models/mod.js index b248801..9d07e15 100644 --- a/models/mod.js +++ b/models/mod.js @@ -2,10 +2,24 @@ const mongoose = require('mongoose'); const ModModel = new mongoose.Schema({ gid: {type: String, unique: true}, - cases: {type: Map, default: new Map()}, - rules: {type: Map, default: new Map()}, - auto: {type: Map, default: new Map()}, - warnings: {type: Map, default: new Map()}, + cases: {type: [{ + members: [String], + punishment: String, + reason: String, + status: String, + moderators: [String], + notes: [String], + history: [String], + issued: Date + }], default: []}, + rules: {type: [{ + name: String, + description: String, + punishment: String, + automod: String + }], default: []}, + auto: {type: Object, default: {}}, + warnings: {type: Object, default: {}}, maxWarnings: {type: Number, default: 0}, onMaxWarn: {type: String, default: 'kick'} }); diff --git a/responses/decide.js b/responses/decide.js new file mode 100644 index 0000000..e484b6b --- /dev/null +++ b/responses/decide.js @@ -0,0 +1,29 @@ +const Discord = require('discord.js'); + +module.exports = { + name: "decide", + meta: { + category: "", + perms: "", + staff: false, + vip: "", + serverPerms: [], + writtenBy: "", + serverOnly: false + }, + tags: [], + help: new Discord.MessageEmbed() + .setTitle("Help -> ") + .setDescription("") + .addField("Syntax", "``"), + async condition (message, msg, args, cmd, prefix, mention, client) {return msg.split(/\s+/gm).length > 3 && (msg.startsWith(`<@${client.user.id}>`) || msg.startsWith(`<@!${client.user.id}>`));}, + async execute(message, msg, args, cmd, prefix, mention, client) { + let e = message.content.split(/\s+/g); e.shift(); + let options = e.join(" ").split(/(?:(?:\s+)[oO][rR] [sS][hH][oO][uU][lL][dD] [iI](?:\s+)|,(?:\s*)[oO][rR] [sS][hH][oO][uU][lL][dD] [iI](?:\s+)|(?:\s+)[oO][rR] [dD][oO] [iI](?:\s+)|,(?:\s*)[dD][oO] [iI](?:\s+)|,(?:\s*)[sS][hH][oO][uU][lL][dD] [iI](?:\s+)|,(?:\s*)[oO][rR] [dD][oO] [iI](?:\s+)|,(?:\s*)[oO][rR](?:\s+)|(?:\s+)[oO][rR](?:\s+)|,(?:\s*))/gm); + //console.log(e, options); + if (options.length < 2) {return;} + let cleanups = ['should i ', 'do i ']; + let option; for (option of options) {let c; for (c of cleanups) {if (option.trim().toLowerCase().startsWith(c)) {options[options.indexOf(option)] = option.trim().slice(c.length);}}} + return message.channel.send(`${['You should', 'How about', 'Hmmm... I pick', 'How about you', 'I think you should'][Math.floor(Math.random() * 5)]} ${options[Math.floor(Math.random() * options.length)]}.`); + } +}; \ No newline at end of file