mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 12:03:31 +01:00
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:
@@ -801,13 +801,23 @@ class RESTMethods {
|
|||||||
.then(data => new Webhook(this.client, data));
|
.then(data => new Webhook(this.client, data));
|
||||||
}
|
}
|
||||||
|
|
||||||
editWebhook(webhook, name, avatar) {
|
editWebhook(webhook, options, reason) {
|
||||||
return this.rest.makeRequest('patch', Endpoints.Webhook(webhook.id, webhook.token), false, {
|
let endpoint;
|
||||||
name,
|
let auth;
|
||||||
avatar,
|
|
||||||
}).then(data => {
|
// 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.name = data.name;
|
||||||
webhook.avatar = data.avatar;
|
webhook.avatar = data.avatar;
|
||||||
|
webhook.channelID = data.channel_id;
|
||||||
return webhook;
|
return webhook;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -276,19 +276,46 @@ class Webhook extends EventEmitter {
|
|||||||
return this.client.rest.methods.sendSlackWebhookMessage(this, body);
|
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.
|
* Edit the webhook.
|
||||||
* @param {string} name The new name for the webhook
|
* @param {string|WebhookEditOptions} nameOrOptions The new name for the webhook **(deprecated, use options)**
|
||||||
* @param {BufferResolvable} [avatar] The new avatar for the webhook
|
* 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>}
|
* @returns {Promise<Webhook>}
|
||||||
*/
|
*/
|
||||||
edit(name = this.name, avatar) {
|
edit(nameOrOptions = this.name, avatarOrReason) {
|
||||||
if (avatar) {
|
if (typeof nameOrOptions !== 'object') {
|
||||||
return this.client.resolver.resolveImage(avatar).then(data =>
|
process.emitWarning('Webhook#edit: Use options object instead of separate parameters.');
|
||||||
this.client.rest.methods.editWebhook(this, name, data)
|
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
9
typings/index.d.ts
vendored
@@ -1579,7 +1579,8 @@ declare module 'discord.js' {
|
|||||||
public owner: User | object;
|
public owner: User | object;
|
||||||
public token: string;
|
public token: string;
|
||||||
public delete(reason?: string): Promise<void>;
|
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(content?: StringResolvable, options?: WebhookMessageOptions | RichEmbed | Attachment): Promise<Message | Message[]>;
|
||||||
public send(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[]>;
|
public sendCode(lang: string, content: StringResolvable, options?: WebhookMessageOptions): Promise<Message | Message[]>;
|
||||||
@@ -2201,6 +2202,12 @@ declare module 'discord.js' {
|
|||||||
|
|
||||||
type VoiceStatus = number;
|
type VoiceStatus = number;
|
||||||
|
|
||||||
|
type WebhookEditOptions = {
|
||||||
|
name?: string;
|
||||||
|
avatar?: BufferResolvable;
|
||||||
|
channel?: ChannelResolvable;
|
||||||
|
};
|
||||||
|
|
||||||
type WebhookMessageOptions = {
|
type WebhookMessageOptions = {
|
||||||
username?: string;
|
username?: string;
|
||||||
avatarURL?: string;
|
avatarURL?: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user