diff --git a/app.js b/app.js index 8486f9b..42d24e4 100644 --- a/app.js +++ b/app.js @@ -88,7 +88,8 @@ const Admiral = new Fleet({ }, services: [ { name: "prometheus", path: path.join(__dirname, "./utils/services/prometheus.js") }, - { name: "image", path: path.join(__dirname, "./utils/services/image.js")} + { name: "image", path: path.join(__dirname, "./utils/services/image.js")}, + { name: "database", path: path.join(__dirname, "./utils/services/database.js")} ] }); diff --git a/commands/general/channel.js b/commands/general/channel.js index a44924d..2d0430f 100644 --- a/commands/general/channel.js +++ b/commands/general/channel.js @@ -8,7 +8,7 @@ class ChannelCommand extends Command { if (this.args.length === 0) return "You need to provide whether I should be enabled or disabled in this channel!"; if (this.args[0] !== "disable" && this.args[0] !== "enable") return "That's not a valid option!"; - const guildDB = await db.getGuild(this.message.channel.guild.id); + const guildDB = await db(this.ipc, "getGuild", this.message.channel.guild.id); if (this.args[0].toLowerCase() === "disable") { let channel; @@ -21,7 +21,7 @@ class ChannelCommand extends Command { channel = this.message.channel; } - await db.disableChannel(channel); + await db(this.ipc, "disableChannel", channel); return `I have been disabled in this channel. To re-enable me, just run \`${guildDB.prefix}channel enable\`.`; } else if (this.args[0].toLowerCase() === "enable") { let channel; @@ -34,7 +34,7 @@ class ChannelCommand extends Command { channel = this.message.channel; } - await db.enableChannel(channel); + await db(this.ipc, "enableChannel", channel); return "I have been re-enabled in this channel."; } } diff --git a/commands/general/count.js b/commands/general/count.js index 48cf792..dcb15ad 100644 --- a/commands/general/count.js +++ b/commands/general/count.js @@ -6,7 +6,7 @@ class CountCommand extends Command { async run() { if (this.message.channel.guild && !this.message.channel.permissionsOf(this.client.user.id).has("addReactions")) return "I don't have the `Add Reactions` permission!"; if (this.message.channel.guild && !this.message.channel.permissionsOf(this.client.user.id).has("embedLinks")) return "I don't have the `Embed Links` permission!"; - const counts = await database.getCounts(); + const counts = await database(this.ipc, "getCounts"); const countArray = []; for (const entry of Object.entries(counts)) { countArray.push(entry); diff --git a/commands/general/help.js b/commands/general/help.js index a2524cc..e7958f1 100644 --- a/commands/general/help.js +++ b/commands/general/help.js @@ -8,13 +8,13 @@ const Command = require("../../classes/command.js"); class HelpCommand extends Command { async run() { - const { prefix } = this.message.channel.guild ? await database.getGuild(this.message.channel.guild.id) : "N/A"; + const { prefix } = this.message.channel.guild ? await database(this.ipc, "getGuild", this.message.channel.guild.id) : "N/A"; const commands = collections.commands; const aliases = collections.aliases; if (this.args.length !== 0 && (commands.has(this.args[0].toLowerCase()) || aliases.has(this.args[0].toLowerCase()))) { const command = aliases.has(this.args[0].toLowerCase()) ? collections.aliases.get(this.args[0].toLowerCase()) : this.args[0].toLowerCase(); const info = collections.info.get(command); - const counts = await database.getCounts(); + const counts = await database(this.ipc, "getCounts"); const embed = { "embed": { "author": { diff --git a/commands/general/prefix.js b/commands/general/prefix.js index 19b29c4..d2b295c 100644 --- a/commands/general/prefix.js +++ b/commands/general/prefix.js @@ -4,10 +4,10 @@ const Command = require("../../classes/command.js"); class PrefixCommand extends Command { async run() { if (!this.message.channel.guild) return "This command only works in servers!"; - const guild = await database.getGuild(this.message.channel.guild.id); + const guild = await database(this.ipc, "getGuild", this.message.channel.guild.id); if (this.args.length !== 0) { if (!this.message.member.permissions.has("administrator") && this.message.member.id !== process.env.OWNER) return "You need to be an administrator to change the bot prefix!"; - await database.setPrefix(this.args[0], this.message.channel.guild); + await database(this.ipc, "setPrefix", this.args[0], this.message.channel.guild); return `The prefix has been changed to ${this.args[0]}.`; } else { return `The current prefix is \`${guild.prefix}\`.`; diff --git a/commands/tags/tags.js b/commands/tags/tags.js index 8cbe641..880d0d2 100644 --- a/commands/tags/tags.js +++ b/commands/tags/tags.js @@ -7,7 +7,7 @@ class TagsCommand extends Command { // todo: find a way to split this into subcommands async run() { if (!this.message.channel.guild) return "This command only works in servers!"; - const guild = await database.getGuild(this.message.channel.guild.id); + const guild = await database(this.ipc, "getGuild", this.message.channel.guild.id); 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!"; @@ -27,7 +27,7 @@ class TagsCommand extends Command { if (this.args[1] === undefined) return "You need to provide the name of the tag you want to delete!"; 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!"; - await database.removeTag(this.args[1].toLowerCase(), this.message.channel.guild); + await database(this.ipc, "removeTag", this.args[1].toLowerCase(), this.message.channel.guild); return `The tag \`${this.args[1].toLowerCase()}\` has been deleted!`; case "edit": if (this.args[1] === undefined) return "You need to provide the name of the tag you want to edit!"; @@ -75,7 +75,7 @@ class TagsCommand extends Command { case "enable": case "disable": if (!this.message.member.permissions.has("manageMessages") && this.message.author.id !== process.env.OWNER) return "You don't have permission to disable tags!"; - var toggleResult = await database.toggleTags(this.message.channel.guild); + var toggleResult = await database(this.ipc, "toggleTags", this.message.channel.guild); return `Tags for this guild have been ${toggleResult ? "disabled" : "enabled"}. To ${toggleResult ? "enable" : "disable"} them again, run ${guild.prefix}tags ${toggleResult ? "enable" : "disable"}.`; default: if (!tags[this.args[0].toLowerCase()]) return "This tag doesn't exist!"; @@ -86,11 +86,11 @@ class TagsCommand extends Command { async setTag(content, name, message) { 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) { - await database.setTag(name, { content: `${content} ${message.attachments[0].url}`, author: message.author.id }, message.channel.guild); + await database(this.ipc, "setTag", name, { content: `${content} ${message.attachments[0].url}`, author: message.author.id }, message.channel.guild); } else if (message.attachments.length !== 0) { - await database.setTag(name, { content: message.attachments[0].url, author: message.author.id }, message.channel.guild); + await database(this.ipc, "setTag", name, { content: message.attachments[0].url, author: message.author.id }, message.channel.guild); } else { - await database.setTag(name, { content: content, author: message.author.id }, message.channel.guild); + await database(this.ipc, "setTag", name, { content: content, author: message.author.id }, message.channel.guild); } return; } diff --git a/events/guildCreate.js b/events/guildCreate.js index b33e8e2..1df65ec 100644 --- a/events/guildCreate.js +++ b/events/guildCreate.js @@ -4,5 +4,5 @@ const logger = require("../utils/logger.js"); // run when the bot is added to a guild module.exports = async (client, cluster, worker, ipc, guild) => { logger.log(`[GUILD JOIN] ${guild.name} (${guild.id}) added the bot.`); - await db.addGuild(guild); + await db(ipc, "addGuild", guild); }; diff --git a/events/messageCreate.js b/events/messageCreate.js index 950db8b..2357bbd 100644 --- a/events/messageCreate.js +++ b/events/messageCreate.js @@ -20,9 +20,9 @@ module.exports = async (client, cluster, worker, ipc, message) => { if (cachedPrefix) { prefixCandidate = cachedPrefix; } else { - let guildDB = await database.getGuild(message.channel.guild.id); + let guildDB = await database(ipc, "getGuild", message.channel.guild.id); if (!guildDB) { - guildDB = await database.fixGuild(message.channel.guild); + guildDB = await database(ipc, "fixGuild", message.channel.guild); } prefixCandidate = guildDB.prefix; collections.prefixCache.set(message.channel.guild.id, guildDB.prefix); @@ -64,7 +64,7 @@ module.exports = async (client, cluster, worker, ipc, message) => { const disabled = collections.disabledCache.get(message.channel.guild.id); if (disabled.includes(message.channel.id) && command != "channel") return; } else if (message.channel.guild) { - const guildDB = await database.getGuild(message.channel.guild.id); + const guildDB = await database(ipc, "getGuild", message.channel.guild.id); collections.disabledCache.set(message.channel.guild.id, guildDB.disabled); if (guildDB.disabled.includes(message.channel.id) && command !== "channel") return; } @@ -88,7 +88,7 @@ module.exports = async (client, cluster, worker, ipc, message) => { } }; try { - await database.addCount(collections.aliases.has(command) ? collections.aliases.get(command) : command); + await database(ipc, "addCount", collections.aliases.has(command) ? collections.aliases.get(command) : command); const startTime = new Date(); // eslint-disable-next-line no-unused-vars const commandClass = new cmd(client, cluster, worker, ipc, message, parsed._, message.content.substring(prefix.length).trim().replace(command, "").trim(), (({ _, ...o }) => o)(parsed)); // we also provide the message content as a parameter for cases where we need more accuracy diff --git a/shard.js b/shard.js index ff29a22..fe266d6 100644 --- a/shard.js +++ b/shard.js @@ -100,7 +100,7 @@ class Shard extends BaseClusterWorker { // connect to lavalink if (!sound.status && !sound.connected) sound.connect(this.bot); - database.setup(); + database(this.ipc, "setup"); this.activityChanger(); @@ -136,7 +136,7 @@ class Shard extends BaseClusterWorker { for (const command in collections.commands) { handler.unload(command); } - require("./utils/database.js").stop(); + database(this.ipc, "stop"); done(); } diff --git a/utils/database.js b/utils/database.js index cb9e677..20972ac 100644 --- a/utils/database.js +++ b/utils/database.js @@ -1,3 +1,5 @@ -// wrapper for the database drivers in ./database/ +// wrapper for the database service -module.exports = require(`./database/${process.env.DB ? process.env.DB.split("://")[0] : "dummy"}.js`); \ No newline at end of file +module.exports = async (ipc, name, ...args) => { + return ipc.command("database", { name, args }, true); +}; diff --git a/utils/services/database.js b/utils/services/database.js new file mode 100644 index 0000000..f38947f --- /dev/null +++ b/utils/services/database.js @@ -0,0 +1,29 @@ +// 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; diff --git a/utils/services/prometheus.js b/utils/services/prometheus.js index c9a7a57..72ce76d 100644 --- a/utils/services/prometheus.js +++ b/utils/services/prometheus.js @@ -34,7 +34,7 @@ esmbot_connected_workers ${servers.length} res.write(`esmbot_max_jobs{worker="${i}"} ${w.max}\n`); } } - const counts = await database.getCounts(); + const counts = await database(this.ipc, "getCounts"); for (const [i, w] of Object.entries(counts)) { res.write(`esmbot_command_count{command="${i}"} ${w}\n`); } diff --git a/utils/soundplayer.js b/utils/soundplayer.js index 004636a..2ad6762 100644 --- a/utils/soundplayer.js +++ b/utils/soundplayer.js @@ -89,6 +89,7 @@ exports.play = async (client, sound, message, music = false) => { }; exports.nextSong = async (client, message, connection, track, info, music, voiceChannel, loop = false, inQueue = false, lastTrack = null) => { + this.skipVotes.set(this.message.channel.guild.id, { count: 0, ids: [] }); const parts = Math.floor((0 / info.length) * 10); let playingMessage; if (!music && this.players.get(voiceChannel.guild.id)) {