Skip to content

Commit

Permalink
feat(Webhook): add type property and created* getters (#3585)
Browse files Browse the repository at this point in the history
* feat(Webhook): add created* getters

* feat(Webhook): add type property

* typings(WebhookFields): use primitive string for url getter

Co-Authored-By: Gryffon Bellish <[email protected]>

* fix(Webhook): token can be null

Co-authored-by: Gryffon Bellish <[email protected]>
  • Loading branch information
SpaceEEC and PyroTechniac authored Dec 27, 2019
1 parent fc27ce1 commit ea76a56
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
32 changes: 30 additions & 2 deletions src/structures/Webhook.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

const { WebhookTypes } = require('../util/Constants');
const DataResolver = require('../util/DataResolver');
const Snowflake = require('../util/Snowflake');
const Channel = require('./Channel');
const APIMessage = require('./APIMessage');

Expand Down Expand Up @@ -29,9 +31,9 @@ class Webhook {
/**
* 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 @@ -45,6 +47,12 @@ class Webhook {
*/
this.id = data.id;

/**
* The type of the webhook
* @type {WebhookTypes}
*/
this.type = WebhookTypes[data.type];

/**
* The guild the webhook belongs to
* @type {Snowflake}
Expand Down Expand Up @@ -210,6 +218,23 @@ class Webhook {
delete(reason) {
return this.client.api.webhooks(this.id, this.token).delete({ reason });
}
/**
* 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);
}

/**
* The url of this webhook
Expand All @@ -236,6 +261,9 @@ class Webhook {
'sendSlackMessage',
'edit',
'delete',
'createdTimestamp',
'createdAt',
'url',
]) {
Object.defineProperty(structure.prototype, prop,
Object.getOwnPropertyDescriptor(Webhook.prototype, prop));
Expand Down
13 changes: 13 additions & 0 deletions src/util/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,19 @@ exports.MembershipStates = [
'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',
];

function keyMirror(arr) {
let tmp = Object.create(null);
for (const value of arr) tmp[value] = value;
Expand Down
10 changes: 8 additions & 2 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1636,11 +1636,13 @@ declare module 'discord.js' {
public guildID: Snowflake;
public name: string;
public owner: User | object | null;
public readonly url: string;
public token: string | null;
public type: WebhookTypes;
}

export class WebhookClient extends WebhookMixin(BaseClient) {
constructor(id: string, token: string, options?: ClientOptions);
public token: string;
}

export class WebSocketManager extends EventEmitter {
Expand Down Expand Up @@ -1909,7 +1911,9 @@ declare module 'discord.js' {
interface WebhookFields {
readonly client: Client;
id: Snowflake;
token: string;
readonly createdAt: Date;
readonly createdTimestamp: number;
readonly url: string;
delete(reason?: string): Promise<void>;
edit(options: WebhookEditData): Promise<Webhook>;
send(content?: StringResolvable, options?: WebhookMessageOptions & { split?: false } | MessageAdditions): Promise<Message>;
Expand Down Expand Up @@ -2654,6 +2658,8 @@ declare module 'discord.js' {
split?: boolean | SplitOptions;
}

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

interface WebSocketOptions {
large_threshold?: number;
compress?: boolean;
Expand Down

0 comments on commit ea76a56

Please sign in to comment.