mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
feat(Webhook): backport missing properties (#3710)
* feat(Webhook): add avatarURL getter This backports: https://github.com/discordjs/discord.js/pull/3625 * feat(Webhook): add type, createAt, and createdTimestamp This backports: https://github.com/discordjs/discord.js/pull/3585 * feat(Webhook): add url getter This backports: https://github.com/discordjs/discord.js/pull/3178 * docs(Webhook): add missing type and readonly tags
This commit is contained in:
@@ -3,6 +3,8 @@ const path = require('path');
|
||||
const Util = require('../util/Util');
|
||||
const Attachment = require('./Attachment');
|
||||
const RichEmbed = require('./RichEmbed');
|
||||
const Constants = require('../util/Constants');
|
||||
const Snowflake = require('../util/Snowflake');
|
||||
|
||||
/**
|
||||
* Represents a webhook.
|
||||
@@ -36,9 +38,9 @@ class Webhook extends EventEmitter {
|
||||
/**
|
||||
* The token for the webhook
|
||||
* @name Webhook#token
|
||||
* @type {string}
|
||||
* @type {?string}
|
||||
*/
|
||||
Object.defineProperty(this, 'token', { value: data.token, writable: true, configurable: true });
|
||||
Object.defineProperty(this, 'token', { value: data.token || null, writable: true, configurable: true });
|
||||
|
||||
/**
|
||||
* The avatar for the webhook
|
||||
@@ -52,6 +54,12 @@ class Webhook extends EventEmitter {
|
||||
*/
|
||||
this.id = data.id;
|
||||
|
||||
/**
|
||||
* The type of the webhook
|
||||
* @type {WebhookTypes}
|
||||
*/
|
||||
this.type = Constants.WebhookTypes[data.type];
|
||||
|
||||
/**
|
||||
* The guild the webhook belongs to
|
||||
* @type {Snowflake}
|
||||
@@ -75,6 +83,44 @@ class Webhook extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The timestamp the webhook was created at
|
||||
* @type {number}
|
||||
* @readonly
|
||||
*/
|
||||
get createdTimestamp() {
|
||||
return Snowflake.deconstruct(this.id).timestamp;
|
||||
}
|
||||
|
||||
/**
|
||||
* The time the webhook was created at
|
||||
* @type {Date}
|
||||
* @readonly
|
||||
*/
|
||||
get createdAt() {
|
||||
return new Date(this.createdTimestamp);
|
||||
}
|
||||
|
||||
/**
|
||||
* A link to the webhook user's avatar
|
||||
* @type {?stirng}
|
||||
* @readonly
|
||||
*/
|
||||
get avatarURL() {
|
||||
if (!this.avatar) return null;
|
||||
return Constants.Endpoints.CDN(this.client.options.http.cdn).Avatar(this.id, this.avatar);
|
||||
}
|
||||
|
||||
/**
|
||||
* The url of this webhook
|
||||
* @type {string}
|
||||
* @readonly
|
||||
*/
|
||||
get url() {
|
||||
const API = `${this.client.options.http.host}/api/v${this.client.options.http.version}`;
|
||||
return API + Constants.Endpoints.Webhook(this.id, this.token);
|
||||
}
|
||||
|
||||
/**
|
||||
* Options that can be passed into send, sendMessage, sendFile, sendEmbed, and sendCode.
|
||||
* @typedef {Object} WebhookMessageOptions
|
||||
|
||||
@@ -902,3 +902,16 @@ exports.MembershipStates = [
|
||||
'INVITED',
|
||||
'ACCEPTED',
|
||||
];
|
||||
|
||||
/**
|
||||
* The value set for a webhook's type:
|
||||
* * Incoming
|
||||
* * Channel Follower
|
||||
* @typedef {string} WebhookTypes
|
||||
*/
|
||||
exports.WebhookTypes = [
|
||||
// They start at 1
|
||||
null,
|
||||
'Incoming',
|
||||
'Channel Follower',
|
||||
];
|
||||
|
||||
10
typings/index.d.ts
vendored
10
typings/index.d.ts
vendored
@@ -1578,14 +1578,17 @@ declare module 'discord.js' {
|
||||
|
||||
export class Webhook {
|
||||
constructor(client: Client, dataOrID: object | string, token: string);
|
||||
public avatar: string;
|
||||
public avatar: string | null;
|
||||
public readonly avatarURL: string | null;
|
||||
public channelID: string;
|
||||
public readonly client: Client;
|
||||
public guildID: string;
|
||||
public id: Snowflake;
|
||||
public name: string;
|
||||
public owner: User | object;
|
||||
public token: string;
|
||||
public token: string | null;
|
||||
public type: WebhookTypes;
|
||||
public readonly url: string;
|
||||
public delete(reason?: string): Promise<void>;
|
||||
public edit(name?: string, avatar?: BufferResolvable): Promise<Webhook>;
|
||||
public edit(options?: WebhookEditOptions, reason?: string): Promise<Webhook>;
|
||||
@@ -1604,6 +1607,7 @@ declare module 'discord.js' {
|
||||
private _timeouts: Set<NodeJS.Timer>;
|
||||
private resolver: ClientDataResolver;
|
||||
private rest: object;
|
||||
public token: string;
|
||||
|
||||
public options: ClientOptions;
|
||||
public clearInterval(interval: NodeJS.Timer): void;
|
||||
@@ -2230,6 +2234,8 @@ declare module 'discord.js' {
|
||||
split?: boolean | SplitOptions;
|
||||
};
|
||||
|
||||
type WebhookTypes = 'Incoming' | 'Channel Follower';
|
||||
|
||||
type WebSocketOptions = {
|
||||
large_threshold?: number;
|
||||
compress?: boolean;
|
||||
|
||||
Reference in New Issue
Block a user