From c65a8115aafdb5623204b2ef542ea5c016cc8e9e Mon Sep 17 00:00:00 2001 From: TheEssem Date: Sun, 26 Apr 2020 16:55:33 -0500 Subject: [PATCH] Added count, added some new playing lines, fixed security issue with hackban --- commands/count.js | 43 +++++++++++++++++++++++++++++++++++++++++ commands/hackban.js | 4 ++++ events/messageCreate.js | 3 +++ events/ready.js | 24 +++++++++++++++++++++-- messages.json | 16 ++++++++++++++- utils/database.js | 8 +++++++- 6 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 commands/count.js diff --git a/commands/count.js b/commands/count.js new file mode 100644 index 0000000..3f5690f --- /dev/null +++ b/commands/count.js @@ -0,0 +1,43 @@ +const client = require("../utils/client.js"); +const paginator = require("../utils/pagination/pagination.js"); +const database = require("../utils/database.js"); + +exports.run = async (message) => { + if (!message.channel.guild.members.get(client.user.id).permission.has("addReactions") && !message.channel.permissionsOf(client.user.id).has("addReactions")) return `${message.author.mention}, I don't have the \`Add Reactions\` permission!`; + if (!message.channel.guild.members.get(client.user.id).permission.has("embedLinks") && !message.channel.permissionsOf(client.user.id).has("embedLinks")) return `${message.author.mention}, I don't have the \`Embed Links\` permission!`; + const counts = (await database.global.findOne({}).lean().exec()).cmdCounts; + const countArray = []; + const sortedValues = Object.entries(counts).sort((a, b) => { + return b[1] - a[1]; + }); + console.log(sortedValues); + for (const [key, value] of sortedValues) { + countArray.push(`**${key}**: ${value}`); + } + const embeds = []; + const groups = countArray.map((item, index) => { + return index % 15 === 0 ? countArray.slice(index, index + 15) : null; + }).filter((item) => { + return item; + }); + for (const [i, value] of groups.entries()) { + embeds.push({ + "embed": { + "title": "Command Usage Counts", + "color": 16711680, + "footer": { + "text": `Page ${i + 1} of ${groups.length}` + }, + "description": value.join("\n"), + "author": { + "name": message.author.username, + "icon_url": message.author.avatarURL + } + } + }); + } + return paginator(message, embeds); +}; + +exports.category = 1; +exports.help = "Gets how many times every command was used"; \ No newline at end of file diff --git a/commands/hackban.js b/commands/hackban.js index dca1bc5..3908b76 100644 --- a/commands/hackban.js +++ b/commands/hackban.js @@ -1,4 +1,8 @@ +const client = require("../utils/client.js"); + exports.run = async (message, args) => { + if (!message.member.permission.has("banMembers")) return `${message.author.mention}, you need to have the \`Ban Members\` permission on this server to ban people!`; + if (!message.channel.guild.members.get(client.user.id).permission.has("banMembers") && !message.channel.permissionsOf(client.user.id).has("banMembers")) return `${message.author.mention}, I don't have the \`Ban Members\` permission!`; if (!args[0].match(/^?$/) && args[0] < 21154535154122752) return `${message.author.mention}, that's not a valid snowflake!`; try { const id = args[0].replace("@", "").replace("#", "").replace("!", "").replace("&", "").replace("<", "").replace(">", ""); diff --git a/events/messageCreate.js b/events/messageCreate.js index 9cab702..f1f5b44 100644 --- a/events/messageCreate.js +++ b/events/messageCreate.js @@ -39,6 +39,9 @@ module.exports = async (message) => { // actually run the command logger.log("info", `${message.author.username} (${message.author.id}) ran command ${command}`); try { + const global = (await database.global.findOne({}).exec()); + global.cmdCounts.set(collections.aliases.has(command) ? collections.aliases.get(command) : command, global.cmdCounts.get(command) + 1); + await global.save(); const result = await cmd(message, args, content.replace(command, "").trim()); // we also provide the message content as a parameter for cases where we need more accuracy if (typeof result === "string" || (typeof result === "object" && result.embed)) { await client.createMessage(message.channel.id, result); diff --git a/events/ready.js b/events/ready.js index 97e0cef..0248650 100644 --- a/events/ready.js +++ b/events/ready.js @@ -2,6 +2,7 @@ const gm = require("gm"); const { promisify } = require("util"); const client = require("../utils/client.js"); const database = require("../utils/database.js"); +const collections = require("../utils/collections.js"); const logger = require("../utils/logger.js"); const messages = require("../messages.json"); const misc = require("../utils/misc.js"); @@ -16,11 +17,11 @@ module.exports = async () => { for (const [id] of client.guilds) { const guildDB = ( await database.guilds - .find({ + .findOne({ id: id, }) .exec() - )[0]; + ); if (!guildDB) { logger.log(`Registering guild database entry for guild ${id}...`); const newGuild = new database.guilds({ @@ -44,6 +45,25 @@ module.exports = async () => { } } + const global = (await database.global.findOne({}).exec()); + if (!global) { + const countObject = {}; + for (const command of collections.commands.keys()) { + countObject[command] = 0; + } + const newGlobal = new database.global({ + cmdCounts: countObject + }); + await newGlobal.save(); + } else { + for (const command of collections.commands.keys()) { + if (!global.cmdCounts.has(command)) { + global.cmdCounts.set(command, 0); + await global.save(); + } + } + } + // generate docs if (helpGenerator) { await helpGenerator(process.env.OUTPUT); diff --git a/messages.json b/messages.json index 0325fb7..a88a795 100644 --- a/messages.json +++ b/messages.json @@ -88,5 +88,19 @@ "XMPlay", "OpenMPT", "follow @esmBot_ on Twitter", - "with GIFs" + "with GIFs", + "check out more stuff at projectlounge.pw", + "the", + "sissy hypnosis", + "Yandere Simulator", + "PogChamp", + "yourself", + "sentience", + "with Brody Foxx", + "AOL Instant Messenger", + "beep boop", + "Animal Crossing: New Horizons", + "Hello, Gordon!", + "the j", + "Want esmBot to play some new games? Send suggestions!" ] \ No newline at end of file diff --git a/utils/database.js b/utils/database.js index ffb3ae2..00b8d34 100644 --- a/utils/database.js +++ b/utils/database.js @@ -22,5 +22,11 @@ const tweetSchema = new mongoose.Schema({ }); const TweetCollection = mongoose.model("TweetCollection", tweetSchema); +const globalSchema = new mongoose.Schema({ + cmdCounts: Map +}); +const Global = mongoose.model("Global", globalSchema); + exports.guilds = Guild; -exports.tweets = TweetCollection; \ No newline at end of file +exports.tweets = TweetCollection; +exports.global = Global; \ No newline at end of file