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

111 lines
3.8 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 { FileUtils } from 'common';
2018-01-30 16:59:27 +01:00
export default class {
constructor(pluginInternals) {
this.__pluginInternals = pluginInternals;
2018-02-02 18:52:38 +01:00
this.saveSettings = this.saveSettings.bind(this);
2018-01-30 16:59:27 +01:00
this.hasSettings = this.pluginConfig && this.pluginConfig.length > 0;
this.start = this.start.bind(this);
this.stop = this.stop.bind(this);
}
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-12 23:49:44 +01:00
get id() { return this.info.id || this.info.name.replace(/[^a-zA-Z0-9-]/g, '-').replace(/--/g, '-') }
2018-01-30 16:59:27 +01:00
get name() { return this.info.name }
get authors() { return this.info.authors }
get version() { return this.info.version }
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-02-12 23:49:44 +01:00
get pluginConfig() { return this.userConfig.config || [] }
get exports() { return this._exports ? this._exports : (this._exports = this.getExports()) }
2018-01-30 16:59:27 +01:00
2018-02-12 16:20:27 +01:00
getSetting(setting_id, category_id) {
for (let category of this.pluginConfig) {
if (category_id && category.category !== category_id) return;
for (let setting of category.settings) {
if (setting.id !== setting_id) return;
return setting.value;
}
}
2018-02-02 18:52:38 +01:00
}
2018-02-04 20:06:08 +01:00
async saveSettings(newSettings) {
for (let category of newSettings) {
const oldCategory = this.pluginConfig.find(c => c.category === category.category);
for (let setting of category.settings) {
const oldSetting = oldCategory.settings.find(s => s.id === setting.id);
if (oldSetting.value === setting.value) continue;
oldSetting.value = setting.value;
if (this.settingChanged) this.settingChanged(category.category, setting.id, setting.value);
2018-02-04 20:06:08 +01:00
}
2018-02-02 18:52:38 +01:00
}
2018-02-04 20:06:08 +01:00
this.saveConfiguration();
2018-02-04 20:06:08 +01:00
if (this.settingsChanged) this.settingsChanged(this.pluginConfig);
return this.pluginConfig;
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.pluginConfig.map(category => {
return {
category: category.category,
settings: category.settings.map(setting => {
return {
id: setting.id,
value: setting.value
};
})
};
})
}));
} catch (err) {
throw err;
}
}
2018-01-30 16:59:27 +01:00
start() {
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;
this.saveConfiguration();
}
return true;
2018-01-30 16:59:27 +01:00
}
stop() {
2018-02-12 23:49:44 +01:00
if (this.onstop && !this.onstop()) return false;
if (this.onStop && !this.onStop()) return false;
2018-01-30 16:59:27 +01:00
this.userConfig.enabled = false;
this.saveConfiguration();
return true;
2018-01-30 16:59:27 +01:00
}
}