From c70c9b249425a5b0e2605dc612aed9a9b0325809 Mon Sep 17 00:00:00 2001 From: Isaac Date: Fri, 21 May 2021 19:58:41 +0100 Subject: [PATCH] Guild settings --- src/commands/blacklist.js | 6 +++--- src/listeners/guildCreate.js | 1 - src/listeners/message.js | 3 +-- src/listeners/messageDelete.js | 3 +-- src/listeners/messageReactionAdd.js | 7 +++---- src/listeners/messageReactionRemove.js | 3 +-- src/listeners/messageUpdate.js | 3 +-- src/modules/commands/manager.js | 4 ++-- src/structures/guild.js | 16 ++++++---------- 9 files changed, 18 insertions(+), 28 deletions(-) diff --git a/src/commands/blacklist.js b/src/commands/blacklist.js index dac17ef..1f5f481 100644 --- a/src/commands/blacklist.js +++ b/src/commands/blacklist.js @@ -54,7 +54,7 @@ module.exports = class BlacklistCommand extends Command { if (member) id = member.id; else if (role) id = role.id; else if (/\d{17,19}/.test(input)) id = input; - else if (settings.blacklist.length === 0) { + else if (settings.blacklist?.length === 0) { return await message.channel.send( new MessageEmbed() .setColor(settings.colour) @@ -64,7 +64,7 @@ module.exports = class BlacklistCommand extends Command { ); } else { // list blacklisted members - const blacklist = settings.blacklist.map(element => { + const blacklist = settings.blacklist?.map(element => { const is_role = message.guild.roles.cache.has(element); if (is_role) return `» <@&${element}> (\`${element}\`)`; else return `» <@${element}> (\`${element}\`)`; @@ -80,7 +80,7 @@ module.exports = class BlacklistCommand extends Command { const is_role = role !== undefined || message.guild.roles.cache.has(id); const member_or_role = is_role ? 'role' : 'member'; - const index = settings.blacklist.findIndex(element => element === id); + const index = settings.blacklist?.findIndex(element => element === id); const new_blacklist = [...settings.blacklist]; diff --git a/src/listeners/guildCreate.js b/src/listeners/guildCreate.js index bcde138..df930d5 100644 --- a/src/listeners/guildCreate.js +++ b/src/listeners/guildCreate.js @@ -9,6 +9,5 @@ module.exports = class GuildCreateEventListener extends EventListener { async execute(guild) { this.client.log.info(`Added to "${guild.name}"`); - await guild.createSettings(); } }; \ No newline at end of file diff --git a/src/listeners/message.js b/src/listeners/message.js index a98adcd..d00e76a 100644 --- a/src/listeners/message.js +++ b/src/listeners/message.js @@ -13,8 +13,7 @@ module.exports = class MessageEventListener extends EventListener { async execute(message) { if (!message.guild) return; - let settings = await message.guild.settings; - if (!settings) settings = await message.guild.createSettings(); + const settings = await message.guild.settings; const i18n = this.client.i18n.getLocale(settings.locale); const t_row = await this.client.db.models.Ticket.findOne({ diff --git a/src/listeners/messageDelete.js b/src/listeners/messageDelete.js index e90efec..2a77660 100644 --- a/src/listeners/messageDelete.js +++ b/src/listeners/messageDelete.js @@ -10,8 +10,7 @@ module.exports = class MessageDeleteEventListener extends EventListener { async execute(message) { if (!message.guild) return; - let settings = await message.guild.settings; - if (!settings) settings = await message.guild.createSettings(); + const settings = await message.guild.settings; if (settings.log_messages && !message.system) this.client.tickets.archives.deleteMessage(message); // mark the message as deleted in the database (if it exists) } diff --git a/src/listeners/messageReactionAdd.js b/src/listeners/messageReactionAdd.js index 2a03db9..bcb6ac4 100644 --- a/src/listeners/messageReactionAdd.js +++ b/src/listeners/messageReactionAdd.js @@ -33,17 +33,16 @@ module.exports = class MessageReactionAddEventListener extends EventListener { const guild = r.message.guild; if (!guild) return; - let settings = await guild.settings; - if (!settings) settings = await guild.createSettings(); + const settings = await guild.settings; const i18n = this.client.i18n.getLocale(settings.locale); const channel = r.message.channel; const member = await guild.members.fetch(u.id); - if (settings.blacklist.includes(u.id)) { + if (settings.blacklist?.includes(u.id)) { return this.client.log.info(`Ignoring blacklisted member ${u.tag}`); } else { - settings.blacklist.forEach(element => { + settings.blacklist?.forEach(element => { if (guild.roles.cache.has(element) && member.roles.cache.has(element)) { return this.client.log.info(`Ignoring member ${u.tag} with blacklisted role`); } diff --git a/src/listeners/messageReactionRemove.js b/src/listeners/messageReactionRemove.js index 3045e92..68dcbac 100644 --- a/src/listeners/messageReactionRemove.js +++ b/src/listeners/messageReactionRemove.js @@ -32,8 +32,7 @@ module.exports = class MessageReactionRemoveEventListener extends EventListener const guild = r.message.guild; if (!guild) return; - let settings = await guild.settings; - if (!settings) settings = await guild.createSettings(); + const settings = await guild.settings; const i18n = this.client.i18n.getLocale(settings.locale); const channel = r.message.channel; diff --git a/src/listeners/messageUpdate.js b/src/listeners/messageUpdate.js index 03d489b..f4a6a5e 100644 --- a/src/listeners/messageUpdate.js +++ b/src/listeners/messageUpdate.js @@ -18,8 +18,7 @@ module.exports = class MessageUpdateEventListener extends EventListener { if (!newm.guild) return; - let settings = await newm.guild.settings; - if (!settings) settings = await newm.guild.createSettings(); + const settings = await newm.guild.settings; if (settings.log_messages && !newm.system) this.client.tickets.archives.updateMessage(newm); // update the message in the database } diff --git a/src/modules/commands/manager.js b/src/modules/commands/manager.js index 13faacd..caaf68c 100644 --- a/src/modules/commands/manager.js +++ b/src/modules/commands/manager.js @@ -73,11 +73,11 @@ module.exports = class CommandManager { const i18n = this.client.i18n.getLocale(settings.locale); let is_blacklisted = false; - if (settings.blacklist.includes(message.author.id)) { + if (settings.blacklist?.includes(message.author.id)) { is_blacklisted = true; this.client.log.info(`Ignoring blacklisted member ${message.author.tag}`); } else { - settings.blacklist.forEach(element => { + settings.blacklist?.forEach(element => { if (message.guild.roles.cache.has(element) && message.member.roles.cache.has(element)) { is_blacklisted = true; this.client.log.info(`Ignoring member ${message.author.tag} with blacklisted role`); diff --git a/src/structures/guild.js b/src/structures/guild.js index 3e11bba..68266e1 100644 --- a/src/structures/guild.js +++ b/src/structures/guild.js @@ -6,22 +6,18 @@ Structures.extend('Guild', Guild => { super(client, data); } - createSettings() { - return this.client.db.models.Guild.create({ - id: this.id - }); - } - async deleteSettings() { const row = await this.settings; return await row.destroy(); } get settings() { - return this.client.db.models.Guild.findOne({ - where: { - id: this.id - } + const data = { + id: this.id + }; + return this.client.db.models.Guild.findOrCreate({ + defaults: data, + where: data }); } };