Co-authored-by: Jack W. <Jack5079@users.noreply.github.com>
This commit is contained in:
May 2023-10-29 20:48:23 +00:00
parent f2deea4bc2
commit 2e29d40a1b
2 changed files with 32 additions and 2 deletions

View file

@ -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 )

View file

@ -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<APIMessage>)
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) {
}
}