Skip to content

Commit

Permalink
fix(webhooks): revert webhook caching (and returning Message) (#8038)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyapat committed Jun 17, 2022
1 parent e5ec1c4 commit d54bf5d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
18 changes: 17 additions & 1 deletion packages/discord.js/src/structures/InteractionWebhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,26 @@ class InteractionWebhook {
/**
* Sends a message with this webhook.
* @param {string|MessagePayload|InteractionReplyOptions} options The content for the reply
* @returns {Promise<Message|APIMessage>}
* @returns {Promise<Message>}
*/

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() {}

/**
* 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() {}
deleteMessage() {}
get url() {}
Expand Down
21 changes: 14 additions & 7 deletions packages/discord.js/src/structures/Webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class Webhook {
/**
* Sends a message with this webhook.
* @param {string|MessagePayload|WebhookMessageOptions} options The options to provide
* @returns {Promise<Message>}
* @returns {Promise<APIMessage>}
* @example
* // Send a basic message
* webhook.send('hello!')
Expand Down Expand Up @@ -211,7 +211,9 @@ class Webhook {

const { body, files } = await messagePayload.resolveFiles();
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);
}

/**
Expand Down Expand Up @@ -286,17 +288,19 @@ class Webhook {
* Gets a message that was sent by this webhook.
* @param {Snowflake|'@original'} message The id of the message to fetch
* @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');

const data = await this.client.rest.get(Routes.webhookMessage(this.id, this.token, message), {
query: threadId ? makeURLSearchParams({ thread_id: threadId }) : undefined,
auth: false,
});

if (!this.client.channels) return data;
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)
);
}
Expand All @@ -305,7 +309,7 @@ class Webhook {
* 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
* @returns {Promise<APIMessage>} Returns the message edited by this webhook
*/
async editMessage(message, options) {
if (!this.token) throw new Error('WEBHOOK_TOKEN_UNAVAILABLE');
Expand All @@ -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);

const existing = messageManager.cache.get(d.id);
Expand Down
18 changes: 11 additions & 7 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1564,6 +1564,11 @@ export class InteractionWebhook extends PartialWebhookMixin() {
public constructor(client: Client, id: Snowflake, token: string);
public token: string;
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 {
Expand Down Expand Up @@ -2801,9 +2806,9 @@ export class WebhookClient extends WebhookMixin(BaseClient) {
public editMessage(
message: MessageResolvable,
options: string | MessagePayload | WebhookEditMessageOptions,
): Promise<Message>;
public fetchMessage(message: Snowflake, options?: WebhookFetchMessageOptions): Promise<Message>;
public send(options: string | MessagePayload | WebhookMessageOptions): Promise<Message>;
): Promise<APIMessage>;
public fetchMessage(message: Snowflake, options?: WebhookFetchMessageOptions): Promise<APIMessage>;
public send(options: string | MessagePayload | WebhookMessageOptions): Promise<APIMessage>;
}

export class WebSocketManager extends EventEmitter {
Expand Down Expand Up @@ -3488,9 +3493,9 @@ export interface PartialWebhookFields {
editMessage(
message: MessageResolvable | '@original',
options: string | MessagePayload | WebhookEditMessageOptions,
): Promise<Message>;
fetchMessage(message: Snowflake | '@original', options?: WebhookFetchMessageOptions): Promise<Message>;
send(options: string | MessagePayload | Omit<WebhookMessageOptions, 'flags'>): Promise<Message>;
): Promise<APIMessage | Message>;
fetchMessage(message: Snowflake | '@original', options?: WebhookFetchMessageOptions): Promise<APIMessage | Message>;
send(options: string | MessagePayload | Omit<WebhookMessageOptions, 'flags'>): Promise<APIMessage | Message>;
}

export interface WebhookFields extends PartialWebhookFields {
Expand Down Expand Up @@ -5320,7 +5325,6 @@ export type WebhookEditMessageOptions = Pick<
>;

export interface WebhookFetchMessageOptions {
cache?: boolean;
threadId?: Snowflake;
}

Expand Down

0 comments on commit d54bf5d

Please sign in to comment.