Move tags to separate table
This commit is contained in:
		
							parent
							
								
									81277f95c0
								
							
						
					
					
						commit
						a75ceb41f2
					
				
					 7 changed files with 62 additions and 73 deletions
				
			
		| 
						 | 
					@ -11,7 +11,7 @@ class TagsCommand extends Command {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if ((guild.tagsDisabled || guild.tags_disabled) && this.args[0].toLowerCase() !== ("enable" || "disable")) return;
 | 
					    if ((guild.tagsDisabled || guild.tags_disabled) && this.args[0].toLowerCase() !== ("enable" || "disable")) return;
 | 
				
			||||||
    if (this.args.length === 0) return "You need to provide the name of the tag you want to view!";
 | 
					    if (this.args.length === 0) return "You need to provide the name of the tag you want to view!";
 | 
				
			||||||
    const tags = guild.tags instanceof Map ? Object.fromEntries(guild.tags) : typeof guild.tags === "string" ? JSON.parse(guild.tags) : guild.tags;
 | 
					    const tags = await database(this.ipc, "getTags", this.message.channel.guild.id);
 | 
				
			||||||
    const blacklist = ["create", "add", "edit", "remove", "delete", "list", "random", "own", "owner", "enable", "disable"];
 | 
					    const blacklist = ["create", "add", "edit", "remove", "delete", "list", "random", "own", "owner", "enable", "disable"];
 | 
				
			||||||
    switch (this.args[0].toLowerCase()) {
 | 
					    switch (this.args[0].toLowerCase()) {
 | 
				
			||||||
      case "create":
 | 
					      case "create":
 | 
				
			||||||
| 
						 | 
					@ -19,7 +19,7 @@ class TagsCommand extends Command {
 | 
				
			||||||
        if (this.args[1] === undefined) return "You need to provide the name of the tag you want to 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 (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!";
 | 
					        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, guild);
 | 
					        var result = await this.setTag(this.args.slice(2).join(" "), this.args[1].toLowerCase(), this.message);
 | 
				
			||||||
        if (result) return result;
 | 
					        if (result) return result;
 | 
				
			||||||
        return `The tag \`${this.args[1].toLowerCase()}\` has been added!`;
 | 
					        return `The tag \`${this.args[1].toLowerCase()}\` has been added!`;
 | 
				
			||||||
      case "delete":
 | 
					      case "delete":
 | 
				
			||||||
| 
						 | 
					@ -33,7 +33,7 @@ class TagsCommand extends Command {
 | 
				
			||||||
        if (this.args[1] === undefined) return "You need to provide the name of the tag you want to 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()]) 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!";
 | 
					        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, guild);
 | 
					        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!`;
 | 
					        return `The tag \`${this.args[1].toLowerCase()}\` has been edited!`;
 | 
				
			||||||
      case "own":
 | 
					      case "own":
 | 
				
			||||||
      case "owner":
 | 
					      case "owner":
 | 
				
			||||||
| 
						 | 
					@ -83,14 +83,14 @@ class TagsCommand extends Command {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async setTag(content, name, message) {
 | 
					  async setTag(content, name, message, edit = false) {
 | 
				
			||||||
    if ((!content || content.length === 0) && message.attachments.length === 0) return "You need to provide the content of the tag!";
 | 
					    if ((!content || content.length === 0) && message.attachments.length === 0) return "You need to provide the content of the tag!";
 | 
				
			||||||
    if (message.attachments.length !== 0 && content) {
 | 
					    if (message.attachments.length !== 0 && content) {
 | 
				
			||||||
      await database(this.ipc, "setTag", name, { content: `${content} ${message.attachments[0].url}`, author: message.author.id }, message.channel.guild);
 | 
					      await database(this.ipc, edit ? "editTag" : "setTag", name, { content: `${content} ${message.attachments[0].url}`, author: message.author.id }, message.channel.guild);
 | 
				
			||||||
    } else if (message.attachments.length !== 0) {
 | 
					    } else if (message.attachments.length !== 0) {
 | 
				
			||||||
      await database(this.ipc, "setTag", name, { content: message.attachments[0].url, author: message.author.id }, message.channel.guild);
 | 
					      await database(this.ipc, edit ? "editTag" : "setTag", name, { content: message.attachments[0].url, author: message.author.id }, message.channel.guild);
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      await database(this.ipc, "setTag", name, { content: content, author: message.author.id }, message.channel.guild);
 | 
					      await database(this.ipc, edit ? "editTag" : "setTag", name, { content: content, author: message.author.id }, message.channel.guild);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return;
 | 
					    return;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,56 +0,0 @@
 | 
				
			||||||
//require("dotenv").config();
 | 
					 | 
				
			||||||
const { Pool } = require("pg");
 | 
					 | 
				
			||||||
const pool = new Pool({
 | 
					 | 
				
			||||||
  connectionString: process.env.DB
 | 
					 | 
				
			||||||
});
 | 
					 | 
				
			||||||
const mongoose = require("mongoose");
 | 
					 | 
				
			||||||
mongoose.connect(process.env.MONGO, { poolSize: 10, bufferMaxEntries: 0, useNewUrlParser: true, useUnifiedTopology: true });
 | 
					 | 
				
			||||||
const guildSchema = new mongoose.Schema({
 | 
					 | 
				
			||||||
  id: String,
 | 
					 | 
				
			||||||
  tags: Map,
 | 
					 | 
				
			||||||
  prefix: String,
 | 
					 | 
				
			||||||
  disabled: [String],
 | 
					 | 
				
			||||||
  tagsDisabled: Boolean
 | 
					 | 
				
			||||||
});
 | 
					 | 
				
			||||||
const Guild = mongoose.model("Guild", guildSchema);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
const globalSchema = new mongoose.Schema({
 | 
					 | 
				
			||||||
  cmdCounts: Map
 | 
					 | 
				
			||||||
});
 | 
					 | 
				
			||||||
const Global = mongoose.model("Global", globalSchema);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
(async () => {
 | 
					 | 
				
			||||||
  console.log("Migrating guilds...");
 | 
					 | 
				
			||||||
  const guilds = await Guild.find();
 | 
					 | 
				
			||||||
  try {
 | 
					 | 
				
			||||||
    await pool.query("CREATE TABLE guilds ( guild_id VARCHAR(30) NOT NULL, tags json NOT NULL, prefix VARCHAR(15) NOT NULL, warns json NOT NULL, disabled text ARRAY NOT NULL, tags_disabled boolean NOT NULL )");
 | 
					 | 
				
			||||||
  } catch (e) {
 | 
					 | 
				
			||||||
    console.error(`Skipping table creation due to error: ${e}`);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  for (const guild of guilds) {
 | 
					 | 
				
			||||||
    if ((await pool.query("SELECT * FROM guilds WHERE guild_id = $1", [guild.id])).rows.length !== 0) {
 | 
					 | 
				
			||||||
      await pool.query("UPDATE guilds SET tags = $1, prefix = $2, disabled = $3, tags_disabled = $4 WHERE guild_id = $5", [guild.tags, guild.prefix.substring(0, 15), guild.disabled ? guild.disabled : guild.disabledChannels, guild.tagsDisabled === undefined ? false : guild.tagsDisabled, guild.id]);
 | 
					 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
      await pool.query("INSERT INTO guilds (guild_id, tags, prefix, disabled, tags_disabled) VALUES ($1, $2, $3, $4, $5)", [guild.id, guild.tags, guild.prefix.substring(0, 15), guild.disabled ? guild.disabled : guild.disabledChannels, guild.tagsDisabled === undefined ? false : guild.tagsDisabled]);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    console.log(`Migrated guild with ID ${guild.id}`);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  console.log("Migrating command counts...");
 | 
					 | 
				
			||||||
  const global = await Global.findOne();
 | 
					 | 
				
			||||||
  try {
 | 
					 | 
				
			||||||
    await pool.query("CREATE TABLE counts ( command VARCHAR NOT NULL, count integer NOT NULL )");
 | 
					 | 
				
			||||||
  } catch (e) {
 | 
					 | 
				
			||||||
    console.error(`Skipping table creation due to error: ${e}`);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  console.log(global);
 | 
					 | 
				
			||||||
  for (const [key, value] of global.cmdCounts) {
 | 
					 | 
				
			||||||
    if ((await pool.query("SELECT * FROM counts WHERE command = $1", [key])).rows.length !== 0) {
 | 
					 | 
				
			||||||
      await pool.query("UPDATE counts SET count = $1 WHERE command = $2", [value, key]);
 | 
					 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
      await pool.query("INSERT INTO counts (command, count) VALUES ($1, $2)", [key, value]);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    console.log(`Migrated counts for command ${key}`);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  console.log("Done!");
 | 
					 | 
				
			||||||
  return process.exit(0);
 | 
					 | 
				
			||||||
})();
 | 
					 | 
				
			||||||
							
								
								
									
										27
									
								
								utils/converttags.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								utils/converttags.js
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,27 @@
 | 
				
			||||||
 | 
					require("dotenv").config();
 | 
				
			||||||
 | 
					const { Pool } = require("pg");
 | 
				
			||||||
 | 
					const pool = new Pool({
 | 
				
			||||||
 | 
					  connectionString: process.env.DB
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(async () => {
 | 
				
			||||||
 | 
					  console.log("Migrating tags...");
 | 
				
			||||||
 | 
					  const guilds = (await pool.query("SELECT * FROM guilds")).rows;
 | 
				
			||||||
 | 
					  try {
 | 
				
			||||||
 | 
					    await pool.query("CREATE TABLE tags ( guild_id VARCHAR(30) NOT NULL, name text NOT NULL, content text NOT NULL, author VARCHAR(30) NOT NULL, UNIQUE(guild_id, name) )");
 | 
				
			||||||
 | 
					  } catch (e) {
 | 
				
			||||||
 | 
					    console.error(`Skipping table creation due to error: ${e}`);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  for (const guild of guilds) {
 | 
				
			||||||
 | 
					    for (const [name, value] of Object.entries(guild.tags)) {
 | 
				
			||||||
 | 
					      if ((await pool.query("SELECT * FROM tags WHERE guild_id = $1 AND name = $2", [guild.guild_id, name])).rows.length !== 0) {
 | 
				
			||||||
 | 
					        await pool.query("UPDATE tags SET content = $1, author = $2 WHERE guild_id = $3 AND name = $4", [value.content, value.author, guild.guild_id, name]);
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        await pool.query("INSERT INTO tags (guild_id, name, content, author) VALUES ($1, $2, $3, $4)", [guild.guild_id, name, value.content, value.author]);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      console.log(`Migrated tag ${name} in guild ${guild.guild_id}`);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  console.log("Done!");
 | 
				
			||||||
 | 
					  return process.exit(0);
 | 
				
			||||||
 | 
					})();
 | 
				
			||||||
| 
						 | 
					@ -14,8 +14,10 @@ exports.getCounts = async () => {
 | 
				
			||||||
exports.disableChannel = async () => {};
 | 
					exports.disableChannel = async () => {};
 | 
				
			||||||
exports.enableChannel = async () => {};
 | 
					exports.enableChannel = async () => {};
 | 
				
			||||||
exports.toggleTags = async () => {};
 | 
					exports.toggleTags = async () => {};
 | 
				
			||||||
 | 
					exports.getTags = async () => {};
 | 
				
			||||||
exports.setTag = async () => {};
 | 
					exports.setTag = async () => {};
 | 
				
			||||||
exports.removeTag = async () => {};
 | 
					exports.removeTag = async () => {};
 | 
				
			||||||
 | 
					exports.editTag = async () => {};
 | 
				
			||||||
exports.setPrefix = async () => {};
 | 
					exports.setPrefix = async () => {};
 | 
				
			||||||
exports.addGuild = async (guild) => {
 | 
					exports.addGuild = async (guild) => {
 | 
				
			||||||
  return {
 | 
					  return {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,5 @@
 | 
				
			||||||
const collections = require("../collections.js");
 | 
					const collections = require("../collections.js");
 | 
				
			||||||
const logger = require("../logger.js");
 | 
					const logger = require("../logger.js");
 | 
				
			||||||
const misc = require("../misc.js");
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
const { Pool } = require("pg");
 | 
					const { Pool } = require("pg");
 | 
				
			||||||
const connection = new Pool({
 | 
					const connection = new Pool({
 | 
				
			||||||
| 
						 | 
					@ -17,16 +16,25 @@ exports.setPrefix = async (prefix, guild) => {
 | 
				
			||||||
  collections.prefixCache.set(guild.id, prefix);
 | 
					  collections.prefixCache.set(guild.id, prefix);
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					exports.getTags = async (guild) => {
 | 
				
			||||||
 | 
					  const tagArray = (await connection.query("SELECT * FROM tags WHERE guild_id = $1", [guild])).rows;
 | 
				
			||||||
 | 
					  const tags = {};
 | 
				
			||||||
 | 
					  for (const tag of tagArray) {
 | 
				
			||||||
 | 
					    tags[tag.name] = { content: tag.content, author: tag.author };
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  return tags;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
exports.setTag = async (name, content, guild) => {
 | 
					exports.setTag = async (name, content, guild) => {
 | 
				
			||||||
  const guildDB = await this.getGuild(guild.id);
 | 
					  await connection.query("INSERT INTO tags (guild_id, name, content, author) VALUES ($1, $2, $3, $4)", [guild.id, name, content.content, content.author]);
 | 
				
			||||||
  guildDB.tags[name] = content;
 | 
					};
 | 
				
			||||||
  await connection.query("UPDATE guilds SET tags = $1 WHERE guild_id = $2", [guildDB.tags, guild.id]);
 | 
					
 | 
				
			||||||
 | 
					exports.editTag = async (name, content, guild) => {
 | 
				
			||||||
 | 
					  await connection.query("UPDATE tags SET content = $1, author = $2 WHERE guild_id = $3 AND name = $4", [content.content, content.author, guild.id, name]);
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
exports.removeTag = async (name, guild) => {
 | 
					exports.removeTag = async (name, guild) => {
 | 
				
			||||||
  const guildDB = await this.getGuild(guild.id);
 | 
					  await connection.query("DELETE FROM tags WHERE guild_id = $1 AND name = $2", [guild.id, name]);
 | 
				
			||||||
  delete guildDB.tags[name];
 | 
					 | 
				
			||||||
  await connection.query("UPDATE guilds SET tags = $1 WHERE guild_id = $2", [guildDB.tags, guild.id]);
 | 
					 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
exports.toggleTags = async (guild) => {
 | 
					exports.toggleTags = async (guild) => {
 | 
				
			||||||
| 
						 | 
					@ -71,7 +79,7 @@ exports.addCount = async (command) => {
 | 
				
			||||||
exports.addGuild = async (guild) => {
 | 
					exports.addGuild = async (guild) => {
 | 
				
			||||||
  const query = await this.getGuild(guild);
 | 
					  const query = await this.getGuild(guild);
 | 
				
			||||||
  if (query) return query;
 | 
					  if (query) return query;
 | 
				
			||||||
  await connection.query("INSERT INTO guilds (guild_id, tags, prefix, disabled, tags_disabled) VALUES ($1, $2, $3, $4, $5)", [guild.id, misc.tagDefaults, process.env.PREFIX, [], false]);
 | 
					  await connection.query("INSERT INTO guilds (guild_id, prefix, disabled, tags_disabled) VALUES ($1, $2, $3, $4)", [guild.id, process.env.PREFIX, [], false]);
 | 
				
			||||||
  return await this.getGuild(guild.id);
 | 
					  return await this.getGuild(guild.id);
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -85,6 +85,11 @@ exports.toggleTags = async (guild) => {
 | 
				
			||||||
  connection.prepare("UPDATE guilds SET tags_disabled = ? WHERE guild_id = ?").run(guildDB.tags_disabled, guild.id);
 | 
					  connection.prepare("UPDATE guilds SET tags_disabled = ? WHERE guild_id = ?").run(guildDB.tags_disabled, guild.id);
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					exports.getTags = async (name, content, guild) => {
 | 
				
			||||||
 | 
					  const guildDB = await this.getGuild(guild.id);
 | 
				
			||||||
 | 
					  return JSON.parse(guildDB.tags);
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
exports.setTag = async (name, content, guild) => {
 | 
					exports.setTag = async (name, content, guild) => {
 | 
				
			||||||
  const guildDB = await this.getGuild(guild.id);
 | 
					  const guildDB = await this.getGuild(guild.id);
 | 
				
			||||||
  const tags = JSON.parse(guildDB.tags);
 | 
					  const tags = JSON.parse(guildDB.tags);
 | 
				
			||||||
| 
						 | 
					@ -99,6 +104,8 @@ exports.removeTag = async (name, guild) => {
 | 
				
			||||||
  connection.prepare("UPDATE guilds SET tags = ? WHERE guild_id = ?").run(JSON.stringify(tags), guild.id);
 | 
					  connection.prepare("UPDATE guilds SET tags = ? WHERE guild_id = ?").run(JSON.stringify(tags), guild.id);
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					exports.editTag = this.setTag;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
exports.setPrefix = async (prefix, guild) => {
 | 
					exports.setPrefix = async (prefix, guild) => {
 | 
				
			||||||
  connection.prepare("UPDATE guilds SET prefix = ? WHERE guild_id = ?").run(prefix, guild.id);
 | 
					  connection.prepare("UPDATE guilds SET prefix = ? WHERE guild_id = ?").run(prefix, guild.id);
 | 
				
			||||||
  collections.prefixCache.set(guild.id, prefix);
 | 
					  collections.prefixCache.set(guild.id, prefix);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,6 +2,7 @@
 | 
				
			||||||
set -e
 | 
					set -e
 | 
				
			||||||
 | 
					
 | 
				
			||||||
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
 | 
					psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
 | 
				
			||||||
    CREATE TABLE guilds ( guild_id VARCHAR(30) NOT NULL, tags json NOT NULL, prefix VARCHAR(15) NOT NULL, disabled text ARRAY NOT NULL, tags_disabled boolean NOT NULL );
 | 
					    CREATE TABLE guilds ( guild_id VARCHAR(30) NOT NULL PRIMARY KEY, prefix VARCHAR(15) NOT NULL, disabled text ARRAY NOT NULL, tags_disabled boolean NOT NULL );
 | 
				
			||||||
    CREATE TABLE counts ( command VARCHAR NOT NULL, count integer NOT NULL );
 | 
					    CREATE TABLE counts ( command VARCHAR NOT NULL PRIMARY KEY, count integer NOT NULL );
 | 
				
			||||||
 | 
					    CREATE TABLE tags ( guild_id VARCHAR(30) NOT NULL, name text NOT NULL, content text NOT NULL, author VARCHAR(30) NOT NULL, UNIQUE(guild_id, name) );
 | 
				
			||||||
EOSQL
 | 
					EOSQL
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue