From 6c8dce059366a6ef85f5b8b1794c056515b9f5b6 Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Sat, 10 Aug 2024 21:44:09 +0700 Subject: [PATCH] fix(bots/discord): parse larger units of durations, fix wrong timestamp in mod embed --- bots/discord/src/commands/moderation/mute.ts | 2 +- .../src/commands/moderation/role-preset.ts | 2 +- bots/discord/src/utils/discord/moderation.ts | 6 +++--- bots/discord/src/utils/discord/rolePresets.ts | 4 ++-- bots/discord/src/utils/duration.ts | 15 +++++++++------ 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/bots/discord/src/commands/moderation/mute.ts b/bots/discord/src/commands/moderation/mute.ts index 2fa1313..2b4472f 100644 --- a/bots/discord/src/commands/moderation/mute.ts +++ b/bots/discord/src/commands/moderation/mute.ts @@ -60,7 +60,7 @@ export default new ModerationCommand({ await applyRolePreset(member, 'mute', expires) await sendModerationReplyAndLogs( interaction, - createModerationActionEmbed('Muted', user, executor.user, reason, duration), + createModerationActionEmbed('Muted', user, executor.user, reason, Math.ceil(expires / 1000)), ) if (duration) diff --git a/bots/discord/src/commands/moderation/role-preset.ts b/bots/discord/src/commands/moderation/role-preset.ts index aa33817..c65b1bb 100644 --- a/bots/discord/src/commands/moderation/role-preset.ts +++ b/bots/discord/src/commands/moderation/role-preset.ts @@ -83,6 +83,6 @@ export default new ModerationCommand({ removeRolePreset(member, preset) }, expires) - await sendPresetReplyAndLogs(apply ? 'apply' : 'remove', trigger, executor, user, preset, expires) + await sendPresetReplyAndLogs(apply ? 'apply' : 'remove', trigger, executor, user, preset, expires ? Math.ceil(expires / 1000) : undefined) }, }) diff --git a/bots/discord/src/utils/discord/moderation.ts b/bots/discord/src/utils/discord/moderation.ts index 9f8cb00..dbb0a9c 100644 --- a/bots/discord/src/utils/discord/moderation.ts +++ b/bots/discord/src/utils/discord/moderation.ts @@ -1,6 +1,6 @@ import { config, logger } from '$/context' import decancer from 'decancer' -import type { ChatInputCommandInteraction, EmbedBuilder, Guild, GuildMember, Message, User } from 'discord.js' +import type { CommandInteraction, EmbedBuilder, Guild, GuildMember, Message, User } from 'discord.js' import { applyReferenceToModerationActionEmbed, createModerationActionEmbed } from './embeds' const PresetLogAction = { @@ -10,7 +10,7 @@ const PresetLogAction = { export const sendPresetReplyAndLogs = ( action: keyof typeof PresetLogAction, - interaction: ChatInputCommandInteraction | Message, + interaction: CommandInteraction | Message, executor: GuildMember, user: User, preset: string, @@ -24,7 +24,7 @@ export const sendPresetReplyAndLogs = ( ) export const sendModerationReplyAndLogs = async ( - interaction: ChatInputCommandInteraction | Message, + interaction: CommandInteraction | Message, embed: EmbedBuilder, ) => { const reply = await interaction.reply({ embeds: [embed] }).then(it => it.fetch()) diff --git a/bots/discord/src/utils/discord/rolePresets.ts b/bots/discord/src/utils/discord/rolePresets.ts index 69c2fa5..42f1704 100644 --- a/bots/discord/src/utils/discord/rolePresets.ts +++ b/bots/discord/src/utils/discord/rolePresets.ts @@ -6,9 +6,9 @@ import { and, eq } from 'drizzle-orm' // TODO: Fix this type type PresetKey = string -export const applyRolePreset = async (member: GuildMember, presetName: PresetKey, untilMs: number) => { +export const applyRolePreset = async (member: GuildMember, presetName: PresetKey, expires: number) => { const afterInsert = await applyRolesUsingPreset(presetName, member, true) - const until = untilMs === Infinity ? null : Math.ceil(untilMs / 1000) + const until = expires === Infinity ? null : Math.ceil(expires / 1000) await database .insert(appliedPresets) diff --git a/bots/discord/src/utils/duration.ts b/bots/discord/src/utils/duration.ts index 4b5205c..64e672c 100644 --- a/bots/discord/src/utils/duration.ts +++ b/bots/discord/src/utils/duration.ts @@ -1,13 +1,16 @@ export const parseDuration = (duration: string) => { if (!duration.length) return Number.NaN - const matches = duration.match(/(?:(\d+)d)?(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s?)?/)! + const matches = duration.match(/(?:(\d+y)?(\d+M)?(\d+w)?(\d+)d)?(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s?)/)! - const [, days, hours, minutes, seconds] = matches.map(Number) + const [, years, months, weeks, days, hours, minutes, seconds] = matches.map(Number) return ( - (days || 0) * 24 * 60 * 60 * 1000 + - (hours || 0) * 60 * 60 * 1000 + - (minutes || 0) * 60 * 1000 + - (seconds || 0) * 1000 + (years || 0) * 290304e5 + + (months || 0) * 24192e5 + + (weeks || 0) * 6048e5 + + (days || 0) * 864e5 + + (hours || 0) * 36e5 + + (minutes || 0) * 6e4 + + (seconds || 0) * 1e3 ) }