Skip to content

Commit

Permalink
feat(Webhook): backport missing properties (#3710)
Browse files Browse the repository at this point in the history
* feat(Webhook): add avatarURL getter

This backports: #3625

* feat(Webhook): add type, createAt, and createdTimestamp

This backports: #3585

* feat(Webhook): add url getter

This backports: #3178

* docs(Webhook): add missing type and readonly tags
  • Loading branch information
SpaceEEC authored Jan 24, 2020
1 parent 88b675d commit 30adb37
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
50 changes: 48 additions & 2 deletions src/structures/Webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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}
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/util/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 8 additions & 2 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand All @@ -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;
Expand Down Expand Up @@ -2230,6 +2234,8 @@ declare module 'discord.js' {
split?: boolean | SplitOptions;
};

type WebhookTypes = 'Incoming' | 'Channel Follower';

type WebSocketOptions = {
large_threshold?: number;
compress?: boolean;
Expand Down

0 comments on commit 30adb37

Please sign in to comment.