Add guild wrapper and default settings

This commit is contained in:
nolan 2021-08-25 13:50:49 -07:00
parent 9f7ccc678f
commit 17d49c8a9c
8 changed files with 54 additions and 21 deletions

View File

@ -0,0 +1,3 @@
{
}

View File

@ -1,4 +1,5 @@
module.exports = {
Commands: require('./Commands.json'),
Emojis: require('./Emojis.json')
Emojis: require('./Emojis.json'),
DefaultGuild: require('./defaults/DefaultGuild.json')
}

View File

@ -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 () => {

View File

@ -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) {

View File

@ -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()));
}
}

View File

@ -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 });

View File

@ -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) };

View File

@ -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;