BetterDiscordApp-v2/client/src/modules/plugin.js

140 lines
4.5 KiB
JavaScript
Raw Normal View History

2018-01-30 16:59:27 +01:00
/**
* BetterDiscord Plugin Base
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { Utils, FileUtils } from 'common';
2018-02-13 18:07:51 +01:00
import { Modals } from 'ui';
import { EventEmitter } from 'events';
2018-02-22 17:19:35 +01:00
import PluginManager from './pluginmanager';
import { SettingUpdatedEvent, SettingsUpdatedEvent } from 'structs';
class PluginEvents {
constructor(plugin) {
this.plugin = plugin;
this.emitter = new EventEmitter();
}
on(eventname, callback) {
this.emitter.on(eventname, callback);
}
off(eventname, callback) {
this.emitter.removeListener(eventname, callback);
}
emit(...args) {
this.emitter.emit(...args);
}
}
2018-02-13 23:14:50 +01:00
export default class Plugin {
2018-01-30 16:59:27 +01:00
constructor(pluginInternals) {
this.__pluginInternals = pluginInternals;
2018-02-02 18:52:38 +01:00
this.saveSettings = this.saveSettings.bind(this);
2018-02-14 16:45:10 +01:00
this.hasSettings = this.config && this.config.length > 0;
2018-01-30 16:59:27 +01:00
this.start = this.start.bind(this);
this.stop = this.stop.bind(this);
2018-03-01 20:00:24 +01:00
this.settings.on('setting-updated', event => this.events.emit('setting-updated', event));
this.settings.on('settings-updated', event => this.events.emit('settings-updated', event));
this.settings.on('settings-updated', event => this.saveConfiguration());
2018-01-30 16:59:27 +01:00
}
2018-02-13 17:06:36 +01:00
get type() { return 'plugin' }
2018-01-30 16:59:27 +01:00
get configs() { return this.__pluginInternals.configs }
get info() { return this.__pluginInternals.info }
2018-02-04 22:19:05 +01:00
get icon() { return this.info.icon }
2018-01-30 16:59:27 +01:00
get paths() { return this.__pluginInternals.paths }
get main() { return this.__pluginInternals.main }
get defaultConfig() { return this.configs.defaultConfig }
get userConfig() { return this.configs.userConfig }
2018-02-14 18:55:10 +01:00
get configSchemes() { return this.configs.schemes }
2018-02-21 18:46:27 +01:00
get id() { return this.info.id || this.name.toLowerCase().replace(/[^a-zA-Z0-9-]/g, '-').replace(/--/g, '-') }
2018-01-30 16:59:27 +01:00
get name() { return this.info.name }
2018-03-01 02:52:08 +01:00
get description() { return this.info.description }
2018-01-30 16:59:27 +01:00
get authors() { return this.info.authors }
get version() { return this.info.version }
2018-02-21 16:58:45 +01:00
get contentPath() { return this.paths.contentPath }
2018-01-30 23:21:06 +01:00
get pluginPath() { return this.paths.contentPath }
2018-01-30 16:59:27 +01:00
get dirName() { return this.paths.dirName }
get enabled() { return this.userConfig.enabled }
2018-03-01 20:00:24 +01:00
get settings() { return this.userConfig.config }
get config() { return this.settings.settings }
2018-02-14 02:45:41 +01:00
get pluginConfig() { return this.config }
get data() { return this.userConfig.data }
2018-02-12 23:49:44 +01:00
get exports() { return this._exports ? this._exports : (this._exports = this.getExports()) }
get events() { return this.EventEmitter ? this.EventEmitter : (this.EventEmitter = new PluginEvents(this)) }
2018-01-30 16:59:27 +01:00
2018-02-12 16:20:27 +01:00
getSetting(setting_id, category_id) {
2018-02-14 16:45:10 +01:00
for (let category of this.config) {
if (category_id && category.category !== category_id) continue;
2018-02-12 16:20:27 +01:00
for (let setting of category.settings) {
if (setting.id !== setting_id) continue;
2018-02-12 16:20:27 +01:00
return setting.value;
}
}
2018-02-02 18:52:38 +01:00
}
2018-02-13 18:07:51 +01:00
showSettingsModal() {
return Modals.contentSettings(this);
2018-02-13 18:07:51 +01:00
}
2018-02-04 20:06:08 +01:00
async saveSettings(newSettings) {
2018-03-01 20:00:24 +01:00
const updatedSettings = this.settings.merge(newSettings);
2018-02-04 20:06:08 +01:00
2018-03-01 20:00:24 +01:00
await this.saveConfiguration();
return updatedSettings;
2018-02-02 18:52:38 +01:00
}
async saveConfiguration() {
try {
await FileUtils.writeFile(`${this.pluginPath}/user.config.json`, JSON.stringify({
enabled: this.enabled,
config: this.settings.strip().settings,
data: this.data
}));
2018-03-01 20:00:24 +01:00
this.settings.setSaved();
} catch (err) {
2018-03-01 20:00:24 +01:00
console.error(`Plugin ${this.id} configuration failed to save`, err);
throw err;
}
}
2018-02-21 18:46:27 +01:00
start(save = true) {
2018-02-12 23:49:44 +01:00
if (this.onstart && !this.onstart()) return false;
if (this.onStart && !this.onStart()) return false;
if (!this.enabled) {
this.userConfig.enabled = true;
2018-02-21 18:46:27 +01:00
if (save) this.saveConfiguration();
}
return true;
2018-01-30 16:59:27 +01:00
}
2018-02-21 18:46:27 +01:00
stop(save = true) {
2018-02-12 23:49:44 +01:00
if (this.onstop && !this.onstop()) return false;
if (this.onStop && !this.onStop()) return false;
2018-02-21 18:46:27 +01:00
if (this.enabled) {
this.userConfig.enabled = false;
if (save) this.saveConfiguration();
}
return true;
2018-01-30 16:59:27 +01:00
}
2018-02-21 18:46:27 +01:00
unload() {
PluginManager.unloadPlugin(this);
}
2018-01-30 16:59:27 +01:00
}