From 5e8c587c417435fc6831999ba0090bcd8506bfe3 Mon Sep 17 00:00:00 2001 From: Essem Date: Sat, 14 Aug 2021 08:00:16 -0500 Subject: [PATCH] Improved tag retrieval, blacklisted a ton of events, fixed skip issue for real --- app.js | 16 ++++- commands/tags/tags.js | 123 ++++++++++++++++++----------------- utils/database/dummy.js | 1 + utils/database/postgresql.js | 11 +++- utils/database/sqlite.js | 5 ++ utils/soundplayer.js | 2 +- 6 files changed, 94 insertions(+), 64 deletions(-) diff --git a/app.js b/app.js index 88c5779..53bd155 100644 --- a/app.js +++ b/app.js @@ -65,7 +65,21 @@ const Admiral = new Fleet({ GUILD_ROLE_DELETE: true, GUILD_ROLE_UPDATE: true, TYPING_START: true, - MESSAGE_DELETE_BULK: true + MESSAGE_DELETE_BULK: true, + WEBHOOKS_UPDATE: true, + STAGE_INSTANCE_CREATE: true, + STAGE_INSTANCE_DELETE: true, + STAGE_INSTANCE_UPDATE: true, + MESSAGE_REACTION_ADD: true, + MESSAGE_REACTION_REMOVE: true, + MESSAGE_REACTION_REMOVE_ALL: true, + MESSAGE_REACTION_REMOVE_EMOJI: true, + INVITE_CREATE: true, + INVITE_DELETE: true, + THREAD_CREATE: true, + THREAD_UPDATE: true, + THREAD_DELETE: true, + THREAD_LIST_SYNC: true }, allowedMentions: { everyone: false, diff --git a/commands/tags/tags.js b/commands/tags/tags.js index 764cd4a..005ee76 100644 --- a/commands/tags/tags.js +++ b/commands/tags/tags.js @@ -9,69 +9,70 @@ class TagsCommand extends Command { if (!this.message.channel.guild) return "This command only works in servers!"; if (this.args.length === 0) return "You need to provide the name of the tag you want to view!"; - const tags = await database.getTags(this.message.channel.guild.id); const blacklist = ["create", "add", "edit", "remove", "delete", "list", "random", "own", "owner"]; - switch (this.args[0].toLowerCase()) { - case "create": - case "add": - if (this.args[1] === undefined) return "You need to provide the name of the tag you want to add!"; - if (blacklist.includes(this.args[1].toLowerCase())) return "You can't make a tag with that name!"; - if (tags[this.args[1].toLowerCase()]) return "This tag already exists!"; - var result = await this.setTag(this.args.slice(2).join(" "), this.args[1].toLowerCase(), this.message); - if (result) return result; - return `The tag \`${this.args[1].toLowerCase()}\` has been added!`; - case "delete": - case "remove": - if (this.args[1] === undefined) return "You need to provide the name of the tag you want to delete!"; - if (!tags[this.args[1].toLowerCase()]) return "This tag doesn't exist!"; - if (tags[this.args[1].toLowerCase()].author !== this.message.author.id && !this.message.member.permissions.has("manageMessages") && this.message.author.id !== process.env.OWNER) return "You don't own this tag!"; - await database.removeTag(this.args[1].toLowerCase(), this.message.channel.guild); - return `The tag \`${this.args[1].toLowerCase()}\` has been deleted!`; - case "edit": - if (this.args[1] === undefined) return "You need to provide the name of the tag you want to edit!"; - if (!tags[this.args[1].toLowerCase()]) return "This tag doesn't exist!"; - if (tags[this.args[1].toLowerCase()].author !== this.message.author.id && !this.message.member.permissions.has("manageMessages") && this.message.author.id !== process.env.OWNER) return "You don't own this tag!"; - await this.setTag(this.args.slice(2).join(" "), this.args[1].toLowerCase(), this.message, true); - return `The tag \`${this.args[1].toLowerCase()}\` has been edited!`; - case "own": - case "owner": - if (this.args[1] === undefined) return "You need to provide the name of the tag you want to check the owner of!"; - if (!tags[this.args[1].toLowerCase()]) return "This tag doesn't exist!"; - var user = await this.ipc.fetchUser(tags[this.args[1].toLowerCase()].author); - if (!user) return `I couldn't find exactly who owns this tag, but I was able to get their ID: \`${tags[this.args[1].toLowerCase()].author}\``; - return `This tag is owned by **${user.username}#${user.discriminator}** (\`${tags[this.args[1].toLowerCase()].author}\`).`; - case "list": - if (!this.message.channel.permissionsOf(this.client.user.id).has("embedLinks")) return "I don't have the `Embed Links` permission!"; - var pageSize = 15; - var embeds = []; - var groups = Object.keys(tags).map((item, index) => { - return index % pageSize === 0 ? Object.keys(tags).slice(index, index + pageSize) : null; - }).filter((item) => { - return item; - }); - for (const [i, value] of groups.entries()) { - embeds.push({ - "embed": { - "title": "Tag List", - "color": 16711680, - "footer": { - "text": `Page ${i + 1} of ${groups.length}` - }, - "description": value.join("\n"), - "author": { - "name": this.message.author.username, - "icon_url": this.message.author.avatarURL - } + if (this.args[0].toLowerCase() === "create" || this.args[0].toLowerCase() === "add") { + if (this.args[1] === undefined) return "You need to provide the name of the tag you want to add!"; + if (blacklist.includes(this.args[1].toLowerCase())) return "You can't make a tag with that name!"; + const getResult = await database.getTag(this.message.channel.guild.id, this.args[1].toLowerCase()); + if (getResult) return "This tag already exists!"; + const result = await this.setTag(this.args.slice(2).join(" "), this.args[1].toLowerCase(), this.message); + if (result) return result; + return `The tag \`${this.args[1].toLowerCase()}\` has been added!`; + } else if (this.args[0].toLowerCase() === "delete" || this.args[0].toLowerCase() === "remove") { + if (this.args[1] === undefined) return "You need to provide the name of the tag you want to delete!"; + const getResult = await database.getTag(this.message.channel.guild.id, this.args[1].toLowerCase()); + if (!getResult) return "This tag doesn't exist!"; + if (getResult.author !== this.message.author.id && !this.message.member.permissions.has("manageMessages") && this.message.author.id !== process.env.OWNER) return "You don't own this tag!"; + await database.removeTag(this.args[1].toLowerCase(), this.message.channel.guild); + return `The tag \`${this.args[1].toLowerCase()}\` has been deleted!`; + } else if (this.args[0].toLowerCase() === "edit") { + if (this.args[1] === undefined) return "You need to provide the name of the tag you want to edit!"; + const getResult = await database.getTag(this.message.channel.guild.id, this.args[1].toLowerCase()); + if (!getResult) return "This tag doesn't exist!"; + if (getResult.author !== this.message.author.id && !this.message.member.permissions.has("manageMessages") && this.message.author.id !== process.env.OWNER) return "You don't own this tag!"; + await this.setTag(this.args.slice(2).join(" "), this.args[1].toLowerCase(), this.message, true); + return `The tag \`${this.args[1].toLowerCase()}\` has been edited!`; + } else if (this.args[0].toLowerCase() === "own" || this.args[0].toLowerCase() === "owner") { + if (this.args[1] === undefined) return "You need to provide the name of the tag you want to check the owner of!"; + const getResult = await database.getTag(this.message.channel.guild.id, this.args[1].toLowerCase()); + if (!getResult) return "This tag doesn't exist!"; + const user = await this.ipc.fetchUser(getResult.author); + if (!user) return `I couldn't find exactly who owns this tag, but I was able to get their ID: \`${getResult.author}\``; + return `This tag is owned by **${user.username}#${user.discriminator}** (\`${getResult.author}\`).`; + } else if (this.args[0].toLowerCase() === "list") { + if (!this.message.channel.permissionsOf(this.client.user.id).has("embedLinks")) return "I don't have the `Embed Links` permission!"; + const tagList = await database.getTags(this.message.channel.guild.id); + const embeds = []; + const groups = Object.keys(tagList).map((item, index) => { + return index % 15 === 0 ? Object.keys(tagList).slice(index, index + 15) : null; + }).filter((item) => { + return item; + }); + for (const [i, value] of groups.entries()) { + embeds.push({ + "embed": { + "title": "Tag List", + "color": 16711680, + "footer": { + "text": `Page ${i + 1} of ${groups.length}` + }, + "description": value.join("\n"), + "author": { + "name": this.message.author.username, + "icon_url": this.message.author.avatarURL } - }); - } - if (embeds.length === 0) return "I couldn't find any tags!"; - return paginator(this.client, this.message, embeds); - case "random": - return tags[random(Object.keys(tags))].content; - default: - if (!tags[this.args[0].toLowerCase()]) return "This tag doesn't exist!"; - return tags[this.args[0].toLowerCase()].content; + } + }); + } + if (embeds.length === 0) return "I couldn't find any tags!"; + return paginator(this.client, this.message, embeds); + } else if (this.args[0].toLowerCase() === "random") { + const tagList = await database.getTags(this.message.channel.guild.id); + return tagList[random(Object.keys(tagList))].content; + } else { + const getResult = await database.getTag(this.message.channel.guild.id, this.args[0].toLowerCase()); + if (!getResult) return "This tag doesn't exist!"; + return getResult.content; } } diff --git a/utils/database/dummy.js b/utils/database/dummy.js index d5167e7..336eea2 100644 --- a/utils/database/dummy.js +++ b/utils/database/dummy.js @@ -16,6 +16,7 @@ exports.enableCommand = async () => {}; exports.disableChannel = async () => {}; exports.enableChannel = async () => {}; exports.getTags = async () => {}; +exports.getTag = async () => {}; exports.setTag = async () => {}; exports.removeTag = async () => {}; exports.editTag = async () => {}; diff --git a/utils/database/postgresql.js b/utils/database/postgresql.js index 617e8ee..c61cfd7 100644 --- a/utils/database/postgresql.js +++ b/utils/database/postgresql.js @@ -16,6 +16,11 @@ exports.setPrefix = async (prefix, guild) => { collections.prefixCache.set(guild.id, prefix); }; +exports.getTag = async (guild, tag) => { + const tagResult = (await connection.query("SELECT * FROM tags WHERE guild_id = $1 AND name = $2", [guild, tag])).rows; + return tagResult[0] ? { content: tagResult[0].content, author: tagResult[0].author } : undefined; +}; + exports.getTags = async (guild) => { const tagArray = (await connection.query("SELECT * FROM tags WHERE guild_id = $1", [guild])).rows; const tags = {}; @@ -85,7 +90,11 @@ exports.addCount = async (command) => { exports.addGuild = async (guild) => { const query = await this.getGuild(guild); if (query) return query; - await connection.query("INSERT INTO guilds (guild_id, prefix, disabled, disabled_commands) VALUES ($1, $2, $3, $4)", [guild.id, process.env.PREFIX, [], []]); + try { + await connection.query("INSERT INTO guilds (guild_id, prefix, disabled, disabled_commands) VALUES ($1, $2, $3, $4)", [guild.id, process.env.PREFIX, [], []]); + } catch (e) { + logger.error(`Failed to register guild ${guild.id}: ${e}`); + } return await this.getGuild(guild.id); }; diff --git a/utils/database/sqlite.js b/utils/database/sqlite.js index 8013412..5d11682 100644 --- a/utils/database/sqlite.js +++ b/utils/database/sqlite.js @@ -97,6 +97,11 @@ exports.enableChannel = async (channel) => { collections.disabledCache.set(channel.guild.id, newDisabled); }; +exports.getTag = async (guild, tag) => { + const tagResult = connection.prepare("SELECT * FROM tags WHERE guild_id = ? AND name = ?").get(guild, tag); + return tagResult ? { content: tagResult.content, author: tagResult.author } : undefined; +}; + exports.getTags = async (guild) => { const tagArray = connection.prepare("SELECT * FROM tags WHERE guild_id = ?").all(guild); const tags = {}; diff --git a/utils/soundplayer.js b/utils/soundplayer.js index 2ad6762..9a00e55 100644 --- a/utils/soundplayer.js +++ b/utils/soundplayer.js @@ -89,7 +89,7 @@ exports.play = async (client, sound, message, music = false) => { }; exports.nextSong = async (client, message, connection, track, info, music, voiceChannel, loop = false, inQueue = false, lastTrack = null) => { - this.skipVotes.set(this.message.channel.guild.id, { count: 0, ids: [] }); + this.skipVotes.set(voiceChannel.guild.id, { count: 0, ids: [] }); const parts = Math.floor((0 / info.length) * 10); let playingMessage; if (!music && this.players.get(voiceChannel.guild.id)) {