WebhookClient.js
1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
const BaseClient = require('./BaseClient');
const { Error } = require('../errors');
const Webhook = require('../structures/Webhook');
/**
* The webhook client.
* @implements {Webhook}
* @extends {BaseClient}
*/
class WebhookClient extends BaseClient {
/**
* The data for the webhook client containing either an id and token or just a URL
* @typedef {Object} WebhookClientData
* @property {Snowflake} [id] The id of the webhook
* @property {string} [token] The token of the webhook
* @property {string} [url] The full URL for the webhook client
*/
/**
* @param {WebhookClientData} data The data of the webhook
* @param {ClientOptions} [options] Options for the client
*/
constructor(data, options) {
super(options);
Object.defineProperty(this, 'client', { value: this });
let { id, token } = data;
if ('url' in data) {
const url = data.url.match(
// eslint-disable-next-line no-useless-escape
/^https?:\/\/(?:canary|ptb)?\.?discord\.com\/api\/webhooks(?:\/v[0-9]\d*)?\/([^\/]+)\/([^\/]+)/i,
);
if (!url || url.length <= 1) throw new Error('WEBHOOK_URL_INVALID');
[, id, token] = url;
}
this.id = id;
Object.defineProperty(this, 'token', { value: token, writable: true, configurable: true });
}
// These are here only for documentation purposes - they are implemented by Webhook
/* eslint-disable no-empty-function */
send() {}
sendSlackMessage() {}
fetchMessage() {}
edit() {}
editMessage() {}
delete() {}
deleteMessage() {}
get createdTimestamp() {}
get createdAt() {}
get url() {}
}
Webhook.applyToClass(WebhookClient);
module.exports = WebhookClient;