fix(bots/discord): parse larger units of durations, fix wrong timestamp in mod embed

This commit is contained in:
PalmDevs
2024-08-10 21:44:09 +07:00
parent 9897f244e0
commit 6c8dce0593
5 changed files with 16 additions and 13 deletions

View File

@@ -60,7 +60,7 @@ export default new ModerationCommand({
await applyRolePreset(member, 'mute', expires) await applyRolePreset(member, 'mute', expires)
await sendModerationReplyAndLogs( await sendModerationReplyAndLogs(
interaction, interaction,
createModerationActionEmbed('Muted', user, executor.user, reason, duration), createModerationActionEmbed('Muted', user, executor.user, reason, Math.ceil(expires / 1000)),
) )
if (duration) if (duration)

View File

@@ -83,6 +83,6 @@ export default new ModerationCommand({
removeRolePreset(member, preset) removeRolePreset(member, preset)
}, expires) }, 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)
}, },
}) })

View File

@@ -1,6 +1,6 @@
import { config, logger } from '$/context' import { config, logger } from '$/context'
import decancer from 'decancer' 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' import { applyReferenceToModerationActionEmbed, createModerationActionEmbed } from './embeds'
const PresetLogAction = { const PresetLogAction = {
@@ -10,7 +10,7 @@ const PresetLogAction = {
export const sendPresetReplyAndLogs = ( export const sendPresetReplyAndLogs = (
action: keyof typeof PresetLogAction, action: keyof typeof PresetLogAction,
interaction: ChatInputCommandInteraction | Message, interaction: CommandInteraction | Message,
executor: GuildMember, executor: GuildMember,
user: User, user: User,
preset: string, preset: string,
@@ -24,7 +24,7 @@ export const sendPresetReplyAndLogs = (
) )
export const sendModerationReplyAndLogs = async ( export const sendModerationReplyAndLogs = async (
interaction: ChatInputCommandInteraction | Message, interaction: CommandInteraction | Message,
embed: EmbedBuilder, embed: EmbedBuilder,
) => { ) => {
const reply = await interaction.reply({ embeds: [embed] }).then(it => it.fetch()) const reply = await interaction.reply({ embeds: [embed] }).then(it => it.fetch())

View File

@@ -6,9 +6,9 @@ import { and, eq } from 'drizzle-orm'
// TODO: Fix this type // TODO: Fix this type
type PresetKey = string 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 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 await database
.insert(appliedPresets) .insert(appliedPresets)

View File

@@ -1,13 +1,16 @@
export const parseDuration = (duration: string) => { export const parseDuration = (duration: string) => {
if (!duration.length) return Number.NaN 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 ( return (
(days || 0) * 24 * 60 * 60 * 1000 + (years || 0) * 290304e5 +
(hours || 0) * 60 * 60 * 1000 + (months || 0) * 24192e5 +
(minutes || 0) * 60 * 1000 + (weeks || 0) * 6048e5 +
(seconds || 0) * 1000 (days || 0) * 864e5 +
(hours || 0) * 36e5 +
(minutes || 0) * 6e4 +
(seconds || 0) * 1e3
) )
} }