| @@ -0,0 +1,60 @@ | ||
| const Config = require('../Config.js'); | ||
|
|
||
| class Array { | ||
| constructor(conf, data) { | ||
| Object.defineProperty(this, '_guild', { value: conf._guild }); | ||
| Object.defineProperty(this, '_dataDir', { value: conf._dataDir }); | ||
| Object.defineProperty(this, '_client', { value: conf._client }); | ||
| this.type = 'Array'; | ||
| if (!(data instanceof Array)) this._data = []; | ||
| else this._data = data; | ||
| } | ||
|
|
||
| get data() { | ||
| return this._data; | ||
| } | ||
|
|
||
| set data(data) { | ||
| if (data === undefined || (!(data instanceof Array) || typeof data !== 'string')) throw 'Please supply a valid array or string.'; | ||
| if (data instanceof Array) { | ||
| data.forEach(dat => { | ||
| if (this._data.includes(data)) this.del(dat); | ||
| else this.add(dat); | ||
| }); | ||
| } else if (this._data.includes(data)) { | ||
| this.del(data); | ||
| } else { this.add(data); } | ||
| Config.save(this._dataDir, this._guild.id); | ||
| return this; | ||
| } | ||
|
|
||
| add(value) { | ||
| if (!value === undefined || (!(value instanceof Array) && typeof value !== 'string')) throw 'Please supply a valid value (array, or string) to add to the possibles array.'; | ||
| if (value instanceof Array) { | ||
| value.forEach((val) => { | ||
| if (!this._datas.includes(val)) this._data.push(val); | ||
| }); | ||
| } else { | ||
| if (this._data.includes(value)) throw 'That value already exists in the possibles array.'; | ||
| this._data.push(value); | ||
| } | ||
| Config.save(this._dataDir, this._guild.id); | ||
| return this; | ||
| } | ||
|
|
||
| del(value) { | ||
| if (!value === undefined || (!(value instanceof Array) && typeof value !== 'string')) throw 'Please supply a valid value (array, or string) to add to the possibles array.'; | ||
| if (value instanceof Array) { | ||
| value.forEach((val) => { | ||
| if (this._data.includes(val)) this._data.splice(this._data.indexOf(val), 1); | ||
| }); | ||
| } else { | ||
| if (!this._data.includes(value)) throw 'That value does not already exist in the possibles array.'; | ||
| this._data.splice(this._data.indexOf(value), 1); | ||
| } | ||
| Config.save(this._dataDir, this._guild.id); | ||
| return this; | ||
| } | ||
| } | ||
|
|
||
| module.exports = Array; |
| @@ -0,0 +1,29 @@ | ||
| const Config = require('../Config.js'); | ||
|
|
||
| const truthy = [true, 'true', 1, 't', 'yes', 'y']; | ||
| const falsy = [false, 'false', 0, 'f', 'no', 'n']; | ||
|
|
||
| class Boolean { | ||
| constructor(conf, data) { | ||
| Object.defineProperty(this, '_guild', { value: conf._guild }); | ||
| Object.defineProperty(this, '_dataDir', { value: conf._dataDir }); | ||
| Object.defineProperty(this, '_client', { value: conf._client }); | ||
| this.type = 'Boolean'; | ||
| if (typeof data !== 'boolean') this._data = false; | ||
| else this._data = data; | ||
| } | ||
|
|
||
| get data() { | ||
| return this._data; | ||
| } | ||
|
|
||
| set data(data) { | ||
| if (truthy.includes(data)) data = true; | ||
| else if (falsy.includes(data)) data = false; | ||
| else throw 'Data provided was not a truthy or falsy variable.'; | ||
| this._data = data; | ||
| Config.save(this._dataDir, this._guild.id); | ||
| } | ||
| } | ||
|
|
||
| module.exports = Boolean; |
| @@ -0,0 +1,36 @@ | ||
| const Config = require('../Config.js'); | ||
| const Discord = require('discord.js'); | ||
|
|
||
| class Channel { | ||
| constructor(conf, data) { | ||
| Object.defineProperty(this, '_guild', { value: conf._guild }); | ||
| Object.defineProperty(this, '_dataDir', { value: conf._dataDir }); | ||
| Object.defineProperty(this, '_client', { value: conf._client }); | ||
| this.type = 'Channel'; | ||
| if (data instanceof Discord.Channel) this._data = data.id; | ||
| if (typeof data === 'string') { | ||
| if (/^<#\d+>$/.test(data)) data = /^<#\d+>$/.exec(data)[0]; | ||
| const dataChannel = this._client.channels.get(data); | ||
| if (dataChannel) this._data = dataChannel.id; | ||
| else this._data = null; | ||
| } else { this._data = null; } | ||
| } | ||
|
|
||
| get data() { | ||
| return this._client.channels.get(this._data); | ||
| } | ||
|
|
||
| set data(channel) { | ||
| if (channel instanceof Discord.Channel) this._data = channel; | ||
| if (typeof data === 'string') { | ||
| if (/^<#\d+>$/.test(channel)) channel = /^<#\d+>$/.exec(channel)[0]; | ||
| const dataChannel = this._client.channels.get(channel); | ||
| if (dataChannel) this._data = channel; | ||
| else this._data = null; | ||
| } else { this._data = channel; } | ||
| Config.save(this._dataDir, this._guild.id); | ||
| return this; | ||
| } | ||
| } | ||
|
|
||
| module.exports = Channel; |
| @@ -0,0 +1,47 @@ | ||
| const Config = require('../Config.js'); | ||
|
|
||
| class Number { | ||
| constructor(conf, data) { | ||
| Object.defineProperty(this, '_guild', { value: conf._guild }); | ||
| Object.defineProperty(this, '_dataDir', { value: conf._dataDir }); | ||
| Object.defineProperty(this, '_client', { value: conf._client }); | ||
| this.type = 'Number'; | ||
| if (typeof data !== 'number') this._data = 0; | ||
| else this._data = data; | ||
| this.min = data.min || NaN; | ||
| this.max = data.max || NaN; | ||
| } | ||
|
|
||
| get data() { | ||
| return this._data; | ||
| } | ||
|
|
||
| set data(data) { | ||
| if (data === undefined || !['number', 'string'].includes(typeof data)) throw 'Data must be a valid number or string that parses into a number.'; | ||
| data = parseFloat(data); | ||
| if (isNaN(data)) throw 'Provided value was not a number'; | ||
| if (this.min && !isNaN(this.min)) { | ||
| if (data < this.min) throw `Value provided was lower then the minimum ${this.min}`; | ||
| } | ||
| if (this.max && !isNaN(this.max)) { | ||
| if (data > this.max) throw `Value provided was higher than the maximum ${this.max}`; | ||
| } | ||
| this._data = data; | ||
| Config.save(this._dataDir, this._guild.id); | ||
| return this; | ||
| } | ||
|
|
||
| setMin(value) { | ||
| this.min = value; | ||
| Config.save(this._dataDir, this._guild.id); | ||
| return this; | ||
| } | ||
|
|
||
| setMax(value) { | ||
| this.max = value; | ||
| Config.save(this._dataDir, this._guild.id); | ||
| return this; | ||
| } | ||
| } | ||
|
|
||
| module.exports = Number; |
| @@ -0,0 +1,35 @@ | ||
| const Config = require('../Config.js'); | ||
| const Discord = require('discord.js'); | ||
|
|
||
| class Role { | ||
| constructor(conf, data) { | ||
| Object.defineProperty(this, '_guild', { value: conf._guild }); | ||
| Object.defineProperty(this, '_dataDir', { value: conf._dataDir }); | ||
| Object.defineProperty(this, '_client', { value: conf._client }); | ||
| this.type = 'Role'; | ||
| if (data instanceof Discord.Role) this._data = data.id; | ||
| if (typeof data === 'string') { | ||
| const dataRole = this._guild.roles.get(data); | ||
| if (dataRole) this._data = dataRole.id; | ||
| else this._data = this._guild.roles.find(role => role.name === data).id || null; | ||
| } else { this._data = null; } | ||
| } | ||
|
|
||
| get data() { | ||
| return this._guild.roles.get(this._data); | ||
| } | ||
|
|
||
| set data(value) { | ||
| if (value instanceof Discord.Role) { | ||
| this._data = value.id; | ||
| } else if (typeof value === 'string') { | ||
| const valueRole = this._guild.roles.get(value); | ||
| if (valueRole) this._data = valueRole.id; | ||
| else this._data = this._guild.roles.find(role => role.name === value).id || null; | ||
| } else { this._data = null; } | ||
| Config.save(this._dataDir, this._guild.id); | ||
| return this; | ||
| } | ||
| } | ||
|
|
||
| module.exports = Role; |
| @@ -0,0 +1,56 @@ | ||
| const Config = require('../Config.js'); | ||
|
|
||
| class String { | ||
| constructor(conf, data) { | ||
| Object.defineProperty(this, '_guild', { value: conf._guild }); | ||
| Object.defineProperty(this, '_dataDir', { value: conf._dataDir }); | ||
| Object.defineProperty(this, '_client', { value: conf._client }); | ||
| this.type = 'String'; | ||
| if (typeof data !== 'string') this._data = ''; | ||
| else this._data = data; | ||
| if (data.possibles) this.possibles = data.possibles; | ||
| else this.possibles = []; | ||
| } | ||
|
|
||
| get data() { | ||
| return this._data; | ||
| } | ||
|
|
||
| set data(data) { | ||
| if (typeof data !== 'string') throw 'Data must be a string'; | ||
| if (this.possibles.length !== 0 && !this.possibles.includes(data)) throw `That is not a valid option. Valid options: ${this.possibles.join(', ')}`; | ||
| else this._data = data; | ||
| Config.save(this._dataDir, this._guild.id); | ||
| return this; | ||
| } | ||
|
|
||
| add(value) { | ||
| if (!value === undefined || (!(value instanceof Array) && typeof value !== 'string')) throw 'Please supply a valid value (array, or string) to add to the possibles array.'; | ||
| if (value instanceof Array) { | ||
| value.forEach((val) => { | ||
| if (!this.possibles.includes(val)) this.possibles.push(val); | ||
| }); | ||
| } else { | ||
| if (this.possibles.includes(value)) throw 'That value already exists in the possibles array.'; | ||
| this.possibles.push(value); | ||
| } | ||
| Config.save(this._dataDir, this._guild.id); | ||
| return this; | ||
| } | ||
|
|
||
| del(value) { | ||
| if (!value === undefined || (!(value instanceof Array) && typeof value !== 'string')) throw 'Please supply a valid value (array, or string) to add to the possibles array.'; | ||
| if (value instanceof Array) { | ||
| value.forEach((val) => { | ||
| if (this.possibles.includes(val)) this.possibles.splice(this.possibles.indexOf(val), 1); | ||
| }); | ||
| } else { | ||
| if (!this.possibles.includes(value)) throw 'That value does not already exist in the possibles array.'; | ||
| this.possibles.splice(this.possibles.indexOf(value), 1); | ||
| } | ||
| Config.save(this._dataDir, this._guild.id); | ||
| return this; | ||
| } | ||
| } | ||
|
|
||
| module.exports = String; |
| @@ -0,0 +1,17 @@ | ||
| const Array = require('../Array.js'); | ||
| const Boolean = require('../Boolean.js'); | ||
| const Channel = require('../Channel.js'); | ||
| const Number = require('../Number.js'); | ||
| const Role = require('../Role.js'); | ||
| const String = require('../String.js'); | ||
|
|
||
| module.exports = { | ||
| Array: Array, | ||
| Boolean: Boolean, | ||
| Channel: Channel, | ||
| Number: Number, | ||
| Role: Role, | ||
| String: String | ||
| }; | ||
|
|
||
| // Could define a loader here to add custom user-defined types in the future |
| @@ -1,4 +1,4 @@ | ||
| exports.run = (client, guild) => { | ||
| if (!guild.available) return; | ||
| client.configuration.insert(guild); | ||
| }; |