Skip to content

Commit

Permalink
Merge 1981cdf into f95f5dc
Browse files Browse the repository at this point in the history
  • Loading branch information
izexi committed Jan 23, 2021
2 parents f95f5dc + 1981cdf commit 45adb3b
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 13 deletions.
62 changes: 60 additions & 2 deletions src/structures/Webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ class Webhook {
* @property {string} [username=this.name] Username override for the message
* @property {string} [avatarURL] Avatar URL override for the message
* @property {boolean} [tts=false] Whether or not the message should be spoken aloud
* @property {StringResolvable} [content] The content for the message
* @property {string} [nonce=''] The nonce for the message
* @property {Object[]} [embeds] An array of embeds for the message
* @property {MessageEmbed[]|Object[]} [embeds] An array of embeds for the message
* @property {MessageMentionOptions} [allowedMentions] Which mentions should be parsed from the message content
* (see [here](https://discord.com/developers/docs/resources/channel#embed-object) for more details)
* @property {FileOptions[]|string[]} [files] Files to send with the message
Expand All @@ -92,6 +93,14 @@ class Webhook {
* it exceeds the character limit. If an object is provided, these are the options for splitting the message.
*/

/**
* Options that can be passed into editMessage.
* @typedef {Object} WebhookEditMessageOptions
* @property {MessageEmbed[]|Object[]} [embeds] See {@link WebhookMessageOptions#embeds}
* @property {StringResolvable} [content] See {@link WebhookMessageOptions#content}
* @property {MessageMentionOptions} [allowedMentions] See {@link WebhookMessageOptions#allowedMentions}
*/

/**
* Sends a message with this webhook.
* @param {StringResolvable|APIMessage} [content=''] The content to send
Expand Down Expand Up @@ -216,6 +225,32 @@ class Webhook {
return this;
}

/**
* Edits a message that was sent by this webhook.
* @param {MessageResolvable} message The message to edit
* @param {StringResolvable} [content] The new content for the message
* @param {WebhookEditMessageOptions} [options] The options to provide
* @returns {Promise<Message|Object>} Returns the raw message data if the webhook was instantiated as a
* {@link WebhookClient} or if the channel is uncached, otherwise a {@link Message} will be returned
*/
async editMessage(message, content, options) {
const { data } = APIMessage.create(this, content, options).resolveData();
const d = await this.client.api
.webhooks(this.id, this.token)
.messages(typeof message === 'string' ? message : message.id)
.patch({ data });

const messageManager = this.client.channels?.cache.get(d.channel_id)?.messages;
if (!messageManager) return d;

const existing = messageManager.cache.get(d.id);
if (!existing) return messageManager.add(d);

const clone = existing._clone();
clone._patch(d);
return clone;
}

/**
* Deletes the webhook.
* @param {string} [reason] Reason for deleting this webhook
Expand All @@ -224,6 +259,19 @@ class Webhook {
delete(reason) {
return this.client.api.webhooks(this.id, this.token).delete({ reason });
}

/**
* Delete a message that was sent by this webhook.
* @param {MessageResolvable} message The message to delete
* @returns {Promise<void>}
*/
async deleteMessage(message) {
await this.client.api
.webhooks(this.id, this.token)
.messages(typeof message === 'string' ? message : message.id)
.delete();
}

/**
* The timestamp the webhook was created at
* @type {number}
Expand Down Expand Up @@ -262,7 +310,17 @@ class Webhook {
}

static applyToClass(structure) {
for (const prop of ['send', 'sendSlackMessage', 'edit', 'delete', 'createdTimestamp', 'createdAt', 'url']) {
for (const prop of [
'send',
'sendSlackMessage',
'edit',
'editMessage',
'delete',
'deleteMessage',
'createdTimestamp',
'createdAt',
'url',
]) {
Object.defineProperty(structure.prototype, prop, Object.getOwnPropertyDescriptor(Webhook.prototype, prop));
}
}
Expand Down
62 changes: 51 additions & 11 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1763,6 +1763,34 @@ declare module 'discord.js' {
constructor(id: string, token: string, options?: ClientOptions);
public client: this;
public token: string;
public editMessage(
message: MessageResolvable,
content: APIMessageContentResolvable,
options?: WebhookEditMessageOptions,
): Promise<WebhookRawMessageResponse>;
public editMessage(
message: MessageResolvable,
options: WebhookEditMessageOptions,
): Promise<WebhookRawMessageResponse>;
public send(
content: APIMessageContentResolvable | (WebhookMessageOptions & { split?: false }) | MessageAdditions,
): Promise<WebhookRawMessageResponse>;
public send(options: WebhookMessageOptions & { split: true | SplitOptions }): Promise<WebhookRawMessageResponse[]>;
public send(
options: WebhookMessageOptions | APIMessage,
): Promise<WebhookRawMessageResponse | WebhookRawMessageResponse[]>;
public send(
content: StringResolvable,
options: (WebhookMessageOptions & { split?: false }) | MessageAdditions,
): Promise<WebhookRawMessageResponse>;
public send(
content: StringResolvable,
options: WebhookMessageOptions & { split: true | SplitOptions },
): Promise<WebhookRawMessageResponse[]>;
public send(
content: StringResolvable,
options: WebhookMessageOptions,
): Promise<WebhookRawMessageResponse | WebhookRawMessageResponse[]>;
}

export class WebSocketManager extends EventEmitter {
Expand Down Expand Up @@ -2091,7 +2119,17 @@ declare module 'discord.js' {
readonly createdTimestamp: number;
readonly url: string;
delete(reason?: string): Promise<void>;
deleteMessage(message: MessageResolvable): Promise<void>;
edit(options: WebhookEditData): Promise<Webhook>;
editMessage(
message: MessageResolvable,
content: APIMessageContentResolvable,
options?: WebhookEditMessageOptions,
): Promise<Message | WebhookRawMessageResponse>;
editMessage(
message: MessageResolvable,
options: WebhookEditMessageOptions,
): Promise<Message | WebhookRawMessageResponse>;
send(
content: APIMessageContentResolvable | (WebhookMessageOptions & { split?: false }) | MessageAdditions,
): Promise<Message | WebhookRawMessageResponse>;
Expand Down Expand Up @@ -3294,17 +3332,19 @@ declare module 'discord.js' {
reason?: string;
}

interface WebhookMessageOptions {
username?: string;
avatarURL?: string;
tts?: boolean;
nonce?: string;
embeds?: (MessageEmbed | object)[];
allowedMentions?: MessageMentionOptions;
files?: (FileOptions | BufferResolvable | Stream | MessageAttachment)[];
code?: string | boolean;
split?: boolean | SplitOptions;
}
type WebhookEditMessageOptions = Pick<WebhookMessageOptions, 'content' | 'embeds' | 'allowedMentions'>;

type WebhookMessageOptions = Omit<MessageOptions, 'embed'> & { embeds?: (MessageEmbed | object)[] };

type WebhookRawMessageResponse = Omit<APIRawMessage, 'author'> & {
author: {
bot: true;
id: Snowflake;
username: string;
avatar: string | null;
discriminator: '0000';
};
};

type WebhookRawMessageResponse = Omit<APIRawMessage, 'author'> & {
author: {
Expand Down

0 comments on commit 45adb3b

Please sign in to comment.