Added command, reverted database service split
This commit is contained in:
parent
da709c485f
commit
9f36a79a2b
20 changed files with 119 additions and 91 deletions
|
@ -27,4 +27,5 @@ exports.runningCommands = new TimedMap();
|
|||
}*/
|
||||
|
||||
exports.prefixCache = new Map();
|
||||
exports.disabledCache = new Map();
|
||||
exports.disabledCache = new Map();
|
||||
exports.disabledCmdCache = new Map();
|
|
@ -5,8 +5,8 @@ const pool = new Pool({
|
|||
});
|
||||
|
||||
(async () => {
|
||||
console.log("Migrating tags...");
|
||||
const guilds = (await pool.query("SELECT * FROM guilds")).rows;
|
||||
console.log("Migrating tags...");
|
||||
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) {
|
||||
|
@ -22,6 +22,11 @@ const pool = new Pool({
|
|||
console.log(`Migrated tag ${name} in guild ${guild.guild_id}`);
|
||||
}
|
||||
}
|
||||
console.log("Migrating disabled commands...");
|
||||
for (const guild of guilds) {
|
||||
await pool.query("UPDATE guilds SET disabled_commands = $1 WHERE guild_id = $2", [guild.tags_disabled ? ["tags"] : [], guild.guild_id]);
|
||||
console.log(`Migrated disabled commands in guild ${guild.guild_id}`);
|
||||
}
|
||||
console.log("Done!");
|
||||
return process.exit(0);
|
||||
})();
|
|
@ -1,5 +1,3 @@
|
|||
// wrapper for the database service
|
||||
// wrapper for the database drivers in ./database/
|
||||
|
||||
module.exports = async (ipc, name, ...args) => {
|
||||
return ipc.command("database", { name, args }, true);
|
||||
};
|
||||
module.exports = require(`./database/${process.env.DB ? process.env.DB.split("://")[0] : "dummy"}.js`);
|
|
@ -11,9 +11,10 @@ exports.addCount = async () => {};
|
|||
exports.getCounts = async () => {
|
||||
return {};
|
||||
};
|
||||
exports.disableCommand = async () => {};
|
||||
exports.enableCommand = async () => {};
|
||||
exports.disableChannel = async () => {};
|
||||
exports.enableChannel = async () => {};
|
||||
exports.toggleTags = async () => {};
|
||||
exports.getTags = async () => {};
|
||||
exports.setTag = async () => {};
|
||||
exports.removeTag = async () => {};
|
||||
|
|
|
@ -37,11 +37,17 @@ exports.removeTag = async (name, guild) => {
|
|||
await connection.query("DELETE FROM tags WHERE guild_id = $1 AND name = $2", [guild.id, name]);
|
||||
};
|
||||
|
||||
exports.toggleTags = async (guild) => {
|
||||
const guildDB = await this.getGuild(guild.id);
|
||||
guildDB.tags_disabled = !guildDB.tags_disabled;
|
||||
await connection.query("UPDATE guilds SET tags_disabled = $1 WHERE guild_id = $2", [guildDB.tags_disabled, guild.id]);
|
||||
return guildDB.tags_disabled;
|
||||
exports.disableCommand = async (guild, command) => {
|
||||
const guildDB = await this.getGuild(guild);
|
||||
await connection.query("UPDATE guilds SET disabled_commands = $1 WHERE guild_id = $2", [(guildDB.disabled_commands ? [...guildDB.disabled_commands, command] : [command]).filter((v) => v !== undefined), guild]);
|
||||
collections.disabledCmdCache.set(guild, guildDB.disabled_commands ? [...guildDB.disabled_commands, command] : [command].filter((v) => v !== undefined));
|
||||
};
|
||||
|
||||
exports.enableCommand = async (guild, command) => {
|
||||
const guildDB = await this.getGuild(guild);
|
||||
const newDisabled = guildDB.disabled_commands ? guildDB.disabled_commands.filter(item => item !== command) : [];
|
||||
await connection.query("UPDATE guilds SET disabled_commands = $1 WHERE guild_id = $2", [newDisabled, guild]);
|
||||
collections.disabledCmdCache.set(guild, newDisabled);
|
||||
};
|
||||
|
||||
exports.disableChannel = async (channel) => {
|
||||
|
@ -79,7 +85,7 @@ 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, tags_disabled) VALUES ($1, $2, $3, $4)", [guild.id, process.env.PREFIX, [], false]);
|
||||
await connection.query("INSERT INTO guilds (guild_id, prefix, disabled, disabled_commands) VALUES ($1, $2, $3, $4)", [guild.id, process.env.PREFIX, [], []]);
|
||||
return await this.getGuild(guild.id);
|
||||
};
|
||||
|
||||
|
|
|
@ -50,7 +50,7 @@ exports.fixGuild = async (guild) => {
|
|||
try {
|
||||
guildDB = connection.prepare("SELECT * FROM guilds WHERE guild_id = ?").get(guild.id);
|
||||
} catch {
|
||||
connection.prepare("CREATE TABLE guilds ( guild_id VARCHAR(30) NOT NULL PRIMARY KEY, prefix VARCHAR(15) NOT NULL, disabled text NOT NULL, tags_disabled integer NOT NULL DEFAULT 0 CHECK(tags_disabled IN (0,1)) )").run();
|
||||
connection.prepare("CREATE TABLE guilds ( guild_id VARCHAR(30) NOT NULL PRIMARY KEY, prefix VARCHAR(15) NOT NULL, disabled text NOT NULL, disabled_commands text NOT NULL )").run();
|
||||
}
|
||||
if (!guildDB) {
|
||||
logger.log(`Registering guild database entry for guild ${guild.id}...`);
|
||||
|
@ -71,6 +71,19 @@ exports.getCounts = async () => {
|
|||
return countObject;
|
||||
};
|
||||
|
||||
exports.disableCommand = async (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 !== undefined)), guild);
|
||||
collections.disabledCmdCache.set(guild, guildDB.disabled_commands ? [...JSON.parse(guildDB.disabledCommands), command] : [command].filter((v) => v !== undefined));
|
||||
};
|
||||
|
||||
exports.enableCommand = async (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);
|
||||
collections.disabledCmdCache.set(guild, newDisabled);
|
||||
};
|
||||
|
||||
exports.disableChannel = async (channel) => {
|
||||
const guildDB = await this.getGuild(channel.guild.id);
|
||||
connection.prepare("UPDATE guilds SET disabled = ? WHERE guild_id = ?").run(JSON.stringify([...JSON.parse(guildDB.disabled), channel.id]), channel.guild.id);
|
||||
|
@ -84,12 +97,6 @@ exports.enableChannel = async (channel) => {
|
|||
collections.disabledCache.set(channel.guild.id, newDisabled);
|
||||
};
|
||||
|
||||
exports.toggleTags = async (guild) => {
|
||||
const guildDB = await this.getGuild(guild.id);
|
||||
guildDB.tags_disabled = guildDB.tags_disabled ? 0 : 1;
|
||||
connection.prepare("UPDATE guilds SET tags_disabled = ? WHERE guild_id = ?").run(guildDB.tags_disabled, guild.id);
|
||||
};
|
||||
|
||||
exports.getTags = async (guild) => {
|
||||
const tagArray = connection.prepare("SELECT * FROM tags WHERE guild_id = ?").all(guild);
|
||||
const tags = {};
|
||||
|
@ -130,9 +137,9 @@ exports.addGuild = async (guild) => {
|
|||
id: guild.id,
|
||||
prefix: process.env.PREFIX,
|
||||
disabled: "[]",
|
||||
tagsDisabled: 0
|
||||
disabledCommands: "[]"
|
||||
};
|
||||
connection.prepare("INSERT INTO guilds (guild_id, prefix, disabled, tags_disabled) VALUES (@id, @prefix, @disabled, @tagsDisabled)").run(guildObject);
|
||||
connection.prepare("INSERT INTO guilds (guild_id, prefix, disabled, disabled_commands) VALUES (@id, @prefix, @disabled, @tagsDisabled)").run(guildObject);
|
||||
return guildObject;
|
||||
};
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
set -e
|
||||
|
||||
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
|
||||
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 guilds ( guild_id VARCHAR(30) NOT NULL PRIMARY KEY, prefix VARCHAR(15) NOT NULL, disabled text ARRAY NOT NULL, disabled_commands text ARRAY 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
|
|
@ -1,29 +0,0 @@
|
|||
// service wrapper for the database drivers in ../database/
|
||||
const { BaseServiceWorker } = require("eris-fleet");
|
||||
|
||||
const database = require(`../database/${process.env.DB ? process.env.DB.split("://")[0] : "dummy"}.js`);
|
||||
|
||||
class DatabaseWorker extends BaseServiceWorker {
|
||||
constructor(setup) {
|
||||
super(setup);
|
||||
this.serviceReady();
|
||||
}
|
||||
|
||||
async handleCommand(data) {
|
||||
try {
|
||||
if (database[data.name]) {
|
||||
return await database[data.name](...data.args);
|
||||
} else {
|
||||
throw "Unknown query";
|
||||
}
|
||||
} catch (err) {
|
||||
return { err: typeof err === "string" ? err : err.message };
|
||||
}
|
||||
}
|
||||
|
||||
shutdown(done) {
|
||||
database.stop().then(() => done);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = DatabaseWorker;
|
|
@ -34,7 +34,7 @@ esmbot_connected_workers ${servers.length}
|
|||
res.write(`esmbot_max_jobs{worker="${i}"} ${w.max}\n`);
|
||||
}
|
||||
}
|
||||
const counts = await database(this.ipc, "getCounts");
|
||||
const counts = await database.getCounts();
|
||||
for (const [i, w] of Object.entries(counts)) {
|
||||
res.write(`esmbot_command_count{command="${i}"} ${w}\n`);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue