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

View File

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