feat(Webhook): add ability to change channel and specify reason to edit (#3587)

* feat(Webhook): add ability to change channel and specify reason to edit

* fix(RESTMethods): update channelID of the webhook too
This commit is contained in:
SpaceEEC
2020-01-05 18:34:00 +01:00
committed by GitHub
parent d1d0d75d4a
commit fbe9bc499b
3 changed files with 58 additions and 14 deletions

View File

@@ -801,13 +801,23 @@ class RESTMethods {
.then(data => new Webhook(this.client, data));
}
editWebhook(webhook, name, avatar) {
return this.rest.makeRequest('patch', Endpoints.Webhook(webhook.id, webhook.token), false, {
name,
avatar,
}).then(data => {
editWebhook(webhook, options, reason) {
let endpoint;
let auth;
// Changing the channel of a webhook or specifying a reason requires a bot token
if (options.channel_id || reason) {
endpoint = Endpoints.Webhook(webhook.id);
auth = true;
} else {
endpoint = Endpoints.Webhook(webhook.id, webhook.token);
auth = false;
}
return this.rest.makeRequest('patch', endpoint, auth, options, undefined, reason).then(data => {
webhook.name = data.name;
webhook.avatar = data.avatar;
webhook.channelID = data.channel_id;
return webhook;
});
}

View File

@@ -276,19 +276,46 @@ class Webhook extends EventEmitter {
return this.client.rest.methods.sendSlackWebhookMessage(this, body);
}
/**
* Options provided to edit a webhook.
* @property {string} [name] The new name for the webhook
* @property {BufferResolvable} [avatar] The new avatar for the webhook
* @property {ChannelResolvable} [channel] The new channel for the webhook
* @typedef {Object} WebhookEditOptions
*/
/**
* Edit the webhook.
* @param {string} name The new name for the webhook
* @param {BufferResolvable} [avatar] The new avatar for the webhook
* @param {string|WebhookEditOptions} nameOrOptions The new name for the webhook **(deprecated, use options)**
* Alternatively options for the webhook, overriding the avatar parameter.
* @param {BufferResolvable|string} [avatarOrReason] The new avatar for the webhook **(deprecated, use options)**
* Alternatively a reason to edit, if using options as first parameter.
* @returns {Promise<Webhook>}
*/
edit(name = this.name, avatar) {
if (avatar) {
return this.client.resolver.resolveImage(avatar).then(data =>
this.client.rest.methods.editWebhook(this, name, data)
);
edit(nameOrOptions = this.name, avatarOrReason) {
if (typeof nameOrOptions !== 'object') {
process.emitWarning('Webhook#edit: Use options object instead of separate parameters.');
nameOrOptions = {
name: nameOrOptions,
avatar: avatarOrReason,
};
// Parameter was an avatar here; Clear the now reason parameter
avatarOrReason = undefined;
}
return this.client.rest.methods.editWebhook(this, name);
if (nameOrOptions.channel) {
nameOrOptions.channel_id = this.client.resolver.resolveChannelID(nameOrOptions.channel);
nameOrOptions.channel = undefined;
}
if (nameOrOptions.avatar) {
return this.client.resolver.resolveImage(nameOrOptions.avatar).then(data => {
nameOrOptions.avatar = data;
return this.client.rest.methods.editWebhook(this, nameOrOptions, avatarOrReason);
});
}
return this.client.rest.methods.editWebhook(this, nameOrOptions, avatarOrReason);
}
/**

9
typings/index.d.ts vendored
View File

@@ -1579,7 +1579,8 @@ declare module 'discord.js' {
public owner: User | object;
public token: string;
public delete(reason?: string): Promise<void>;
public edit(name: string, avatar: BufferResolvable): Promise<Webhook>;
public edit(name?: string, avatar?: BufferResolvable): Promise<Webhook>;
public edit(options?: WebhookEditOptions, reason?: string): Promise<Webhook>;
public send(content?: StringResolvable, options?: WebhookMessageOptions | RichEmbed | Attachment): Promise<Message | Message[]>;
public send(options?: WebhookMessageOptions | RichEmbed | Attachment): Promise<Message | Message[]>;
public sendCode(lang: string, content: StringResolvable, options?: WebhookMessageOptions): Promise<Message | Message[]>;
@@ -2201,6 +2202,12 @@ declare module 'discord.js' {
type VoiceStatus = number;
type WebhookEditOptions = {
name?: string;
avatar?: BufferResolvable;
channel?: ChannelResolvable;
};
type WebhookMessageOptions = {
username?: string;
avatarURL?: string;