thing -> interactionwrapper
This commit is contained in:
parent
75c2c3630a
commit
3f066f8cbf
@ -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() {
|
||||
|
71
src/structure/client/wrappers/InteractionWrapper.js
Normal file
71
src/structure/client/wrappers/InteractionWrapper.js
Normal file
@ -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;
|
4
src/structure/client/wrappers/index.js
Normal file
4
src/structure/client/wrappers/index.js
Normal file
@ -0,0 +1,4 @@
|
||||
module.exports = {
|
||||
GuildWrapper: require('./GuildWrapper.js'),
|
||||
InteractionWrapper: require('./InteractionWrapper.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.`);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -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;
|
@ -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')
|
||||
};
|
Loading…
Reference in New Issue
Block a user