diff options
author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2022-02-05 11:04:00 -0500 |
---|---|---|
committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2022-02-05 11:04:00 -0500 |
commit | 7fce00cec4d5ccb49636116b7eccc1e5c79fdd70 (patch) | |
tree | 199780acef010c01d298f3430a9d092394960590 | |
parent | 49b5e11f76072296ddc809d7ef7419b65abf6c2d (diff) | |
download | tanzanite-7fce00cec4d5ccb49636116b7eccc1e5c79fdd70.tar.gz tanzanite-7fce00cec4d5ccb49636116b7eccc1e5c79fdd70.tar.bz2 tanzanite-7fce00cec4d5ccb49636116b7eccc1e5c79fdd70.zip |
make bushlevel update send error messages to guild error logs
-rw-r--r-- | src/listeners/member-custom/bushLevelUpdate.ts | 73 |
1 files changed, 42 insertions, 31 deletions
diff --git a/src/listeners/member-custom/bushLevelUpdate.ts b/src/listeners/member-custom/bushLevelUpdate.ts index d0dc4a5..c43e341 100644 --- a/src/listeners/member-custom/bushLevelUpdate.ts +++ b/src/listeners/member-custom/bushLevelUpdate.ts @@ -1,6 +1,8 @@ import { BushListener, type BushClientEvents } from '#lib'; import { type TextChannel } from 'discord.js'; +type Args = BushClientEvents['bushLevelUpdate']; + export default class BushLevelUpdateListener extends BushListener { public constructor() { super('bushLevelUpdate', { @@ -10,39 +12,48 @@ export default class BushLevelUpdateListener extends BushListener { }); } - public override async exec(...[member, _oldLevel, newLevel, _currentXp, message]: BushClientEvents['bushLevelUpdate']) { - if (await message.guild.hasFeature('sendLevelUpMessages')) { - void (async () => { - const channel = ((await message.guild.channels - .fetch((await message.guild.getSetting('levelUpChannel')) ?? message.channelId) - .catch(() => null)) ?? message.channel) as TextChannel; + public override async exec(...[member, _oldLevel, newLevel, _currentXp, message]: Args) { + void this.sendLevelUpMessages(member, newLevel, message); + void this.assignLevelRoles(member, newLevel, message); + } - const success = await channel - .send(`${util.format.input(member.user.tag)} leveled up to level ${util.format.input(`${newLevel}`)}.`) - .catch(() => null); + private async sendLevelUpMessages(member: Args[0], newLevel: Args[2], message: Args[4]) { + if (!(await message.guild.hasFeature('sendLevelUpMessages'))) return; - if (!success) await client.console.warn('bushLevelUpdate', `Could not send level up message in ${message.guild}`); - })(); - } - void (async () => { - const levelRoles = await message.guild.getSetting('levelRoles'); - if (Object.keys(levelRoles).length) { - const promises = []; - for (let i = 1; i <= newLevel; i++) { - if (levelRoles[i]) { - if (member.roles.cache.has(levelRoles[i])) continue; - else promises.push(member.roles.add(levelRoles[i], `[LevelRoles] Role given for reaching level ${i}`)); - } - } - try { - if (promises.length) await Promise.all(promises); - } catch (e) { - await member.guild.error( - 'bushLevelUpdate', - `There was an error adding level roles to ${member.user.tag} upon reaching to level ${newLevel}.\n${e?.message ?? e}` - ); - } + const channel = ((await message.guild.channels + .fetch((await message.guild.getSetting('levelUpChannel')) ?? message.channelId) + .catch(() => null)) ?? message.channel) as TextChannel; + + const success = await channel + .send(`${util.format.input(member.user.tag)} leveled up to level ${util.format.input(`${newLevel}`)}.`) + .catch(() => null); + + if (!success) + await message.guild.error( + 'bushLevelUpdate', + `Could not send level up message for ${member.user.tag} in <#${message.channel.id}>.` + ); + } + + private async assignLevelRoles(member: Args[0], newLevel: Args[2], message: Args[4]) { + const levelRoles = await message.guild.getSetting('levelRoles'); + + if (!Object.keys(levelRoles).length) return; + + const promises = []; + for (let i = 1; i <= newLevel; i++) { + if (levelRoles[i]) { + if (member.roles.cache.has(levelRoles[i])) continue; + else promises.push(member.roles.add(levelRoles[i], `[LevelRoles] Role given for reaching level ${i}`)); } - })(); + } + try { + if (promises.length) await Promise.all(promises); + } catch (e) { + await member.guild.error( + 'bushLevelUpdate', + `There was an error adding level roles to ${member.user.tag} upon reaching to level ${newLevel}.\n${e?.message ?? e}` + ); + } } } |