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

93 lines
3.2 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);
}
get configs() { return this.__pluginInternals.configs }
get info() { return this.__pluginInternals.info }
get paths() { return this.__pluginInternals.paths }
get main() { return this.__pluginInternals.main }
get defaultConfig() { return this.configs.defaultConfig }
get userConfig() { return this.configs.userConfig }
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-01-30 23:21:06 +01:00
get pluginConfig() { return this.userConfig.config }
2018-01-30 16:59:27 +01:00
2018-02-02 18:52:38 +01:00
getSetting(settingId) {
return this.userConfig.config.find(setting => setting.id === settingId);
}
2018-02-04 20:06:08 +01:00
async sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async saveSettings(newSettings) {
await this.sleep(2000); // Fake sleep to test loading
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-02 18:52:38 +01:00
}
2018-02-04 20:06:08 +01:00
try {
await FileUtils.writeFile(`${this.pluginPath}/user.config.json`, JSON.stringify({ enabled: this.enabled, config: this.pluginConfig }));
} catch (err) {
throw err;
}
if (this.settingsChanged) this.settingsChanged(this.pluginConfig);
return this.pluginConfig;
2018-02-02 18:52:38 +01:00
}
2018-01-30 16:59:27 +01:00
start() {
if (this.onStart) {
const started = this.onStart();
if (started) {
return this.userConfig.enabled = true;
}
return false;
}
return this.userConfig.enabled = true; //Assume plugin started since it doesn't have onStart
}
stop() {
if (this.onStop) {
const stopped = this.onStop();
if (stopped) {
this.userConfig.enabled = false;
return true;
}
return false;
}
this.userConfig.enabled = false;
return true; //Assume plugin stopped since it doesn't have onStop
}
}