From 17d49c8a9c2261f736ebd4e38f2db9295f493393 Mon Sep 17 00:00:00 2001 From: nolan Date: Wed, 25 Aug 2021 13:50:49 -0700 Subject: [PATCH] Add guild wrapper and default settings --- src/constants/defaults/DefaultGuild.json | 3 +++ src/constants/index.js | 3 ++- src/structure/DiscordClient.js | 20 +++++++++++++++ src/structure/client/LocaleLoader.js | 2 +- src/structure/client/wrappers/GuildWrapper.js | 25 +++++++++++++++++++ .../commands/moderation/MuteCommand.js | 2 ++ .../components/observers/CommandHandler.js | 2 -- src/structure/interfaces/Thing.js | 18 +------------ 8 files changed, 54 insertions(+), 21 deletions(-) create mode 100644 src/constants/defaults/DefaultGuild.json create mode 100644 src/structure/client/wrappers/GuildWrapper.js diff --git a/src/constants/defaults/DefaultGuild.json b/src/constants/defaults/DefaultGuild.json new file mode 100644 index 0000000..544b7b4 --- /dev/null +++ b/src/constants/defaults/DefaultGuild.json @@ -0,0 +1,3 @@ +{ + +} \ No newline at end of file diff --git a/src/constants/index.js b/src/constants/index.js index be378c4..6a6e97a 100644 --- a/src/constants/index.js +++ b/src/constants/index.js @@ -1,4 +1,5 @@ module.exports = { Commands: require('./Commands.json'), - Emojis: require('./Emojis.json') + Emojis: require('./Emojis.json'), + DefaultGuild: require('./defaults/DefaultGuild.json') } \ No newline at end of file diff --git a/src/structure/DiscordClient.js b/src/structure/DiscordClient.js index cabad56..110a3df 100644 --- a/src/structure/DiscordClient.js +++ b/src/structure/DiscordClient.js @@ -3,6 +3,8 @@ const { Client } = require('discord.js'); const { Logger, Intercom, EventHooker, LocaleLoader, Registry, Dispatcher, Resolver } = require('./client/'); const { Observer, Command } = require('./interfaces/'); +const { DefaultGuild } = require('../constants/'); + const options = require('../../options.json'); class DiscordClient extends Client { @@ -24,6 +26,8 @@ class DiscordClient extends Client { this.resolver = new Resolver(this); + this._defaultConfig = null; + this._activity = 0; this._options = options; this._built = false; @@ -63,6 +67,22 @@ class DiscordClient extends Client { } + defaultConfig() { + if(this._defaultConfig) return this._defaultConfig; + const settings = this.registry.components.filter((c) => c.type === 'setting'); + let def = DefaultGuild; + for(const setting of settings.values()) { + if(setting.default !== null) { + def = { + ...def, + ...setting.default + }; + } + } + this._defaultConfig = def; + return def; + } + async _setActivity() { const activities = { 0: async () => { diff --git a/src/structure/client/LocaleLoader.js b/src/structure/client/LocaleLoader.js index 0826d2b..e9f51da 100644 --- a/src/structure/client/LocaleLoader.js +++ b/src/structure/client/LocaleLoader.js @@ -29,7 +29,7 @@ class LocaleLoader { if(!string) return `< Missing Locale: ${language}.${index} >`; for(const [ parameter, value ] of Object.entries(parameters)) { - string = string.replace(new RegExp(`{${Util.escapeRegex(parameter.toLowerCase())}}`, 'giu'), value); + string = string.replace(new RegExp(Util.escapeRegex(`{${parameter.toLowerCase()}}`), 'giu'), value); } if(code) { diff --git a/src/structure/client/wrappers/GuildWrapper.js b/src/structure/client/wrappers/GuildWrapper.js new file mode 100644 index 0000000..44624c3 --- /dev/null +++ b/src/structure/client/wrappers/GuildWrapper.js @@ -0,0 +1,25 @@ +class GuildWrapper { + + constructor(client, guild) { + + this.client = client; + this.guild = guild; + + this._settings = null; + this._permissions = null; + + } + + 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; + 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; + } + + get defaultConfig() { + return JSON.parse(JSON.stringify(this.client.defaultConfig())); + } + +} \ No newline at end of file diff --git a/src/structure/components/commands/moderation/MuteCommand.js b/src/structure/components/commands/moderation/MuteCommand.js index c51c4cc..a96d615 100644 --- a/src/structure/components/commands/moderation/MuteCommand.js +++ b/src/structure/components/commands/moderation/MuteCommand.js @@ -35,8 +35,10 @@ class MuteCommand extends SlashCommand { async execute(thing, options) { + fruck; console.log(options); + // console.log(interaction, interaction.options); thing.reply({ content: this.resolveable }); diff --git a/src/structure/components/observers/CommandHandler.js b/src/structure/components/observers/CommandHandler.js index 7fee260..4fd5282 100644 --- a/src/structure/components/observers/CommandHandler.js +++ b/src/structure/components/observers/CommandHandler.js @@ -85,7 +85,6 @@ class CommandHandler extends Observer { for(const option of interaction.options._hoistedOptions) { const matched = command.options.find((o) => o.name === option.name); - console.log('matched', matched) const newOption = new CommandOption({ name: matched.name, type: matched.type, minimum: matched.minimum, maximum: matched.maximum, _rawValue: option.value }); const parsed = await this._parseOption(thing, newOption); @@ -134,7 +133,6 @@ class CommandHandler extends Observer { return { error: false, value: string }; }, INTEGER: (integer) => { - console.log(option); if(option.minimum !== undefined && integer < option.minimum) return { error: true }; if(option.maximum !== undefined && integer > option.maximum) return { error: true }; return { error: false, value: parseInt(integer) }; diff --git a/src/structure/interfaces/Thing.js b/src/structure/interfaces/Thing.js index 79c0b26..4758e5d 100644 --- a/src/structure/interfaces/Thing.js +++ b/src/structure/interfaces/Thing.js @@ -9,9 +9,6 @@ class Thing { this.command = command; this.options = []; - - this._guild = null; - this._channel = null; this._pending = null; @@ -43,20 +40,6 @@ class Thing { return this.interaction.channel || null; } - // async guild() { - // if(!this.interaction.guild) return null; - // if(this._guild) return this._guild; - // this._guild = await this.client.guilds.fetch(this.interaction.guildId); - // return this._guild; - // } - - // async channel() { - // if(!this.interaction.channel) return null; - // if(this._channel) return this._channel; - // this._channel = await this.client.channels.fetch(this.interaction.channelId); - // return this._channel; - // } - get user() { return this.interaction.user || null; } @@ -64,6 +47,7 @@ class Thing { get member() { return this.interaction.member || null; } + } module.exports = Thing; \ No newline at end of file