From 61bd1297cd1208a6d6b63c2a38ae9851b22f27ff Mon Sep 17 00:00:00 2001 From: Paul Makles Date: Sat, 3 Jun 2023 19:15:02 +0100 Subject: [PATCH] feat: backport webhooks --- src/maps/Messages.ts | 28 +++++++++++++++++++--------- src/websocket/client.ts | 2 +- src/websocket/notifications.ts | 4 +++- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/maps/Messages.ts b/src/maps/Messages.ts index dbc6ffe2..c5c19eda 100644 --- a/src/maps/Messages.ts +++ b/src/maps/Messages.ts @@ -31,6 +31,7 @@ export class Message { nonce?: string; channel_id: string; author_id: string; + webhook?: { name: string; avatar?: string }; content: Nullable; system: Nullable; @@ -90,7 +91,12 @@ export class Message { * Get the username for this message. */ get username() { - return this.masquerade?.name ?? this.member?.nickname ?? this.author?.username; + return ( + this.masquerade?.name ?? + this.webhook?.name ?? + this.member?.nickname ?? + this.author?.username + ); } /** @@ -104,20 +110,22 @@ export class Message { * Get the avatar URL for this message. */ get avatarURL() { - return this.generateMasqAvatarURL() - ?? (this.member - ? this.member?.avatarURL - : this.author?.avatarURL); + return this.generateMasqAvatarURL() ?? this.webhook?.avatar + ? `https://autumn.revolt.chat/avatars/${this.webhook?.avatar}` + : this.member + ? this.member?.avatarURL + : this.author?.avatarURL; } /** * Get the animated avatar URL for this message. */ get animatedAvatarURL() { - return this.generateMasqAvatarURL() - ?? (this.member - ? this.member?.animatedAvatarURL - : this.author?.animatedAvatarURL); + return this.generateMasqAvatarURL() ?? this.webhook?.avatar + ? `https://autumn.revolt.chat/avatars/${this.webhook?.avatar}` + : this.member + ? this.member?.animatedAvatarURL + : this.author?.animatedAvatarURL; } @computed generateMasqAvatarURL() { @@ -164,6 +172,7 @@ export class Message { this.nonce = data.nonce ?? undefined; this.channel_id = data.channel; this.author_id = data.author; + this.webhook = toNullable((data as any).webhook); this.content = toNullable(data.content); this.system = toNullable(data.system); @@ -211,6 +220,7 @@ export class Message { } }; + apply("webhook"); apply("content"); apply("attachments"); apply("edited", undefined, toNullableDate as (obj: unknown) => unknown); diff --git a/src/websocket/client.ts b/src/websocket/client.ts index fa2d9c07..9bf61416 100644 --- a/src/websocket/client.ts +++ b/src/websocket/client.ts @@ -238,7 +238,7 @@ export class WebSocketClient { break; } } - } else { + } else if (!packet.webhook) { await this.client.users.fetch( packet.author, ); diff --git a/src/websocket/notifications.ts b/src/websocket/notifications.ts index 1c1b9c13..15ecb026 100644 --- a/src/websocket/notifications.ts +++ b/src/websocket/notifications.ts @@ -42,7 +42,9 @@ export type ClientboundNotification = | ({ type: "Error" } & WebSocketError) | { type: "Authenticated" } | ReadyPacket - | ({ type: "Message" } & Message) + | ({ type: "Message" } & Message & { + webhook?: { name: string; avatar?: string }; + }) | { type: "MessageUpdate"; id: string;