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:
SpaceEEC
2020-01-24 16:52:52 +01:00
committed by GitHub
parent 88b675d38a
commit 30adb378fc
3 changed files with 69 additions and 4 deletions

View File

@@ -3,6 +3,8 @@ const path = require('path');
const Util = require('../util/Util'); const Util = require('../util/Util');
const Attachment = require('./Attachment'); const Attachment = require('./Attachment');
const RichEmbed = require('./RichEmbed'); const RichEmbed = require('./RichEmbed');
const Constants = require('../util/Constants');
const Snowflake = require('../util/Snowflake');
/** /**
* Represents a webhook. * Represents a webhook.
@@ -36,9 +38,9 @@ class Webhook extends EventEmitter {
/** /**
* The token for the webhook * The token for the webhook
* @name Webhook#token * @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 * The avatar for the webhook
@@ -52,6 +54,12 @@ class Webhook extends EventEmitter {
*/ */
this.id = data.id; this.id = data.id;
/**
* The type of the webhook
* @type {WebhookTypes}
*/
this.type = Constants.WebhookTypes[data.type];
/** /**
* The guild the webhook belongs to * The guild the webhook belongs to
* @type {Snowflake} * @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. * Options that can be passed into send, sendMessage, sendFile, sendEmbed, and sendCode.
* @typedef {Object} WebhookMessageOptions * @typedef {Object} WebhookMessageOptions

View File

@@ -902,3 +902,16 @@ exports.MembershipStates = [
'INVITED', 'INVITED',
'ACCEPTED', '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
View File

@@ -1578,14 +1578,17 @@ declare module 'discord.js' {
export class Webhook { export class Webhook {
constructor(client: Client, dataOrID: object | string, token: string); constructor(client: Client, dataOrID: object | string, token: string);
public avatar: string; public avatar: string | null;
public readonly avatarURL: string | null;
public channelID: string; public channelID: string;
public readonly client: Client; public readonly client: Client;
public guildID: string; public guildID: string;
public id: Snowflake; public id: Snowflake;
public name: string; public name: string;
public owner: User | object; 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 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 edit(options?: WebhookEditOptions, reason?: string): Promise<Webhook>;
@@ -1604,6 +1607,7 @@ declare module 'discord.js' {
private _timeouts: Set<NodeJS.Timer>; private _timeouts: Set<NodeJS.Timer>;
private resolver: ClientDataResolver; private resolver: ClientDataResolver;
private rest: object; private rest: object;
public token: string;
public options: ClientOptions; public options: ClientOptions;
public clearInterval(interval: NodeJS.Timer): void; public clearInterval(interval: NodeJS.Timer): void;
@@ -2230,6 +2234,8 @@ declare module 'discord.js' {
split?: boolean | SplitOptions; split?: boolean | SplitOptions;
}; };
type WebhookTypes = 'Incoming' | 'Channel Follower';
type WebSocketOptions = { type WebSocketOptions = {
large_threshold?: number; large_threshold?: number;
compress?: boolean; compress?: boolean;