Skip to content

Commit ad84942

Browse files
committed
Upgrade some code to temporal
1 parent 3cbecd0 commit ad84942

13 files changed

Lines changed: 51 additions & 28 deletions

File tree

src/commands/vote.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { parseArgs, type ParseArgsConfig } from "node:util";
22

33
import { cleanContent } from "discord.js";
4+
import { Temporal } from "@js-temporal/polyfill";
45

56
import type { MessageCommand } from "#commands/command.ts";
67
import type { BotContext } from "#context.ts";
@@ -53,7 +54,7 @@ Optionen:
5354

5455
const embed = {
5556
description: `**${cleanContent(question, message.channel)}**`,
56-
timestamp: new Date().toISOString(),
57+
timestamp: Temporal.Now.instant().toString(),
5758
color: 0x9400d3,
5859
author: {
5960
name: `Umfrage von ${message.author.username}`,

src/commands/woisvote.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export default class WoisCommand implements ApplicationCommand {
138138
const result = await woisAction.insertWoisAction(
139139
woisMessage,
140140
reason,
141-
new Date(timeForWois.toString()),
141+
new Date(timeForWois.toString()).toTemporalInstant(), // TODO
142142
isWoisgangVote,
143143
);
144144
if (!result) {
@@ -206,7 +206,7 @@ export const woisVoteReactionHandler: ReactionHandler = {
206206
};
207207

208208
export const woisVoteScheduler = async (context: BotContext): Promise<void> => {
209-
const pendingAction = await woisAction.getPendingWoisAction(new Date());
209+
const pendingAction = await woisAction.getPendingWoisAction(Temporal.Now.instant());
210210
if (pendingAction === undefined) {
211211
return;
212212
}

src/handler/reaction/quoteHandler.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
type GuildTextBasedChannel,
1313
type ApplicationEmoji,
1414
} from "discord.js";
15+
import { Temporal } from "@js-temporal/polyfill";
1516

1617
import type { BotContext, QuoteConfig } from "#context.ts";
1718
import type { ReactionHandler } from "../ReactionHandler.ts";
@@ -142,7 +143,9 @@ const createQuote = async (
142143
color: randomizedColor,
143144
description: quotedMessage.content,
144145
author: await getAuthor(quotedUser),
145-
timestamp: new Date(quotedMessage.createdTimestamp).toISOString(),
146+
timestamp: Temporal.Instant.fromEpochMilliseconds(
147+
quotedMessage.createdTimestamp,
148+
).toString(),
146149
fields: [
147150
{
148151
name: "Link zur Nachricht",

src/service/ban.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { time, type GuildMember, type User, TimestampStyles } from "discord.js";
22
import * as sentry from "@sentry/node";
3+
import { Temporal } from "@js-temporal/polyfill";
34

45
import type { BotContext } from "#context.ts";
56
import * as ban from "#storage/ban.ts";
@@ -8,7 +9,7 @@ import { formatDuration } from "#utils/dateUtils.ts";
89
import log from "#log";
910

1011
export async function processBans(context: BotContext) {
11-
const now = new Date();
12+
const now = Temporal.Now.instant();
1213

1314
try {
1415
const expiredBans = await ban.findExpiredBans(now);
@@ -85,7 +86,7 @@ export async function banUser(
8586
? null // infinite ban
8687
: new Date(Date.now() + durationInHours * 60 * 60 * 1000);
8788

88-
await ban.persistOrUpdate(member, unbanAt, isSelfBan, reason);
89+
await ban.persistOrUpdate(member, unbanAt?.toTemporalInstant() ?? null, isSelfBan, reason);
8990

9091
const humanReadableDuration = durationInHours
9192
? formatDuration(durationInHours * 60 * 60)

src/service/fadingMessage.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import type { TextChannel } from "discord.js";
22
import * as sentry from "@sentry/node";
3+
import { Temporal } from "@js-temporal/polyfill";
34

45
import type { BotContext } from "#context.ts";
56

67
import log from "#log";
78
import * as fadingMessage from "#storage/fadingMessage.ts";
89

910
export async function handleFadingMessages(context: BotContext) {
10-
const now = new Date();
11+
const now = Temporal.Now.instant();
1112
const fadingMessages = await fadingMessage.findPendingForDeletion(now);
1213
const toRemove = [];
1314

src/service/loot.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { User, Snowflake, Message } from "discord.js";
2+
import { Temporal } from "@js-temporal/polyfill";
23

34
import type { LootId, LootInsertable, LootOrigin } from "#storage/db/model.ts";
45
import type { LootAttributeKindId, LootKindId } from "./lootData.ts";
@@ -86,7 +87,7 @@ export async function createLoot(
8687
template,
8788
winner,
8889
message,
89-
new Date(),
90+
Temporal.Now.instant(),
9091
origin,
9192
predecessorLootId,
9293
rarityAttribute,

src/service/pollEmbed.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
TimestampStyles,
99
type User,
1010
} from "discord.js";
11+
import { Temporal } from "@js-temporal/polyfill";
1112

1213
export const LETTERS = [
1314
":regional_indicator_a:",
@@ -97,7 +98,7 @@ export function buildPollEmbed(
9798
o.author?.username !== poll.author.username,
9899
),
99100
),
100-
timestamp: new Date().toISOString(),
101+
timestamp: Temporal.Now.instant().toString(),
101102
author: {
102103
name: `${poll.multipleChoices ? "Umfrage" : "Strawpoll"} von ${poll.author.username}`,
103104
icon_url: poll.author.iconURL,
@@ -182,7 +183,7 @@ export function buildDelayedPollResultEmbed(
182183
value: option.chosenBy.map(user => user.toString()).join("\n") || "-",
183184
inline: false,
184185
})),
185-
timestamp: new Date().toISOString(),
186+
timestamp: Temporal.Now.instant().toString(),
186187
author: {
187188
name: author.name,
188189
icon_url: author.iconURL,

src/service/roll.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { cleanContent, type APIEmbed, type TextChannel, type User } from "discord.js";
2+
import { Temporal } from "@js-temporal/polyfill";
23

34
export async function rollInChannel(
45
author: User,
@@ -9,7 +10,7 @@ export async function rollInChannel(
910
const diceName = `${throwCount}d${diceSides}`;
1011
const embed = {
1112
title: cleanContent(`${diceName}:`, targetChannel),
12-
timestamp: new Date().toISOString(),
13+
timestamp: Temporal.Now.instant().toString(),
1314
author: {
1415
name: `Würfelergebnis für ${author.username}`,
1516
icon_url: author.displayAvatarURL(),

src/storage/ban.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import type { GuildMember, Snowflake, User } from "discord.js";
2+
import type { Temporal } from "@js-temporal/polyfill";
23

34
import type { Ban } from "./db/model.ts";
45
import db from "#db";
56
import log from "#log";
67

78
export async function persistOrUpdate(
89
user: GuildMember,
9-
until: Date | null,
10+
until: Temporal.Instant | null,
1011
isSelfBan: boolean,
1112
reason: string | null = null,
1213
ctx = db(),
@@ -15,7 +16,7 @@ export async function persistOrUpdate(
1516
`Saving Ban for user ${user} until ${until} (is self ban: ${isSelfBan}, reason: ${reason})`,
1617
);
1718

18-
const bannedUntil = until?.toISOString();
19+
const bannedUntil = until?.toString();
1920
await ctx
2021
.insertInto("bans")
2122
.values({
@@ -42,11 +43,11 @@ export async function remove(userId: Snowflake, ctx = db()) {
4243
await ctx.deleteFrom("bans").where("userId", "=", userId).execute();
4344
}
4445

45-
export async function findExpiredBans(now: Date, ctx = db()): Promise<Ban[]> {
46+
export async function findExpiredBans(now: Temporal.Instant, ctx = db()): Promise<Ban[]> {
4647
return ctx
4748
.selectFrom("bans")
4849
.where("bannedUntil", "is not", null)
49-
.where("bannedUntil", "<=", now.toISOString())
50+
.where("bannedUntil", "<=", now.toString())
5051
.selectAll()
5152
.execute();
5253
}

src/storage/emote.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { sql } from "kysely";
2+
import { Temporal } from "@js-temporal/polyfill";
23
import type { Message, Snowflake } from "discord.js";
34

45
import type { Emote } from "./db/model.ts";
@@ -95,7 +96,7 @@ export async function ensureEmote(
9596
export async function markAsDeleted(emoteId: Emote["id"], ctx = db()): Promise<void> {
9697
await ctx
9798
.updateTable("emote")
98-
.set("deletedAt", new Date().toISOString())
99+
.set("deletedAt", Temporal.Now.instant().toString())
99100
.where("id", "=", emoteId)
100101
.execute();
101102
}

0 commit comments

Comments
 (0)