/
index.js
76 lines (71 loc) · 2.12 KB
/
index.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const bind = require("./Bind");
const { EventEmitter } = require("events");
/**
* The DBLPoster
* @class DBLPoster
* @extends {EventEmitter}
*/
class DBLPoster extends EventEmitter {
/**
* Creates an instance of DBLPoster.
* @param {string} apiKey The API Key provded by discordbots.org
* @param {DiscordJS.Client|Eris|DiscordIO.Client|DiscordIE.Client} client The client you want to bind to
* @memberof DBLPoster
*/
constructor (apiKey, client) {
super();
Object.defineProperty(this, "_apiKey", {
value: apiKey,
enumerable: false,
});
this.client = client;
}
/**
* Binds the poster to the client.
* @param {string} [paramName="dblPoster"] The value that determines the accessible
* poster instance on the client
* @param {DiscordJS.Client|Eris|DiscordIO.Client|DiscordIE.Client} [client=this.client] Optional client to bind to,
* if you haven't added it in the constructor
* @memberof DBLPoster
* @chainable
*/
bind (paramName = "dblPoster", client = this.client) {
if (this._apiKey === "" || this._apiKey === null || !this._apiKey || typeof this._apiKey !== "string") {
throw new Error(`The API key is either not specified, or is not a string.`);
}
if (!client) throw new RangeError(`You need to provide a client to bind to, either in the constructor of dblposter or in the bind function!`);
this.client = client;
this.paramName = paramName;
this.client[this.paramName] = this;
bind(this.client, paramName);
return this;
}
/**
* Destroys the DBLPoster's interval.
* @returns {DBLPoster}
* @memberof DBLPoster
* @chainable
*/
destroy () {
if (this.client && this.client[this.paramName]) {
if (this.client[this.paramName]._interval) {
clearInterval(this.client[this.paramName]._interval);
this.client[this.paramName]._interval = null;
}
this.client[this.paramName] = null;
// Null out the param name
this.paramName = null;
}
return this;
}
/**
* Runs a manual post of the current bot stats.
* @memberof DBLPoster
* @returns {void}
*/
post () {
const paramName = this.paramName;
this.destroy().bind(paramName);
}
}
module.exports = DBLPoster;