refactor(listeners): improve code readability

This commit is contained in:
Isaac 2021-08-16 14:32:02 +01:00
parent 4c39dca8ef
commit 7adc2fcae6
No known key found for this signature in database
GPG Key ID: 87B9B6BD04EB7534
2 changed files with 38 additions and 38 deletions

View File

@ -8,49 +8,49 @@ module.exports = class MessageReactionAddEventListener extends EventListener {
super(client, { event: 'messageReactionAdd' });
}
async execute(r, u) {
async execute(reaction, user) {
if (r.partial) {
if (reaction.partial) {
try {
await r.fetch();
await reaction.fetch();
} catch (err) {
return this.client.log.error(err);
}
}
if (u.partial) {
if (user.partial) {
try {
await u.fetch();
await user.fetch();
} catch (err) {
return this.client.log.error(err);
}
}
if (u.id === this.client.user.id) return;
if (user.id === this.client.user.id) return;
const guild = r.message.guild;
const guild = reaction.message.guild;
if (!guild) return;
const settings = await guild.getSettings();
const i18n = this.client.i18n.getLocale(settings.locale);
const channel = r.message.channel;
const member = await guild.members.fetch(u.id);
const channel = reaction.message.channel;
const member = await guild.members.fetch(user.id);
if (settings.blacklist.includes(u.id)) {
return this.client.log.info(`Ignoring blacklisted member ${u.tag}`);
if (settings.blacklist.includes(user.id)) {
return this.client.log.info(`Ignoring blacklisted member ${user.tag}`);
} else {
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`);
return this.client.log.info(`Ignoring member ${user.tag} with blacklisted role`);
}
});
}
const t_row = await this.client.db.models.Ticket.findOne({ where: { id: channel.id } });
if (t_row && t_row.opening_message === r.message.id) {
if (r.emoji.name === '🙌' && await member.isStaff()) {
if (t_row && t_row.opening_message === reaction.message.id) {
if (reaction.emoji.name === '🙌' && await member.isStaff()) {
// ticket claiming
await t_row.update({ claimed_by: member.user.id });
@ -74,16 +74,16 @@ module.exports = class MessageReactionAddEventListener extends EventListener {
.setFooter(settings.footer, guild.iconURL())
);
} else {
await r.users.remove(u.id);
await reaction.users.remove(user.id);
}
} else {
const p_row = await this.client.db.models.Panel.findOne({ where: { message: r.message.id } });
const p_row = await this.client.db.models.Panel.findOne({ where: { message: reaction.message.id } });
if (p_row && typeof p_row.categories !== 'string') {
// panels
await r.users.remove(u.id);
await reaction.users.remove(user.id);
const category_id = p_row.categories[r.emoji.name];
const category_id = p_row.categories[reaction.emoji.name];
if (!category_id) return;
const cat_row = await this.client.db.models.Category.findOne({ where: { id: category_id } });
@ -91,7 +91,7 @@ module.exports = class MessageReactionAddEventListener extends EventListener {
const tickets = await this.client.db.models.Ticket.findAndCountAll({
where: {
category: cat_row.id,
creator: u.id,
creator: user.id,
open: true
}
});
@ -102,12 +102,12 @@ module.exports = class MessageReactionAddEventListener extends EventListener {
if (cat_row.max_per_member === 1) {
const embed = new MessageEmbed()
.setColor(settings.error_colour)
.setAuthor(u.username, u.displayAvatarURL())
.setAuthor(user.username, user.displayAvatarURL())
.setTitle(i18n('commands.new.response.has_a_ticket.title'))
.setDescription(i18n('commands.new.response.has_a_ticket.description', tickets.rows[0].id))
.setFooter(footer(settings.footer, i18n('message_will_be_deleted_in', 15)), guild.iconURL());
try {
response = await u.send(embed);
response = await user.send(embed);
} catch {
response = await channel.send(embed);
}
@ -123,28 +123,28 @@ module.exports = class MessageReactionAddEventListener extends EventListener {
});
const embed = new MessageEmbed()
.setColor(settings.error_colour)
.setAuthor(u.username, u.displayAvatarURL())
.setAuthor(user.username, user.displayAvatarURL())
.setTitle(i18n('commands.new.response.max_tickets.title', tickets.count))
.setDescription(i18n('commands.new.response.max_tickets.description', settings.command_prefix, list.join('\n')))
.setFooter(footer(settings.footer, i18n('message_will_be_deleted_in', 15)), u.iconURL());
.setFooter(footer(settings.footer, i18n('message_will_be_deleted_in', 15)), user.iconURL());
try {
response = await u.send(embed);
response = await user.send(embed);
} catch {
response = await channel.send(embed);
}
}
} else {
try {
await this.client.tickets.create(guild.id, u.id, cat_row.id);
await this.client.tickets.create(guild.id, user.id, cat_row.id);
} catch (error) {
const embed = new MessageEmbed()
.setColor(settings.error_colour)
.setAuthor(u.username, u.displayAvatarURL())
.setAuthor(user.username, user.displayAvatarURL())
.setTitle(i18n('commands.new.response.error.title'))
.setDescription(error.message)
.setFooter(footer(settings.footer, i18n('message_will_be_deleted_in', 15)), guild.iconURL());
try {
response = await u.send(embed);
response = await user.send(embed);
} catch {
response = await channel.send(embed);
}

View File

@ -7,39 +7,39 @@ module.exports = class MessageReactionRemoveEventListener extends EventListener
super(client, { event: 'messageReactionRemove' });
}
async execute(r, u) {
async execute(reaction, user) {
// release (unclaim) ticket
if (r.partial) {
if (reaction.partial) {
try {
await r.fetch();
await reaction.fetch();
} catch (err) {
return this.client.log.error(err);
}
}
if (u.partial) {
if (user.partial) {
try {
await u.fetch();
await user.fetch();
} catch (err) {
return this.client.log.error(err);
}
}
if (u.id === this.client.user.id) return;
if (user.id === this.client.user.id) return;
const guild = r.message.guild;
const guild = reaction.message.guild;
if (!guild) return;
const settings = await guild.getSettings();
const i18n = this.client.i18n.getLocale(settings.locale);
const channel = r.message.channel;
const member = await guild.members.fetch(u.id);
const channel = reaction.message.channel;
const member = await guild.members.fetch(user.id);
const t_row = await this.client.db.models.Ticket.findOne({ where: { id: channel.id } });
if (t_row && t_row.opening_message === r.message.id) {
if (r.emoji.name === '🙌' && await member.isStaff()) {
if (t_row && t_row.opening_message === reaction.message.id) {
if (reaction.emoji.name === '🙌' && await member.isStaff()) {
// ticket claiming
await t_row.update({ claimed_by: null });