From 3f066f8cbfe19f146277f92a2a6f21610d4526e7 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Tue, 11 Jan 2022 22:54:09 +0200 Subject: [PATCH] thing -> interactionwrapper --- src/structure/client/wrappers/GuildWrapper.js | 38 +++++++++- .../client/wrappers/InteractionWrapper.js | 71 +++++++++++++++++++ src/structure/client/wrappers/index.js | 4 ++ .../components/observers/CommandHandler.js | 22 +++--- src/structure/interfaces/Thing.js | 50 ------------- src/structure/interfaces/index.js | 1 - 6 files changed, 124 insertions(+), 62 deletions(-) create mode 100644 src/structure/client/wrappers/InteractionWrapper.js create mode 100644 src/structure/client/wrappers/index.js delete mode 100644 src/structure/interfaces/Thing.js diff --git a/src/structure/client/wrappers/GuildWrapper.js b/src/structure/client/wrappers/GuildWrapper.js index e4c5f69..f63bc25 100644 --- a/src/structure/client/wrappers/GuildWrapper.js +++ b/src/structure/client/wrappers/GuildWrapper.js @@ -10,18 +10,50 @@ class GuildWrapper { } - async settings() { - if(!this._settings) this._settings = this.client.storageManager.mongodb.guilds.findOne({ guildId: this.id }); - if(this._settings instanceof Promise) this._settings = await this._settings || null; + async settings(forceFetch = false) { + if (this._settings && !forceFetch) return this._settings; + + this._settings = await this.client.storageManager.mongodb.guilds.findOne({ guildId: this.id }) || null; + // if(this._settings instanceof Promise) this._settings = await this._settings || null; if(!this._settings) this._settings = { guildId: this.id, ...this.defaultConfig }; else this._settings = { ...this.defaultConfig, ...this._settings }; //eslint-disable-line prefer-object-spread return this._settings; } + async updateSettings(settings) { + if (!this._settings) await this.settings(); + try { + await this.client.storageManager.mongodb.guilds.updateOne({ + guildId: this.id + }, settings); + this._settings = { + ...this._settings, + ...settings + }; + this._storageLog(`Database update: Settings (guild:${this.id})`); + } catch (error) { + this._storageError(error); + } + } + get defaultConfig() { return JSON.parse(JSON.stringify(this.client.defaultConfig())); } + // Logging + + _storageLog(log) { + this.client.logger.debug(log); + } + + _storageError(error) { + this.client.logger.error(`Database error (guild:${this.id}) :\n${error.stack || error}`); + } + + _debugLog(log) { + this.client.logger.debug(`[${this.name}] (${this.id}): ${log}`); + } + /* Wrapper Functions */ fetch() { diff --git a/src/structure/client/wrappers/InteractionWrapper.js b/src/structure/client/wrappers/InteractionWrapper.js new file mode 100644 index 0000000..7c9a7ac --- /dev/null +++ b/src/structure/client/wrappers/InteractionWrapper.js @@ -0,0 +1,71 @@ +const { Emojis } = require('../../../constants'); + +class InteractionWrapper { + + constructor(client, command, interaction, guildWrapper) { + + this.client = client; + this.interaction = interaction; + this.command = command; + this.guild = guildWrapper; + + this.options = []; + + this._pending = null; + + } + + emojify(options = {}) { + if(!Emojis[options.emoji]) this.client.logger.warn(`Invalid emoji provided to command ${this.command.resolveable}: "${options.emoji}"`); + options.content = `${Emojis[options.emoji]} ${options.content}`; + } + + async reply(options = {}) { + + if (options.emoji) this.emojify(options); + + this._pending = await this.interaction.reply(options); + return this._pending; + } + + async editReply(options = {}) { + + if (options.emoji) this.emojify(options); + this._pending = await this.interaction.editReply(options); + return this._pending; + + } + + format(locale, parameters = {}, code = false) { + const language = 'en_us'; //Default language. + //TODO: Fetch guild/user settings and switch localization. + return this.client.localeLoader.format(language, locale, parameters, code); + } + + get channel() { + return this.interaction.channel; + } + + get user() { + return this.interaction.user; + } + + get member() { + return this.interaction.member; + } + + get message() { + return this.interaction.message; + } + + get customId() { + return this.interaction.customId; + } + + get type() { + return this.interaction.type; + } + +} + +module.exports = InteractionWrapper; \ No newline at end of file diff --git a/src/structure/client/wrappers/index.js b/src/structure/client/wrappers/index.js new file mode 100644 index 0000000..e350b9e --- /dev/null +++ b/src/structure/client/wrappers/index.js @@ -0,0 +1,4 @@ +module.exports = { + GuildWrapper: require('./GuildWrapper.js'), + InteractionWrapper: require('./InteractionWrapper.js') +}; \ No newline at end of file diff --git a/src/structure/components/observers/CommandHandler.js b/src/structure/components/observers/CommandHandler.js index 469168c..56373d8 100644 --- a/src/structure/components/observers/CommandHandler.js +++ b/src/structure/components/observers/CommandHandler.js @@ -1,5 +1,5 @@ -const GuildWrapper = require('../../client/wrappers/GuildWrapper'); -const { Observer, Thing, CommandOption } = require('../../interfaces/'); +const { GuildWrapper, InteractionWrapper } = require('../../client/wrappers'); +const { Observer, CommandOption } = require('../../interfaces/'); class CommandHandler extends Observer { @@ -37,6 +37,7 @@ class CommandHandler extends Observer { } async interactionCreate(interaction) { + if(!interaction.isCommand() && !interaction.isContextMenu()) return undefined; @@ -47,20 +48,25 @@ class CommandHandler extends Observer { let guildWrapper = null; if(interaction.guild) guildWrapper = new GuildWrapper(this.client, interaction.guild); - const thing = new Thing(this.client, command, interaction, guildWrapper); + const wrapper = new InteractionWrapper(this.client, command, interaction, guildWrapper); - if(!command) return thing.reply({ content: thing.format('O_COMMANDHANDLER_COMMANDNOTSYNCED'), emoji: 'failure', ephemeral: true }); + if (!command) return wrapper.reply({ content: wrapper.format('O_COMMANDHANDLER_COMMANDNOTSYNCED'), emoji: 'failure', ephemeral: true }); - const response = await this._parseInteraction(thing); + const response = await this._parseInteraction(wrapper); if(response.error) { - return thing.reply({ - content: thing.format(`O_COMMANDHANDLER_TYPE${response.option.type}`, { option: response.option.name, min: response.option.minimum, max: response.option.maximum }), + return wrapper.reply({ + content: wrapper.format(`O_COMMANDHANDLER_TYPE${response.option.type}`, { option: response.option.name, min: response.option.minimum, max: response.option.maximum }), emoji: 'failure', ephemeral: true }); } - return this._executeCommand(thing, response.options); + try { + this._executeCommand(wrapper, response.options); + } catch (err) { + this.client.logger.error(`Error during execution of ${command.name}:\n${err.stack}`); + await wrapper.channel.send(`There was an error during the command execution.`); + } } diff --git a/src/structure/interfaces/Thing.js b/src/structure/interfaces/Thing.js deleted file mode 100644 index ee8f777..0000000 --- a/src/structure/interfaces/Thing.js +++ /dev/null @@ -1,50 +0,0 @@ -const { Emojis } = require('../../constants/'); - -class Thing { - - constructor(client, command, interaction, guild) { - - this.client = client; - this.interaction = interaction; - this.command = command; - this.guild = guild; - - this.options = []; - - this._pending = null; - - } - - async reply(options = {}) { - - if(options.emoji) { - if(!Emojis[options.emoji]) this.client.logger.warn(`Invalid emoji provided to command ${this.command.resolveable}: "${options.emoji}"`); - options.content = `${Emojis[options.emoji]} ${options.content}`; - // delete options.emoji; - } - - this._pending = await this.interaction.reply(options); - return this._pending; - } - - format(locale, parameters = {}, code = false) { - const language = 'en_us'; //Default language. - //TODO: Fetch guild/user settings and switch localization. - return this.client.localeLoader.format(language, locale, parameters, code); - } - - get channel() { - return this.interaction.channel; - } - - get user() { - return this.interaction.user; - } - - get member() { - return this.interaction.member; - } - -} - -module.exports = Thing; \ No newline at end of file diff --git a/src/structure/interfaces/index.js b/src/structure/interfaces/index.js index 83ad16a..294e868 100644 --- a/src/structure/interfaces/index.js +++ b/src/structure/interfaces/index.js @@ -5,6 +5,5 @@ module.exports = { SlashCommand: require('./commands/SlashCommand.js'), Command: require('./commands/Command.js'), CommandOption: require('./CommandOption.js'), - Thing: require('./Thing.js'), Setting: require('./Setting.js') }; \ No newline at end of file