Added support for using both MongoDB and PostgreSQL, changed image api timeouts, removed all moderation commands
This commit is contained in:
parent
69d8100f23
commit
ef945adf09
21 changed files with 683 additions and 371 deletions
|
@ -1,3 +1,6 @@
|
|||
exports.commands = new Map();
|
||||
exports.aliases = new Map();
|
||||
exports.info = new Map();
|
||||
exports.info = new Map();
|
||||
|
||||
exports.prefixCache = new Map();
|
||||
exports.disabledCache = new Map();
|
60
utils/convertdb.js
Normal file
60
utils/convertdb.js
Normal file
|
@ -0,0 +1,60 @@
|
|||
require("dotenv").config();
|
||||
const { Pool } = require("pg");
|
||||
const pool = new Pool({
|
||||
user: "esmbot",
|
||||
host: "localhost",
|
||||
database: "esmbot",
|
||||
port: 5432
|
||||
});
|
||||
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 {
|
||||
console.log("Skipping table creation due to error...");
|
||||
}
|
||||
for (const guild of guilds) {
|
||||
console.log(guild.tagsDisabled);
|
||||
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, warns = $3, disabled = $4, tags_disabled = $5 WHERE guild_id = $6", [guild.tags, guild.prefix, {}, 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, warns, disabled, tags_disabled) VALUES ($1, $2, $3, $4, $5, $6)", [guild.id, guild.tags, guild.prefix, {}, 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 {
|
||||
console.log("Skipping table creation due to error...");
|
||||
}
|
||||
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;
|
||||
})();
|
|
@ -1,21 +1,249 @@
|
|||
// database stuff
|
||||
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,
|
||||
warns: Map,
|
||||
disabledChannels: [String],
|
||||
tagsDisabled: Boolean
|
||||
});
|
||||
const Guild = mongoose.model("Guild", guildSchema);
|
||||
const logger = require("./logger.js");
|
||||
const collections = require("../utils/collections.js");
|
||||
const misc = require("./misc.js");
|
||||
|
||||
const globalSchema = new mongoose.Schema({
|
||||
cmdCounts: Map
|
||||
});
|
||||
const Global = mongoose.model("Global", globalSchema);
|
||||
if (process.env.DB === "mongo") {
|
||||
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);
|
||||
|
||||
exports.guilds = Guild;
|
||||
exports.global = Global;
|
||||
exports.connection = mongoose.connection;
|
||||
const globalSchema = new mongoose.Schema({
|
||||
cmdCounts: Map,
|
||||
});
|
||||
const Global = mongoose.model("Global", globalSchema);
|
||||
|
||||
exports.guilds = Guild;
|
||||
exports.global = Global;
|
||||
exports.connection = mongoose.connection;
|
||||
} else if (process.env.DB === "postgres") {
|
||||
const { Pool } = require("pg");
|
||||
const pool = new Pool({
|
||||
user: "esmbot",
|
||||
host: "localhost",
|
||||
database: "esmbot",
|
||||
port: 5432
|
||||
});
|
||||
exports.connection = pool;
|
||||
}
|
||||
|
||||
exports.getGuild = async (query) => {
|
||||
if (process.env.DB === "mongo") {
|
||||
return await this.guilds.findOne({ id: query });
|
||||
} else if (process.env.DB === "postgres") {
|
||||
return (await this.connection.query("SELECT * FROM guilds WHERE guild_id = $1", [query])).rows[0];
|
||||
}
|
||||
};
|
||||
|
||||
exports.setPrefix = async (prefix, guild) => {
|
||||
if (process.env.DB === "mongo") {
|
||||
const guildDB = await this.getGuild(guild.id);
|
||||
guildDB.prefix = prefix;
|
||||
await guildDB.save();
|
||||
collections.prefixCache.set(guild.id, prefix);
|
||||
} else if (process.env.DB === "postgres") {
|
||||
await this.connection.query("UPDATE guilds SET prefix = $1 WHERE guild_id = $2", [prefix, guild.id]);
|
||||
collections.prefixCache.set(guild.id, prefix);
|
||||
}
|
||||
};
|
||||
|
||||
exports.setTag = async (name, content, guild) => {
|
||||
if (process.env.DB === "mongo") {
|
||||
const guildDB = await this.getGuild(guild.id);
|
||||
guildDB.tags[name] = content;
|
||||
await guildDB.save();
|
||||
} else if (process.env.DB === "postgres") {
|
||||
const guildDB = await this.getGuild(guild.id);
|
||||
guildDB.tags[name] = content;
|
||||
await this.connection.query("UPDATE guilds SET tags = $1 WHERE guild_id = $2", [guildDB.tags, guild.id]);
|
||||
}
|
||||
};
|
||||
|
||||
exports.removeTag = async (name, guild) => {
|
||||
if (process.env.DB === "mongo") {
|
||||
const guildDB = await this.getGuild(guild.id);
|
||||
delete guildDB.tags[name];
|
||||
await guildDB.save();
|
||||
} else if (process.env.DB === "postgres") {
|
||||
const guildDB = await this.getGuild(guild.id);
|
||||
delete guildDB.tags[name];
|
||||
await this.connection.query("UPDATE guilds SET tags = $1 WHERE guild_id = $2", [guildDB.tags, guild.id]);
|
||||
}
|
||||
};
|
||||
|
||||
exports.toggleTags = async (guild) => {
|
||||
if (process.env.DB === "mongo") {
|
||||
const guildDB = await this.getGuild(guild.id);
|
||||
guildDB.tagsDisabled = !guildDB.tagsDisabled;
|
||||
await guildDB.save();
|
||||
return guildDB.tagsDisabled;
|
||||
} else if (process.env.DB === "postgres") {
|
||||
const guildDB = await this.getGuild(guild.id);
|
||||
guildDB.tags_disabled = !guildDB.tags_disabled;
|
||||
await this.connection.query("UPDATE guilds SET tags_disabled = $1 WHERE guild_id = $2", [guildDB.tags_disabled, guild.id]);
|
||||
return guildDB.tags_disabled;
|
||||
}
|
||||
};
|
||||
|
||||
exports.disableChannel = async (channel) => {
|
||||
if (process.env.DB === "mongo") {
|
||||
const guildDB = await this.getGuild(channel.guild.id);
|
||||
guildDB.disabled.push(channel.id);
|
||||
await guildDB.save();
|
||||
collections.disabledCache.set(channel.guild.id, guildDB.disabled);
|
||||
} else if (process.env.DB === "postgres") {
|
||||
const guildDB = await this.getGuild(channel.guild.id);
|
||||
await this.connection.query("UPDATE guilds SET disabled = $1 WHERE guild_id = $2", [[...guildDB.disabled, channel.id], channel.guild.id]);
|
||||
collections.disabledCache.set(channel.guild.id, guildDB.disabled);
|
||||
}
|
||||
};
|
||||
|
||||
exports.enableChannel = async (channel) => {
|
||||
if (process.env.DB === "mongo") {
|
||||
const guildDB = await this.getGuild(channel.guild.id);
|
||||
guildDB.disabled = guildDB.disabled.filter(item => item !== channel.id);
|
||||
await guildDB.save();
|
||||
collections.disabledCache.set(channel.guild.id, guildDB.disabled);
|
||||
} else if (process.env.DB === "postgres") {
|
||||
const guildDB = await this.getGuild(channel.guild.id);
|
||||
const newDisabled = guildDB.disabled.filter(item => item !== channel.id);
|
||||
await this.connection.query("UPDATE guilds SET disabled = $1 WHERE guild_id = $2", [newDisabled, channel.guild.id]);
|
||||
collections.disabledCache.set(channel.guild.id, guildDB.disabled);
|
||||
}
|
||||
};
|
||||
|
||||
exports.getCounts = async () => {
|
||||
if (process.env.DB === "mongo") {
|
||||
return [...(await this.global.findOne({})).cmdCounts.entries()];
|
||||
} else if (process.env.DB === "postgres") {
|
||||
const counts = await this.connection.query("SELECT * FROM counts");
|
||||
const countArray = [];
|
||||
for (const { command, count } of counts.rows) {
|
||||
countArray.push([command, count]);
|
||||
}
|
||||
return countArray;
|
||||
}
|
||||
};
|
||||
|
||||
exports.addCount = async (command) => {
|
||||
if (process.env.DB === "mongo") {
|
||||
const global = await this.global.findOne({});
|
||||
const count = global.cmdCounts.get(command);
|
||||
global.cmdCounts.set(command, parseInt(count) + 1);
|
||||
await global.save();
|
||||
} else if (process.env.DB === "postgres") {
|
||||
const count = await this.connection.query("SELECT * FROM counts WHERE command = $1", [command]);
|
||||
await this.connection.query("UPDATE counts SET count = $1 WHERE command = $2", [count.rows[0].count + 1, command]);
|
||||
}
|
||||
};
|
||||
|
||||
exports.addGuild = async (guild) => {
|
||||
if (process.env.DB === "mongo") {
|
||||
const guildDB = new this.guilds({
|
||||
id: guild.id,
|
||||
tags: misc.tagDefaults,
|
||||
prefix: process.env.PREFIX,
|
||||
disabled: [],
|
||||
tagsDisabled: false
|
||||
});
|
||||
await guildDB.save();
|
||||
return guildDB;
|
||||
} else if (process.env.DB === "postgres") {
|
||||
await this.connection.query("INSERT INTO guilds (guild_id, tags, prefix, warns, disabled, tags_disabled) VALUES ($1, $2, $3, $4, $5, $6)", [guild.id, misc.tagDefaults, process.env.PREFIX, {}, [], false]);
|
||||
return await this.getGuild(guild.id);
|
||||
}
|
||||
};
|
||||
|
||||
exports.fixGuild = async (guild) => {
|
||||
if (process.env.DB === "mongo") {
|
||||
const guildDB = await this.guilds.findOne({ id: guild.id });
|
||||
if (!guildDB) {
|
||||
logger.log(`Registering guild database entry for guild ${guild.id}...`);
|
||||
return await this.addGuild(guild);
|
||||
} else {
|
||||
if (!guildDB.disabled && guildDB.disabledChannels) {
|
||||
guildDB.set("disabled", guildDB.disabledChannels);
|
||||
guildDB.set("disabledChannels", undefined);
|
||||
await guildDB.save();
|
||||
return guildDB;
|
||||
}
|
||||
}
|
||||
} else if (process.env.DB === "postgres") {
|
||||
const guildDB = await this.connection.query("SELECT * FROM guilds WHERE guild_id = $1", [guild.id]);
|
||||
if (guildDB.rows.length === 0) {
|
||||
logger.log(`Registering guild database entry for guild ${guild.id}...`);
|
||||
return await this.addGuild(guild);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
exports.handleCounts = async () => {
|
||||
if (process.env.DB === "mongo") {
|
||||
const global = await this.global.findOne({});
|
||||
if (!global) {
|
||||
const countObject = {};
|
||||
for (const command of collections.commands.keys()) {
|
||||
countObject[command] = 0;
|
||||
}
|
||||
const newGlobal = new this.global({
|
||||
cmdCounts: countObject
|
||||
});
|
||||
await newGlobal.save();
|
||||
} else {
|
||||
const exists = [];
|
||||
for (const command of collections.commands.keys()) {
|
||||
if (!global.cmdCounts.has(command)) {
|
||||
global.cmdCounts.set(command, 0);
|
||||
}
|
||||
exists.push(command);
|
||||
}
|
||||
|
||||
for (const command of global.cmdCounts.keys()) {
|
||||
if (!exists.includes(command)) {
|
||||
global.cmdCounts.set(command, undefined);
|
||||
}
|
||||
}
|
||||
await global.save();
|
||||
}
|
||||
} else if (process.env.DB === "postgres") {
|
||||
let counts;
|
||||
try {
|
||||
counts = await this.connection.query("SELECT * FROM counts");
|
||||
} catch {
|
||||
counts = { rows: [] };
|
||||
}
|
||||
|
||||
if (!counts.rows[0]) {
|
||||
for (const command of collections.commands.keys()) {
|
||||
await this.connection.query("INSERT INTO counts (command, count) VALUES ($1, $2)", [command, 0]);
|
||||
}
|
||||
} else {
|
||||
const exists = [];
|
||||
for (const command of collections.commands.keys()) {
|
||||
const count = await this.connection.query("SELECT * FROM counts WHERE command = $1", [command]);
|
||||
if (!count.rows[0]) {
|
||||
await this.connection.query("INSERT INTO counts (command, count) VALUES ($1, $2)", [command, 0]);
|
||||
}
|
||||
exists.push(command);
|
||||
}
|
||||
|
||||
for (const { command } of counts.rows) {
|
||||
if (!exists.includes(command)) {
|
||||
await this.connection.query("DELETE FROM counts WHERE command = $1", [command]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
|
@ -15,7 +15,6 @@ Default prefix is \`&\`.
|
|||
|
||||
## Table of Contents
|
||||
+ [**General**](#💻-general)
|
||||
+ [**Moderation**](#🔨-moderation)
|
||||
+ [**Tags**](#🏷️-tags)
|
||||
+ [**Fun**](#👌-fun)
|
||||
+ [**Image Editing**](#🖼️-image-editing)
|
||||
|
@ -25,7 +24,6 @@ Default prefix is \`&\`.
|
|||
const commands = collections.commands;
|
||||
const categories = {
|
||||
general: ["## 💻 General"],
|
||||
moderation: ["## 🔨 Moderation"],
|
||||
tags: ["## 🏷️ Tags"],
|
||||
fun: ["## 👌 Fun"],
|
||||
images: ["## 🖼️ Image Editing", "> These commands support the PNG, JPEG, WEBP, and GIF formats. (GIF support is currently experimental)"],
|
||||
|
@ -55,7 +53,7 @@ Default prefix is \`&\`.
|
|||
categories.music.push(`+ **${command}**${params ? ` ${params}` : ""} - ${description}`);
|
||||
}
|
||||
}
|
||||
fs.writeFile(output, `${template}\n${categories.general.join("\n")}\n\n${categories.moderation.join("\n")}\n\n${categories.tags.join("\n")}\n\n${categories.fun.join("\n")}\n\n${categories.images.join("\n")}\n\n${categories.soundboard.join("\n")}\n\n${categories.music.join("\n")}`, () => {
|
||||
fs.writeFile(output, `${template}\n${categories.general.join("\n")}\n\n${categories.tags.join("\n")}\n\n${categories.fun.join("\n")}\n\n${categories.images.join("\n")}\n\n${categories.soundboard.join("\n")}\n\n${categories.music.join("\n")}`, () => {
|
||||
logger.log("The help docs have been generated.");
|
||||
});
|
||||
};
|
|
@ -65,21 +65,18 @@ exports.run = (object, fromAPI = false) => {
|
|||
const socket = dgram.createSocket("udp4");
|
||||
const data = Buffer.concat([Buffer.from([0x1]), Buffer.from(JSON.stringify(object))]);
|
||||
|
||||
let timeout = setTimeout(() => {
|
||||
const timeout = setTimeout(() => {
|
||||
reject("Timed out");
|
||||
}, 25000);
|
||||
|
||||
let jobID;
|
||||
socket.on("message", (msg) => {
|
||||
clearTimeout(timeout);
|
||||
const opcode = msg.readUint8(0);
|
||||
const req = msg.slice(37, msg.length);
|
||||
const uuid = msg.slice(1, 36).toString();
|
||||
if (opcode === 0x0) {
|
||||
clearTimeout(timeout);
|
||||
jobID = uuid;
|
||||
timeout = setTimeout(() => {
|
||||
reject("Timed out");
|
||||
}, 300000);
|
||||
} else if (opcode === 0x1) {
|
||||
if (jobID === uuid) {
|
||||
const client = net.createConnection(req.toString(), currentServer);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue