You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.7 KiB

// @flow
import { get, set } from 'idb-keyval';
async function addPlugin(onStart: (ctx: Object)=>void, onStop: (ctx: Object)=>void, metadata: Object): Promise<boolean> {
// expected metadata: {name: "name", desc: "description", author: "author"}
const obj: Object = { // whether the plugin is started or stopped isn't going to be stored in the iDB, so it can be accessed more easily by all components
metadata: metadata,
code: {
start: onStart.toString(),
stop: onStop.toString() // the hackiest of the hacks
},
enabled: false // should plugins be enabled by default? not sure
}
const globalSettings: Object = await get("demoncord");
/*try {
if (globalSettings["plugin"][metadata.name] !== undefined) {
console.error("[Demoncord] Cannot add plugin that already exists!")
return false
}
} catch (error) {*/
globalSettings["plugin"][metadata.name] = obj
//} //Disabling checking for previous plugins for now as it is breaking literally fucking everything
await set("demoncord", globalSettings)
return true
}
async function delPlugin(name: string): Promise<boolean> {
const globalSettings = await get("demoncord")
if (globalSettings["plugin"][name] === undefined) {
console.error("[Demoncord] Cannot remove non-existant plugin!")
return false
} else {
globalSettings["plugin"][name] = undefined
delete globalSettings["plugin"][name]
}
await set("demoncord", globalSettings)
return true
}
async function startPlugin(name: string): Promise<boolean> {
const globalSettings = await get("demoncord")
if (globalSettings["plugin"][name] === undefined) {
console.error("[Demoncord] Cannot start non-existant plugin!")
return false
} else {
console.log(`[Demoncord] Starting ${name}...`)
const plug = globalSettings["plugin"][name]
const onStartStr: string = plug.code.start
const onStart = new Function('ctx', 'eeeeeeee', onStartStr)
let ctx = {} // ctx is how you're going to store things that need to be accessed in both onStart and onStop. dumb, i know
onStart(ctx, {})
console.log(`[Demoncord] Started ${name}!`)
window.demon.__plugins[name] = {status: 1, ctx: ctx}
return true
}
}
async function stopPlugin(name: string): Promise<boolean> {
const globalSettings = await get("demoncord")
if (globalSettings["plugin"][name] === undefined ) {
console.error("[Demoncord] Cannot stop non-existant or non-running plugin!")
return false
} else {
console.log(`[Demoncord] Stopping ${name}...`)
const plug = globalSettings["plugin"][name]
const onStartStr: string = plug.code.stop
const onStart = new Function('ctx', onStartStr)
onStart()
console.log(`[Demoncord] Stopped ${name}!`)
return true
}
}
export default {
addPlugin,
delPlugin
}