fix(webhooks): revert webhook caching (and returning Message) (#8038)

This commit is contained in:
pat
2022-06-17 16:25:46 -05:00
committed by GitHub
parent e5ec1c4dbc
commit d54bf5d286
3 changed files with 42 additions and 15 deletions

View File

@@ -29,10 +29,26 @@ class InteractionWebhook {
/** /**
* Sends a message with this webhook. * Sends a message with this webhook.
* @param {string|MessagePayload|InteractionReplyOptions} options The content for the reply * @param {string|MessagePayload|InteractionReplyOptions} options The content for the reply
* @returns {Promise<Message|APIMessage>} * @returns {Promise<Message>}
*/ */
send() {} send() {}
/**
* Gets a message that was sent by this webhook.
* @param {Snowflake|'@original'} message The id of the message to fetch
* @returns {Promise<Message>} Returns the message sent by this webhook
*/
fetchMessage() {} fetchMessage() {}
/**
* Edits a message that was sent by this webhook.
* @param {MessageResolvable|'@original'} message The message to edit
* @param {string|MessagePayload|WebhookEditMessageOptions} options The options to provide
* @returns {Promise<Message>} Returns the message edited by this webhook
*/
editMessage() {} editMessage() {}
deleteMessage() {} deleteMessage() {}
get url() {} get url() {}

View File

@@ -148,7 +148,7 @@ class Webhook {
/** /**
* Sends a message with this webhook. * Sends a message with this webhook.
* @param {string|MessagePayload|WebhookMessageOptions} options The options to provide * @param {string|MessagePayload|WebhookMessageOptions} options The options to provide
* @returns {Promise<Message>} * @returns {Promise<APIMessage>}
* @example * @example
* // Send a basic message * // Send a basic message
* webhook.send('hello!') * webhook.send('hello!')
@@ -211,7 +211,9 @@ class Webhook {
const { body, files } = await messagePayload.resolveFiles(); const { body, files } = await messagePayload.resolveFiles();
const d = await this.client.rest.post(Routes.webhook(this.id, this.token), { body, files, query, auth: false }); const d = await this.client.rest.post(Routes.webhook(this.id, this.token), { body, files, query, auth: false });
return this.client.channels?.cache.get(d.channel_id)?.messages._add(d, false) ?? new (getMessage())(this.client, d);
if (!this.client.channels) return d;
return this.client.channels.cache.get(d.channel_id)?.messages._add(d, false) ?? new (getMessage())(this.client, d);
} }
/** /**
@@ -286,17 +288,19 @@ class Webhook {
* Gets a message that was sent by this webhook. * Gets a message that was sent by this webhook.
* @param {Snowflake|'@original'} message The id of the message to fetch * @param {Snowflake|'@original'} message The id of the message to fetch
* @param {WebhookFetchMessageOptions} [options={}] The options to provide to fetch the message. * @param {WebhookFetchMessageOptions} [options={}] The options to provide to fetch the message.
* @returns {Promise<Message>} Returns the message sent by this webhook * @returns {Promise<APIMessage>} Returns the message sent by this webhook
*/ */
async fetchMessage(message, { cache = true, threadId } = {}) { async fetchMessage(message, { threadId } = {}) {
if (!this.token) throw new Error('WEBHOOK_TOKEN_UNAVAILABLE'); if (!this.token) throw new Error('WEBHOOK_TOKEN_UNAVAILABLE');
const data = await this.client.rest.get(Routes.webhookMessage(this.id, this.token, message), { const data = await this.client.rest.get(Routes.webhookMessage(this.id, this.token, message), {
query: threadId ? makeURLSearchParams({ thread_id: threadId }) : undefined, query: threadId ? makeURLSearchParams({ thread_id: threadId }) : undefined,
auth: false, auth: false,
}); });
if (!this.client.channels) return data;
return ( return (
this.client.channels?.cache.get(data.channel_id)?.messages._add(data, cache) ?? this.client.channels.cache.get(data.channel_id)?.messages._add(data, false) ??
new (getMessage())(this.client, data) new (getMessage())(this.client, data)
); );
} }
@@ -305,7 +309,7 @@ class Webhook {
* Edits a message that was sent by this webhook. * Edits a message that was sent by this webhook.
* @param {MessageResolvable|'@original'} message The message to edit * @param {MessageResolvable|'@original'} message The message to edit
* @param {string|MessagePayload|WebhookEditMessageOptions} options The options to provide * @param {string|MessagePayload|WebhookEditMessageOptions} options The options to provide
* @returns {Promise<Message>} Returns the message edited by this webhook * @returns {Promise<APIMessage>} Returns the message edited by this webhook
*/ */
async editMessage(message, options) { async editMessage(message, options) {
if (!this.token) throw new Error('WEBHOOK_TOKEN_UNAVAILABLE'); if (!this.token) throw new Error('WEBHOOK_TOKEN_UNAVAILABLE');
@@ -329,7 +333,10 @@ class Webhook {
}, },
); );
const messageManager = this.client.channels?.cache.get(d.channel_id)?.messages; const channelManager = this.client.channels;
if (!channelManager) return d;
const messageManager = channelManager.cache.get(d.channel_id)?.messages;
if (!messageManager) return new (getMessage())(this.client, d); if (!messageManager) return new (getMessage())(this.client, d);
const existing = messageManager.cache.get(d.id); const existing = messageManager.cache.get(d.id);

View File

@@ -1564,6 +1564,11 @@ export class InteractionWebhook extends PartialWebhookMixin() {
public constructor(client: Client, id: Snowflake, token: string); public constructor(client: Client, id: Snowflake, token: string);
public token: string; public token: string;
public send(options: string | MessagePayload | InteractionReplyOptions): Promise<Message>; public send(options: string | MessagePayload | InteractionReplyOptions): Promise<Message>;
public editMessage(
message: MessageResolvable,
options: string | MessagePayload | WebhookEditMessageOptions,
): Promise<Message>;
public fetchMessage(message: string): Promise<Message>;
} }
export class Invite extends Base { export class Invite extends Base {
@@ -2801,9 +2806,9 @@ export class WebhookClient extends WebhookMixin(BaseClient) {
public editMessage( public editMessage(
message: MessageResolvable, message: MessageResolvable,
options: string | MessagePayload | WebhookEditMessageOptions, options: string | MessagePayload | WebhookEditMessageOptions,
): Promise<Message>; ): Promise<APIMessage>;
public fetchMessage(message: Snowflake, options?: WebhookFetchMessageOptions): Promise<Message>; public fetchMessage(message: Snowflake, options?: WebhookFetchMessageOptions): Promise<APIMessage>;
public send(options: string | MessagePayload | WebhookMessageOptions): Promise<Message>; public send(options: string | MessagePayload | WebhookMessageOptions): Promise<APIMessage>;
} }
export class WebSocketManager extends EventEmitter { export class WebSocketManager extends EventEmitter {
@@ -3488,9 +3493,9 @@ export interface PartialWebhookFields {
editMessage( editMessage(
message: MessageResolvable | '@original', message: MessageResolvable | '@original',
options: string | MessagePayload | WebhookEditMessageOptions, options: string | MessagePayload | WebhookEditMessageOptions,
): Promise<Message>; ): Promise<APIMessage | Message>;
fetchMessage(message: Snowflake | '@original', options?: WebhookFetchMessageOptions): Promise<Message>; fetchMessage(message: Snowflake | '@original', options?: WebhookFetchMessageOptions): Promise<APIMessage | Message>;
send(options: string | MessagePayload | Omit<WebhookMessageOptions, 'flags'>): Promise<Message>; send(options: string | MessagePayload | Omit<WebhookMessageOptions, 'flags'>): Promise<APIMessage | Message>;
} }
export interface WebhookFields extends PartialWebhookFields { export interface WebhookFields extends PartialWebhookFields {
@@ -5320,7 +5325,6 @@ export type WebhookEditMessageOptions = Pick<
>; >;
export interface WebhookFetchMessageOptions { export interface WebhookFetchMessageOptions {
cache?: boolean;
threadId?: Snowflake; threadId?: Snowflake;
} }