From 2e29d40a1b5d7b6939febd95d5159625e9ab985a Mon Sep 17 00:00:00 2001 From: stringsplit <77242831+nbitzz@users.noreply.github.com> Date: Sun, 29 Oct 2023 20:48:23 +0000 Subject: [PATCH] idk Co-authored-by: Jack W. --- src/server/lib/DiscordAPI/DiscordRequests.ts | 2 +- src/server/lib/DiscordAPI/index.ts | 32 +++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/server/lib/DiscordAPI/DiscordRequests.ts b/src/server/lib/DiscordAPI/DiscordRequests.ts index c26776f..f7f1c77 100644 --- a/src/server/lib/DiscordAPI/DiscordRequests.ts +++ b/src/server/lib/DiscordAPI/DiscordRequests.ts @@ -204,7 +204,7 @@ export class REST { return this.queue(path, options) /* it was ratelimited after all getBucket() would have generated a DiscordAPIBucket so this would be fine */ - } + } /* commented out cause i feel like it'll cause issues // let's update the bucket with data from the source now let rd = extractRatelimitData( response.headers ) diff --git a/src/server/lib/DiscordAPI/index.ts b/src/server/lib/DiscordAPI/index.ts index 9f7f90f..b31eca5 100644 --- a/src/server/lib/DiscordAPI/index.ts +++ b/src/server/lib/DiscordAPI/index.ts @@ -2,6 +2,14 @@ import { REST } from "./DiscordRequests" import type { APIMessage } from "discord-api-types/v10" const EXPIRE_AFTER = 20 * 60 * 1000 +const DISCORD_EPOCH = 1420070400000 +// Converts a snowflake ID string into a JS Date object using the provided epoch (in ms), or Discord's epoch if not provided +function convertSnowflakeToDate(snowflake: string|number, epoch = DISCORD_EPOCH) { + // Convert snowflake to BigInt to extract timestamp bits + // https://discord.com/developers/docs/reference#snowflakes + const milliseconds = BigInt(snowflake) >> 22n + return new Date(Number(milliseconds) + epoch) +} interface MessageCacheObject { expire: number, @@ -20,12 +28,34 @@ export class Client { this.targetChannel = targetChannel } - fetchMessage(id: string, cache: boolean = true) { + async fetchMessage(id: string, cache: boolean = true) { if (cache && this.messageCache.has(id)) { let cachedMessage = this.messageCache.get(id)! if (cachedMessage.expire >= Date.now()) { return cachedMessage.object } } + + let message = await (this.rest.fetch(`/channels/${this.targetChannel}/messages/${id}`).then(res=>res.json()) as Promise) + + this.messageCache.set(id, { object: message, expire: EXPIRE_AFTER + Date.now() }) + return message + } + + async deleteMessage(id: string) { + await this.rest.fetch(`/channels/${this.targetChannel}/messages/${id}`, {method: "DELETE"}) + this.messageCache.delete(id) + } + + // https://discord.com/developers/docs/resources/channel#bulk-delete-messages + // "This endpoint will not delete messages older than 2 weeks" so we need to check each id + async deleteMessages(ids: string[]) { + // TODO check if any are older than two weeks + await this.rest.fetch(`/channels/${this.targetChannel}/messages/bulk-delete`, {method: "POST",body: JSON.stringify({messages: ids})}) + ids.forEach(Map.prototype.delete.bind(this.messageCache)) + } + + async sendDataMessage(formData: FormData) { + } } \ No newline at end of file