mirror of
https://github.com/mollersuite/monofile.git
synced 2024-11-28 16:36:27 -08:00
idk
Co-authored-by: Jack W. <Jack5079@users.noreply.github.com>
This commit is contained in:
parent
f2deea4bc2
commit
2e29d40a1b
|
@ -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) {
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue