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

View File

@ -9,6 +9,5 @@ module.exports = class GuildCreateEventListener extends EventListener {
async execute(guild) {
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) {
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({

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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: {
const data = {
id: this.id
}
};
return this.client.db.models.Guild.findOrCreate({
defaults: data,
where: data
});
}
};