Webhook addition for Message (#7)

* Add webhook handling for Message

* formatting
This commit is contained in:
Nicolas 2022-07-02 18:08:37 -03:00 committed by GitHub
parent aa3c2724d3
commit 78b8cf9da0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 15 deletions

View File

@ -101,7 +101,7 @@ export class Member implements Model {
let url: string; let url: string;
if (this.user.bot) { if (this.user.bot) {
return this.user.avatarURL() return this.user.avatarURL();
} }
if (!this.avatarHash) { if (!this.avatarHash) {
@ -110,7 +110,7 @@ export class Member implements Model {
url = Routes.USER_AVATAR(this.user.id, iconBigintToHash(this.avatarHash)); url = Routes.USER_AVATAR(this.user.id, iconBigintToHash(this.avatarHash));
} }
return formatImageURL(url, options.size, options.format) return formatImageURL(url, options.size, options.format);
} }
toString() { toString() {

View File

@ -15,6 +15,7 @@ import User from "./User.ts";
import Member from "./Member.ts"; import Member from "./Member.ts";
import Attachment from "./Attachment.ts"; import Attachment from "./Attachment.ts";
import ComponentFactory from "./components/ComponentFactory.ts"; import ComponentFactory from "./components/ComponentFactory.ts";
import { iconHashToBigInt } from "../util/hash.ts";
import * as Routes from "../util/Routes.ts"; import * as Routes from "../util/Routes.ts";
/** /**
@ -57,6 +58,13 @@ export type ReactionResolvable = string | {
id: Snowflake; id: Snowflake;
}; };
export interface WebhookAuthor {
id: string;
username: string;
discriminator: string;
avatar?: bigint;
}
/** /**
* Represents a message * Represents a message
* @link https://discord.com/developers/docs/resources/channel#message-object * @link https://discord.com/developers/docs/resources/channel#message-object
@ -77,9 +85,19 @@ export class Message implements Model {
this.attachments = data.attachments.map((attachment) => new Attachment(session, attachment)); this.attachments = data.attachments.map((attachment) => new Attachment(session, attachment));
// webhook handling
if (data.author && data.author.discriminator === "0000") {
this.webhook = {
id: data.author.id,
username: data.author.username,
discriminator: data.author.discriminator,
avatar: data.author.avatar ? iconHashToBigInt(data.author.avatar) : undefined,
};
}
// user is always null on MessageCreate and its replaced with author // user is always null on MessageCreate and its replaced with author
if (data.guild_id && data.member) { if (data.guild_id && data.member && data.author && !this.isWebhookMessage()) {
this.member = new Member(session, { ...data.member, user: data.author }, data.guild_id); this.member = new Member(session, { ...data.member, user: data.author }, data.guild_id);
} }
@ -101,6 +119,8 @@ export class Message implements Model {
member?: Member; member?: Member;
components?: Component[]; components?: Component[];
webhook?: WebhookAuthor;
get url() { get url() {
return `https://discord.com/channels/${this.guildId ?? "@me"}/${this.channelId}/${this.id}`; return `https://discord.com/channels/${this.guildId ?? "@me"}/${this.channelId}/${this.id}`;
} }
@ -284,6 +304,11 @@ export class Message implements Model {
inGuild(): this is { guildId: Snowflake } & Message { inGuild(): this is { guildId: Snowflake } & Message {
return !!this.guildId; return !!this.guildId;
} }
/** isWebhookMessage if the messages comes from a Webhook */
isWebhookMessage(): this is User & { author: Partial<User>; webhook: WebhookAuthor; member: undefined } {
return !!this.webhook;
}
} }
export default Message; export default Message;

View File

@ -1,21 +1,17 @@
import type { import type { DiscordEmbed, DiscordEmbedField, DiscordEmbedProvider } from "../../vendor/external.ts";
DiscordEmbedField,
DiscordEmbed,
DiscordEmbedProvider
} from '../../vendor/external.ts';
export interface EmbedFooter { export interface EmbedFooter {
text: string; text: string;
iconUrl?: string; iconUrl?: string;
proxyIconUrl?: string proxyIconUrl?: string;
} }
export interface EmbedAuthor { export interface EmbedAuthor {
name: string name: string;
text?: string; text?: string;
url?: string; url?: string;
iconUrl?: string; iconUrl?: string;
proxyIconUrl?: string proxyIconUrl?: string;
} }
export interface EmbedVideo { export interface EmbedVideo {
@ -26,10 +22,10 @@ export interface EmbedVideo {
} }
export class EmbedBuilder { export class EmbedBuilder {
#data: DiscordEmbed #data: DiscordEmbed;
constructor(data: DiscordEmbed = {}) { constructor(data: DiscordEmbed = {}) {
this.#data = data; this.#data = data;
if (!this.#data.fields) this.#data.fields = [] if (!this.#data.fields) this.#data.fields = [];
} }
setAuthor(author: EmbedAuthor) { setAuthor(author: EmbedAuthor) {
@ -37,7 +33,7 @@ export class EmbedBuilder {
name: author.name, name: author.name,
icon_url: author.iconUrl, icon_url: author.iconUrl,
proxy_icon_url: author.proxyIconUrl, proxy_icon_url: author.proxyIconUrl,
url: author.url url: author.url,
}; };
return this; return this;
} }
@ -102,7 +98,7 @@ export class EmbedBuilder {
height: video.height, height: video.height,
proxy_url: video.proxyUrl, proxy_url: video.proxyUrl,
url: video.url, url: video.url,
width: video.width width: video.width,
}; };
return this; return this;
} }