forked from joe27g/EnhancedDiscord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.js
121 lines (107 loc) · 3.21 KB
/
plugin.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* Plugin Class
*/
class Plugin {
/**
* Create your plugin, must have a name and load() function
* @constructor
* @param {object} options - Plugin options
*/
constructor (opts = {}) {
if (!opts.name || typeof opts.load !== 'function')
return 'Invalid plugin. Needs a name and a load() function.';
Object.assign(this, opts);
if (!this.color)
this.color = 'orange';
if (!this.author)
this.author = '<unknown>';
}
/**
* Load this plugin.
*/
load () {}
/**
* Unload this plugin.
*/
unload () {}
/**
* Reload this plugin.
*/
reload () {
this.log('Reloading...');
this.unload();
delete require.cache[require.resolve(`./plugins/${this.id}`)];
const newPlugin = require(`./plugins/${this.id}`);
ED.plugins[this.id] = newPlugin;
newPlugin.id = this.id;
return newPlugin.load();
}
/**
* Send a decorated console.log prefixed with ED and your plugin name
* @param {...string} msg - Message to be logged
*/
log (...msg) {
console.log(`%c[EnhancedDiscord] %c[${this.name}]`, 'color: red;', `color: ${this.color}`, ...msg);
}
/**
* Send a decorated console.info prefixed with ED and your plugin name
* @param {...string} msg - Message to be logged
*/
info (...msg) {
console.info(`%c[EnhancedDiscord] %c[${this.name}]`, 'color: red;', `color: ${this.color}`, ...msg);
}
/**
* Send a decorated console.warn prefixed with ED and your plugin name
* @param {...string} msg - Message to be logged
*/
warn (...msg) {
console.warn(`%c[EnhancedDiscord] %c[${this.name}]`, 'color: red;', `color: ${this.color}`, ...msg);
}
/**
* Send a decorated console.error prefixed with ED and your plugin name
* @param {...string} msg - Message to be logged
*/
error (...msg) {
console.error(`%c[EnhancedDiscord] %c[${this.name}]`, 'color: red;', `color: ${this.color}`, ...msg);
}
/**
* Returns a Promise that resolves after ms milliseconds.
* @param {number} ms - How long to wait before resolving the promise
*/
sleep (ms) {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}
/**
* Get plugin settings.
* @returns {Object} Plugin settings object
*/
get settings() {
return EDApi.loadPluginSettings(this.id);
}
/**
* Get particular plugin setting.
* @param {string} key
* @returns Plugin setting value
*/
getSetting(key) {
return EDApi.loadData(this.id, key);
}
/**
* Save plugin settings.
* @param {Object} newSets - New plugin settings object
*/
set settings(newSets = {}) {
return EDApi.savePluginSettings(this.id, newSets);
}
/**
* Set particular plugin setting.
* @param {string} key
* @param data
*/
setSetting(key, data) {
return EDApi.saveData(this.id, key, data);
}
}
module.exports = Plugin;