diff options
Diffstat (limited to 'src')
-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}` + ); + } } } |