fixed tags, there are no "guilds" in matrix

This commit is contained in:
murm 2023-03-17 01:19:50 -04:00
parent e9d6a0fe48
commit 0d6e3b349d
2 changed files with 41 additions and 42 deletions

View File

@ -17,7 +17,7 @@ class TagsCommand extends Command {
if (blacklist.includes(tagName)) return "You can't make a tag with that name!"; if (blacklist.includes(tagName)) return "You can't make a tag with that name!";
const getResult = await database.getTag(this.channel, tagName); const getResult = await database.getTag(this.channel, tagName);
if (getResult) return "This tag already exists!"; 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; this.success = true;
if (result) return result; if (result) return result;
return `The tag \`${tagName}\` has been added!`; return `The tag \`${tagName}\` has been added!`;
@ -27,7 +27,7 @@ class TagsCommand extends Command {
if (!getResult) return "This tag doesn't exist!"; if (!getResult) return "This tag doesn't exist!";
const owners = process.env.OWNER.split(","); const owners = process.env.OWNER.split(",");
if (getResult.author !== this.author && !owners.includes(this.author)) return "You don't own this tag!"; 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; this.success = true;
return `The tag \`${tagName}\` has been deleted!`; return `The tag \`${tagName}\` has been deleted!`;
} else if (cmd === "edit") { } else if (cmd === "edit") {
@ -36,7 +36,7 @@ class TagsCommand extends Command {
if (!getResult) return "This tag doesn't exist!"; if (!getResult) return "This tag doesn't exist!";
const owners = process.env.OWNER.split(","); const owners = process.env.OWNER.split(",");
if (getResult.author !== this.author && !owners.includes(this.author)) return "You don't own this tag!"; 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; this.success = true;
return `The tag \`${tagName}\` has been edited!`; return `The tag \`${tagName}\` has been edited!`;
} else if (cmd === "own" || cmd === "owner") { } 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}\`).`; return `This tag is owned by **${user.username}#${user.discriminator}** (\`${getResult.author}\`).`;
} }
} else if (cmd === "list") { } 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 tagList = await database.getTags(this.channel);
const embeds = []; const embeds = [];
const groups = Object.keys(tagList).map((item, index) => { const groups = Object.keys(tagList).map((item, index) => {

View File

@ -4,7 +4,7 @@ import sqlite3 from "better-sqlite3";
const connection = sqlite3(process.env.DB.replace("sqlite://", "")); const connection = sqlite3(process.env.DB.replace("sqlite://", ""));
const schema = ` const schema = `
CREATE TABLE guilds ( CREATE TABLE channels (
guild_id VARCHAR(30) NOT NULL PRIMARY KEY, guild_id VARCHAR(30) NOT NULL PRIMARY KEY,
prefix VARCHAR(15) NOT NULL, prefix VARCHAR(15) NOT NULL,
disabled text NOT NULL, disabled text NOT NULL,
@ -31,8 +31,8 @@ INSERT INTO settings (id) VALUES (1);
const updates = [ const updates = [
"", // reserved "", // reserved
"ALTER TABLE guilds ADD COLUMN accessed int", "ALTER TABLE channels ADD COLUMN accessed int",
"ALTER TABLE guilds DROP COLUMN accessed", "ALTER TABLE channels DROP COLUMN accessed",
`CREATE TABLE settings ( `CREATE TABLE settings (
id smallint PRIMARY KEY, id smallint PRIMARY KEY,
broadcast VARCHAR, broadcast VARCHAR,
@ -103,39 +103,39 @@ export async function getCounts() {
return countObject; return countObject;
} }
export async function disableCommand(guild, command) { export async function disableCommand(channel, command) {
const guildDB = await this.getGuild(guild); const channelDB = await this.getGuild(channel);
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); 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(guild, guildDB.disabled_commands ? [...JSON.parse(guildDB.disabledCommands), command] : [command].filter((v) => !!v)); disabledCmdCache.set(channel, channelDB.disabled_commands ? [...JSON.parse(channelDB.disabledCommands), command] : [command].filter((v) => !!v));
} }
export async function enableCommand(guild, command) { export async function enableCommand(channel, command) {
const guildDB = await this.getGuild(guild); const channelDB = await this.getGuild(channel);
const newDisabled = guildDB.disabledCommands ? JSON.parse(guildDB.disabledCommands).filter(item => item !== command) : []; const newDisabled = channelDB.disabledCommands ? JSON.parse(channelDB.disabledCommands).filter(item => item !== command) : [];
connection.prepare("UPDATE guilds SET disabled_commands = ? WHERE guild_id = ?").run(JSON.stringify(newDisabled), guild); connection.prepare("UPDATE channels SET disabled_commands = ? WHERE guild_id = ?").run(JSON.stringify(newDisabled), channel);
disabledCmdCache.set(guild, newDisabled); disabledCmdCache.set(channel, newDisabled);
} }
export async function disableChannel(channel) { export async function disableChannel(channel) {
const guildDB = await this.getGuild(channel.guildID); const channelDB = await this.getGuild(channel.channelID);
connection.prepare("UPDATE guilds SET disabled = ? WHERE guild_id = ?").run(JSON.stringify([...JSON.parse(guildDB.disabled), channel.id]), channel.guildID); connection.prepare("UPDATE channels SET disabled = ? WHERE guild_id = ?").run(JSON.stringify([...JSON.parse(channelDB.disabled), channel]), channel.channelID);
disabledCache.set(channel.guildID, [...JSON.parse(guildDB.disabled), channel.id]); disabledCache.set(channel.channelID, [...JSON.parse(channelDB.disabled), channel]);
} }
export async function enableChannel(channel) { export async function enableChannel(channel) {
const guildDB = await this.getGuild(channel.guildID); const channelDB = await this.getGuild(channel.channelID);
const newDisabled = JSON.parse(guildDB.disabled).filter(item => item !== channel.id); const newDisabled = JSON.parse(channelDB.disabled).filter(item => item !== channel);
connection.prepare("UPDATE guilds SET disabled = ? WHERE guild_id = ?").run(JSON.stringify(newDisabled), channel.guildID); connection.prepare("UPDATE channels SET disabled = ? WHERE guild_id = ?").run(JSON.stringify(newDisabled), channel.channelID);
disabledCache.set(channel.guildID, newDisabled); disabledCache.set(channel.channelID, newDisabled);
} }
export async function getTag(guild, tag) { export async function getTag(channel, tag) {
const tagResult = connection.prepare("SELECT * FROM tags WHERE guild_id = ? AND name = ?").get(guild, 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; return tagResult ? { content: tagResult.content, author: tagResult.author } : undefined;
} }
export async function getTags(guild) { export async function getTags(channel) {
const tagArray = connection.prepare("SELECT * FROM tags WHERE guild_id = ?").all(guild); const tagArray = connection.prepare("SELECT * FROM tags WHERE guild_id = ?").all(channel);
const tags = {}; const tags = {};
if (!tagArray) return []; if (!tagArray) return [];
for (const tag of tagArray) { for (const tag of tagArray) {
@ -144,9 +144,9 @@ export async function getTags(guild) {
return tags; return tags;
} }
export async function setTag(name, content, guild) { export async function setTag(name, content, channel) {
const tag = { const tag = {
id: guild.id, id: channel,
name: name, name: name,
content: content.content, content: content.content,
author: content.author 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); connection.prepare("INSERT INTO tags (guild_id, name, content, author) VALUES (@id, @name, @content, @author)").run(tag);
} }
export async function removeTag(name, guild) { export async function removeTag(name, channel) {
connection.prepare("DELETE FROM tags WHERE guild_id = ? AND name = ?").run(guild.id, name); connection.prepare("DELETE FROM tags WHERE guild_id = ? AND name = ?").run(channel, name);
} }
export async function editTag(name, content, guild) { export async function editTag(name, content, channel) {
connection.prepare("UPDATE tags SET content = ?, author = ? WHERE guild_id = ? AND name = ?").run(content.content, content.author, guild.id, name); connection.prepare("UPDATE tags SET content = ?, author = ? WHERE guild_id = ? AND name = ?").run(content.content, content.author, channel, name);
} }
export async function setBroadcast(msg) { export async function setBroadcast(msg) {
@ -171,24 +171,24 @@ export async function getBroadcast() {
return result[0].broadcast; return result[0].broadcast;
} }
export async function setPrefix(prefix, guild) { export async function setPrefix(prefix, channel) {
connection.prepare("UPDATE guilds SET prefix = ? WHERE guild_id = ?").run(prefix, guild.id); connection.prepare("UPDATE channels SET prefix = ? WHERE guild_id = ?").run(prefix, channel);
prefixCache.set(guild.id, prefix); prefixCache.set(channel, prefix);
} }
export async function getGuild(query) { export async function getGuild(query) {
let guild; let channel;
connection.transaction(() => { connection.transaction(() => {
guild = connection.prepare("SELECT * FROM guilds WHERE guild_id = ?").get(query); channel = connection.prepare("SELECT * FROM channels WHERE guild_id = ?").get(query);
if (!guild) { if (!channel) {
guild = { channel = {
id: query, id: query,
prefix: process.env.PREFIX, prefix: process.env.PREFIX,
disabled: "[]", disabled: "[]",
disabledCommands: "[]" 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;
} }