Guild settings

This commit is contained in:
Isaac 2021-05-21 19:58:41 +01:00
parent be462cea1d
commit c70c9b2494
No known key found for this signature in database
GPG Key ID: F6812DBC6719B4E3
9 changed files with 18 additions and 28 deletions

View File

@ -54,7 +54,7 @@ module.exports = class BlacklistCommand extends Command {
if (member) id = member.id; if (member) id = member.id;
else if (role) id = role.id; else if (role) id = role.id;
else if (/\d{17,19}/.test(input)) id = input; 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( return await message.channel.send(
new MessageEmbed() new MessageEmbed()
.setColor(settings.colour) .setColor(settings.colour)
@ -64,7 +64,7 @@ module.exports = class BlacklistCommand extends Command {
); );
} else { } else {
// list blacklisted members // list blacklisted members
const blacklist = settings.blacklist.map(element => { const blacklist = settings.blacklist?.map(element => {
const is_role = message.guild.roles.cache.has(element); const is_role = message.guild.roles.cache.has(element);
if (is_role) return `» <@&${element}> (\`${element}\`)`; if (is_role) return `» <@&${element}> (\`${element}\`)`;
else 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 is_role = role !== undefined || message.guild.roles.cache.has(id);
const member_or_role = is_role ? 'role' : 'member'; 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]; const new_blacklist = [...settings.blacklist];

View File

@ -9,6 +9,5 @@ module.exports = class GuildCreateEventListener extends EventListener {
async execute(guild) { async execute(guild) {
this.client.log.info(`Added to "${guild.name}"`); this.client.log.info(`Added to "${guild.name}"`);
await guild.createSettings();
} }
}; };

View File

@ -13,8 +13,7 @@ module.exports = class MessageEventListener extends EventListener {
async execute(message) { async execute(message) {
if (!message.guild) return; if (!message.guild) return;
let settings = await message.guild.settings; const settings = await message.guild.settings;
if (!settings) settings = await message.guild.createSettings();
const i18n = this.client.i18n.getLocale(settings.locale); const i18n = this.client.i18n.getLocale(settings.locale);
const t_row = await this.client.db.models.Ticket.findOne({ const t_row = await this.client.db.models.Ticket.findOne({

View File

@ -10,8 +10,7 @@ module.exports = class MessageDeleteEventListener extends EventListener {
async execute(message) { async execute(message) {
if (!message.guild) return; if (!message.guild) return;
let settings = await message.guild.settings; const settings = await message.guild.settings;
if (!settings) settings = await message.guild.createSettings();
if (settings.log_messages && !message.system) this.client.tickets.archives.deleteMessage(message); // mark the message as deleted in the database (if it exists) if (settings.log_messages && !message.system) this.client.tickets.archives.deleteMessage(message); // mark the message as deleted in the database (if it exists)
} }

View File

@ -33,17 +33,16 @@ module.exports = class MessageReactionAddEventListener extends EventListener {
const guild = r.message.guild; const guild = r.message.guild;
if (!guild) return; if (!guild) return;
let settings = await guild.settings; const settings = await guild.settings;
if (!settings) settings = await guild.createSettings();
const i18n = this.client.i18n.getLocale(settings.locale); const i18n = this.client.i18n.getLocale(settings.locale);
const channel = r.message.channel; const channel = r.message.channel;
const member = await guild.members.fetch(u.id); 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}`); return this.client.log.info(`Ignoring blacklisted member ${u.tag}`);
} else { } else {
settings.blacklist.forEach(element => { settings.blacklist?.forEach(element => {
if (guild.roles.cache.has(element) && member.roles.cache.has(element)) { if (guild.roles.cache.has(element) && member.roles.cache.has(element)) {
return this.client.log.info(`Ignoring member ${u.tag} with blacklisted role`); return this.client.log.info(`Ignoring member ${u.tag} with blacklisted role`);
} }

View File

@ -32,8 +32,7 @@ module.exports = class MessageReactionRemoveEventListener extends EventListener
const guild = r.message.guild; const guild = r.message.guild;
if (!guild) return; if (!guild) return;
let settings = await guild.settings; const settings = await guild.settings;
if (!settings) settings = await guild.createSettings();
const i18n = this.client.i18n.getLocale(settings.locale); const i18n = this.client.i18n.getLocale(settings.locale);
const channel = r.message.channel; const channel = r.message.channel;

View File

@ -18,8 +18,7 @@ module.exports = class MessageUpdateEventListener extends EventListener {
if (!newm.guild) return; if (!newm.guild) return;
let settings = await newm.guild.settings; const settings = await newm.guild.settings;
if (!settings) settings = await newm.guild.createSettings();
if (settings.log_messages && !newm.system) this.client.tickets.archives.updateMessage(newm); // update the message in the database if (settings.log_messages && !newm.system) this.client.tickets.archives.updateMessage(newm); // update the message in the database
} }

View File

@ -73,11 +73,11 @@ module.exports = class CommandManager {
const i18n = this.client.i18n.getLocale(settings.locale); const i18n = this.client.i18n.getLocale(settings.locale);
let is_blacklisted = false; let is_blacklisted = false;
if (settings.blacklist.includes(message.author.id)) { if (settings.blacklist?.includes(message.author.id)) {
is_blacklisted = true; is_blacklisted = true;
this.client.log.info(`Ignoring blacklisted member ${message.author.tag}`); this.client.log.info(`Ignoring blacklisted member ${message.author.tag}`);
} else { } else {
settings.blacklist.forEach(element => { settings.blacklist?.forEach(element => {
if (message.guild.roles.cache.has(element) && message.member.roles.cache.has(element)) { if (message.guild.roles.cache.has(element) && message.member.roles.cache.has(element)) {
is_blacklisted = true; is_blacklisted = true;
this.client.log.info(`Ignoring member ${message.author.tag} with blacklisted role`); this.client.log.info(`Ignoring member ${message.author.tag} with blacklisted role`);

View File

@ -6,22 +6,18 @@ Structures.extend('Guild', Guild => {
super(client, data); super(client, data);
} }
createSettings() {
return this.client.db.models.Guild.create({
id: this.id
});
}
async deleteSettings() { async deleteSettings() {
const row = await this.settings; const row = await this.settings;
return await row.destroy(); return await row.destroy();
} }
get settings() { get settings() {
return this.client.db.models.Guild.findOne({ const data = {
where: {
id: this.id id: this.id
} };
return this.client.db.models.Guild.findOrCreate({
defaults: data,
where: data
}); });
} }
}; };