|
| 1 | +import { GuildSettings, readSettings } from '#lib/database'; |
| 2 | +import { Events } from '#lib/types'; |
| 3 | +import { getLogger, getModeration } from '#utils/functions'; |
| 4 | +import { TypeMetadata, TypeVariation } from '#utils/moderationConstants'; |
| 5 | +import { ApplyOptions } from '@sapphire/decorators'; |
| 6 | +import { Listener } from '@sapphire/framework'; |
| 7 | +import { isNumber } from '@sapphire/utilities'; |
| 8 | +import type { GuildMember } from 'discord.js'; |
| 9 | + |
| 10 | +@ApplyOptions<Listener.Options>({ event: Events.GuildMemberUpdate }) |
| 11 | +export class UserListener extends Listener { |
| 12 | + public async run(previous: GuildMember, next: GuildMember) { |
| 13 | + const prevTimeout = this.#getTimeout(previous); |
| 14 | + const nextTimeout = this.#getTimeout(next); |
| 15 | + if (prevTimeout === nextTimeout) return; |
| 16 | + |
| 17 | + const { user, guild } = next; |
| 18 | + const logger = getLogger(guild); |
| 19 | + const actionBySkyra = logger.timeout.isSet(guild.id); |
| 20 | + const contextPromise = logger.timeout.wait(guild.id); |
| 21 | + |
| 22 | + // If the action was done by Skyra, or external timeout is enabled, create a moderation action: |
| 23 | + if (actionBySkyra || (await readSettings(next, GuildSettings.Events.Timeout))) { |
| 24 | + const context = await contextPromise; |
| 25 | + const moderation = getModeration(guild); |
| 26 | + await moderation.waitLock(); |
| 27 | + await moderation |
| 28 | + .create({ |
| 29 | + userId: user.id, |
| 30 | + moderatorId: context?.userId ?? this.container.client.id!, |
| 31 | + type: TypeVariation.Timeout, |
| 32 | + metadata: nextTimeout ? TypeMetadata.Temporary : TypeMetadata.Appeal, |
| 33 | + duration: nextTimeout, |
| 34 | + reason: context?.reason |
| 35 | + }) |
| 36 | + .create(); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + #getTimeout(member: GuildMember) { |
| 41 | + const timeout = member.communicationDisabledUntilTimestamp; |
| 42 | + return isNumber(timeout) && timeout >= Date.now() ? timeout : null; |
| 43 | + } |
| 44 | +} |
0 commit comments