From 0d6e3b349df5218d19e90ce0410e6cfd59b428ee Mon Sep 17 00:00:00 2001 From: murm Date: Fri, 17 Mar 2023 01:19:50 -0400 Subject: [PATCH] fixed tags, there are no "guilds" in matrix --- commands/tags/tags.js | 7 ++-- utils/database/sqlite.js | 76 ++++++++++++++++++++-------------------- 2 files changed, 41 insertions(+), 42 deletions(-) diff --git a/commands/tags/tags.js b/commands/tags/tags.js index f9d6364..83dd734 100644 --- a/commands/tags/tags.js +++ b/commands/tags/tags.js @@ -17,7 +17,7 @@ class TagsCommand extends Command { if (blacklist.includes(tagName)) return "You can't make a tag with that name!"; const getResult = await database.getTag(this.channel, tagName); if (getResult) return "This tag already exists!"; - const result = await database.setTag(tagName, { content: this.type === "classic" ? this.args.slice(2).join(" ") : this.optionsArray[0].options[1].value, author: this.member.id }, this.guild); + const result = await database.setTag(tagName, { content: this.type === "classic" ? this.args.slice(2).join(" ") : this.optionsArray[0].options[1].value, author: this.message.sender }, this.channel); this.success = true; if (result) return result; return `The tag \`${tagName}\` has been added!`; @@ -27,7 +27,7 @@ class TagsCommand extends Command { if (!getResult) return "This tag doesn't exist!"; const owners = process.env.OWNER.split(","); if (getResult.author !== this.author && !owners.includes(this.author)) return "You don't own this tag!"; - await database.removeTag(tagName, this.guild); + await database.removeTag(tagName, this.channel); this.success = true; return `The tag \`${tagName}\` has been deleted!`; } else if (cmd === "edit") { @@ -36,7 +36,7 @@ class TagsCommand extends Command { if (!getResult) return "This tag doesn't exist!"; const owners = process.env.OWNER.split(","); if (getResult.author !== this.author && !owners.includes(this.author)) return "You don't own this tag!"; - await database.editTag(tagName, { content: this.type === "classic" ? this.args.slice(2).join(" ") : this.optionsArray[0].options[1].value, author: this.member.id }, this.guild); + await database.editTag(tagName, { content: this.type === "classic" ? this.args.slice(2).join(" ") : this.optionsArray[0].options[1].value, author: this.message.sender }, this.channel); this.success = true; return `The tag \`${tagName}\` has been edited!`; } else if (cmd === "own" || cmd === "owner") { @@ -56,7 +56,6 @@ class TagsCommand extends Command { return `This tag is owned by **${user.username}#${user.discriminator}** (\`${getResult.author}\`).`; } } else if (cmd === "list") { - if (!this.channel.permissionsOf(this.client.user.id.toString()).has("EMBED_LINKS")) return "I don't have the `Embed Links` permission!"; const tagList = await database.getTags(this.channel); const embeds = []; const groups = Object.keys(tagList).map((item, index) => { diff --git a/utils/database/sqlite.js b/utils/database/sqlite.js index 2b9d043..6efb61e 100644 --- a/utils/database/sqlite.js +++ b/utils/database/sqlite.js @@ -4,7 +4,7 @@ import sqlite3 from "better-sqlite3"; const connection = sqlite3(process.env.DB.replace("sqlite://", "")); const schema = ` -CREATE TABLE guilds ( +CREATE TABLE channels ( guild_id VARCHAR(30) NOT NULL PRIMARY KEY, prefix VARCHAR(15) NOT NULL, disabled text NOT NULL, @@ -31,8 +31,8 @@ INSERT INTO settings (id) VALUES (1); const updates = [ "", // reserved - "ALTER TABLE guilds ADD COLUMN accessed int", - "ALTER TABLE guilds DROP COLUMN accessed", + "ALTER TABLE channels ADD COLUMN accessed int", + "ALTER TABLE channels DROP COLUMN accessed", `CREATE TABLE settings ( id smallint PRIMARY KEY, broadcast VARCHAR, @@ -103,39 +103,39 @@ export async function getCounts() { return countObject; } -export async function disableCommand(guild, command) { - const guildDB = await this.getGuild(guild); - connection.prepare("UPDATE guilds SET disabled_commands = ? WHERE guild_id = ?").run(JSON.stringify((guildDB.disabledCommands ? [...JSON.parse(guildDB.disabledCommands), command] : [command]).filter((v) => !!v)), guild); - disabledCmdCache.set(guild, guildDB.disabled_commands ? [...JSON.parse(guildDB.disabledCommands), command] : [command].filter((v) => !!v)); +export async function disableCommand(channel, command) { + const channelDB = await this.getGuild(channel); + connection.prepare("UPDATE channels SET disabled_commands = ? WHERE guild_id = ?").run(JSON.stringify((channelDB.disabledCommands ? [...JSON.parse(channelDB.disabledCommands), command] : [command]).filter((v) => !!v)), channel); + disabledCmdCache.set(channel, channelDB.disabled_commands ? [...JSON.parse(channelDB.disabledCommands), command] : [command].filter((v) => !!v)); } -export async function enableCommand(guild, command) { - const guildDB = await this.getGuild(guild); - const newDisabled = guildDB.disabledCommands ? JSON.parse(guildDB.disabledCommands).filter(item => item !== command) : []; - connection.prepare("UPDATE guilds SET disabled_commands = ? WHERE guild_id = ?").run(JSON.stringify(newDisabled), guild); - disabledCmdCache.set(guild, newDisabled); +export async function enableCommand(channel, command) { + const channelDB = await this.getGuild(channel); + const newDisabled = channelDB.disabledCommands ? JSON.parse(channelDB.disabledCommands).filter(item => item !== command) : []; + connection.prepare("UPDATE channels SET disabled_commands = ? WHERE guild_id = ?").run(JSON.stringify(newDisabled), channel); + disabledCmdCache.set(channel, newDisabled); } export async function disableChannel(channel) { - const guildDB = await this.getGuild(channel.guildID); - connection.prepare("UPDATE guilds SET disabled = ? WHERE guild_id = ?").run(JSON.stringify([...JSON.parse(guildDB.disabled), channel.id]), channel.guildID); - disabledCache.set(channel.guildID, [...JSON.parse(guildDB.disabled), channel.id]); + const channelDB = await this.getGuild(channel.channelID); + connection.prepare("UPDATE channels SET disabled = ? WHERE guild_id = ?").run(JSON.stringify([...JSON.parse(channelDB.disabled), channel]), channel.channelID); + disabledCache.set(channel.channelID, [...JSON.parse(channelDB.disabled), channel]); } export async function enableChannel(channel) { - const guildDB = await this.getGuild(channel.guildID); - const newDisabled = JSON.parse(guildDB.disabled).filter(item => item !== channel.id); - connection.prepare("UPDATE guilds SET disabled = ? WHERE guild_id = ?").run(JSON.stringify(newDisabled), channel.guildID); - disabledCache.set(channel.guildID, newDisabled); + const channelDB = await this.getGuild(channel.channelID); + const newDisabled = JSON.parse(channelDB.disabled).filter(item => item !== channel); + connection.prepare("UPDATE channels SET disabled = ? WHERE guild_id = ?").run(JSON.stringify(newDisabled), channel.channelID); + disabledCache.set(channel.channelID, newDisabled); } -export async function getTag(guild, tag) { - const tagResult = connection.prepare("SELECT * FROM tags WHERE guild_id = ? AND name = ?").get(guild, tag); +export async function getTag(channel, tag) { + const tagResult = connection.prepare("SELECT * FROM tags WHERE guild_id = ? AND name = ?").get(channel, tag); return tagResult ? { content: tagResult.content, author: tagResult.author } : undefined; } -export async function getTags(guild) { - const tagArray = connection.prepare("SELECT * FROM tags WHERE guild_id = ?").all(guild); +export async function getTags(channel) { + const tagArray = connection.prepare("SELECT * FROM tags WHERE guild_id = ?").all(channel); const tags = {}; if (!tagArray) return []; for (const tag of tagArray) { @@ -144,9 +144,9 @@ export async function getTags(guild) { return tags; } -export async function setTag(name, content, guild) { +export async function setTag(name, content, channel) { const tag = { - id: guild.id, + id: channel, name: name, content: content.content, author: content.author @@ -154,12 +154,12 @@ export async function setTag(name, content, guild) { connection.prepare("INSERT INTO tags (guild_id, name, content, author) VALUES (@id, @name, @content, @author)").run(tag); } -export async function removeTag(name, guild) { - connection.prepare("DELETE FROM tags WHERE guild_id = ? AND name = ?").run(guild.id, name); +export async function removeTag(name, channel) { + connection.prepare("DELETE FROM tags WHERE guild_id = ? AND name = ?").run(channel, name); } -export async function editTag(name, content, guild) { - connection.prepare("UPDATE tags SET content = ?, author = ? WHERE guild_id = ? AND name = ?").run(content.content, content.author, guild.id, name); +export async function editTag(name, content, channel) { + connection.prepare("UPDATE tags SET content = ?, author = ? WHERE guild_id = ? AND name = ?").run(content.content, content.author, channel, name); } export async function setBroadcast(msg) { @@ -171,24 +171,24 @@ export async function getBroadcast() { return result[0].broadcast; } -export async function setPrefix(prefix, guild) { - connection.prepare("UPDATE guilds SET prefix = ? WHERE guild_id = ?").run(prefix, guild.id); - prefixCache.set(guild.id, prefix); +export async function setPrefix(prefix, channel) { + connection.prepare("UPDATE channels SET prefix = ? WHERE guild_id = ?").run(prefix, channel); + prefixCache.set(channel, prefix); } export async function getGuild(query) { - let guild; + let channel; connection.transaction(() => { - guild = connection.prepare("SELECT * FROM guilds WHERE guild_id = ?").get(query); - if (!guild) { - guild = { + channel = connection.prepare("SELECT * FROM channels WHERE guild_id = ?").get(query); + if (!channel) { + channel = { id: query, prefix: process.env.PREFIX, disabled: "[]", disabledCommands: "[]" }; - connection.prepare("INSERT INTO guilds (guild_id, prefix, disabled, disabled_commands) VALUES (@id, @prefix, @disabled, @disabledCommands)").run(guild); + connection.prepare("INSERT INTO channels (guild_id, prefix, disabled, disabled_commands) VALUES (@id, @prefix, @disabled, @disabledCommands)").run(channel); } })(); - return guild; + return channel; }