Open Ticket v4 Beta
This commit is contained in:
@@ -0,0 +1,197 @@
|
||||
import {openticket, api, utilities} from "../../index"
|
||||
import fs from "fs"
|
||||
import { ODPluginError } from "../api/api"
|
||||
|
||||
export const loadAllPlugins = async () => {
|
||||
//start launching plugins
|
||||
openticket.log("Loading plugins...","system")
|
||||
let initPluginError: boolean = false
|
||||
|
||||
if (!fs.existsSync("./plugins")){
|
||||
openticket.log("Couldn't find ./plugins directory, canceling all plugin execution!","error")
|
||||
return
|
||||
}
|
||||
const plugins = fs.readdirSync("./plugins")
|
||||
|
||||
//check & validate
|
||||
plugins.forEach(async (p) => {
|
||||
//prechecks
|
||||
if (!fs.statSync("./plugins/"+p).isDirectory()) return openticket.log("Plugin is not a directory, canceling plugin execution...","plugin",[
|
||||
{key:"plugin",value:"./plugins/"+p}
|
||||
])
|
||||
if (!fs.existsSync("./plugins/"+p+"/plugin.json")){
|
||||
initPluginError = true
|
||||
openticket.log("Plugin doesn't have a plugin.json, canceling plugin execution...","plugin",[
|
||||
{key:"plugin",value:"./plugins/"+p}
|
||||
])
|
||||
return
|
||||
}
|
||||
|
||||
//plugin loading
|
||||
try {
|
||||
const rawplugindata: api.ODPluginData = JSON.parse(fs.readFileSync("./plugins/"+p+"/plugin.json").toString())
|
||||
|
||||
if (typeof rawplugindata != "object") throw new ODPluginError("Failed to load plugin.json")
|
||||
if (typeof rawplugindata.id != "string") throw new ODPluginError("Failed to load plugin.json/id")
|
||||
if (typeof rawplugindata.name != "string") throw new ODPluginError("Failed to load plugin.json/name")
|
||||
if (typeof rawplugindata.version != "string") throw new ODPluginError("Failed to load plugin.json/version")
|
||||
if (typeof rawplugindata.startFile != "string") throw new ODPluginError("Failed to load plugin.json/startFile")
|
||||
|
||||
if (typeof rawplugindata.enabled != "boolean") throw new ODPluginError("Failed to load plugin.json/enabled")
|
||||
if (typeof rawplugindata.priority != "number") throw new ODPluginError("Failed to load plugin.json/priority")
|
||||
if (!Array.isArray(rawplugindata.events)) throw new ODPluginError("Failed to load plugin.json/events")
|
||||
|
||||
if (!Array.isArray(rawplugindata.npmDependencies)) throw new ODPluginError("Failed to load plugin.json/npmDependencies")
|
||||
if (!Array.isArray(rawplugindata.requiredPlugins)) throw new ODPluginError("Failed to load plugin.json/requiredPlugins")
|
||||
if (!Array.isArray(rawplugindata.incompatiblePlugins)) throw new ODPluginError("Failed to load plugin.json/incompatiblePlugins")
|
||||
|
||||
if (typeof rawplugindata.details != "object") throw new ODPluginError("Failed to load plugin.json/details")
|
||||
if (typeof rawplugindata.details.author != "string") throw new ODPluginError("Failed to load plugin.json/details/author")
|
||||
if (typeof rawplugindata.details.shortDescription != "string") throw new ODPluginError("Failed to load plugin.json/details/shortDescription")
|
||||
if (typeof rawplugindata.details.longDescription != "string") throw new ODPluginError("Failed to load plugin.json/details/longDescription")
|
||||
if (typeof rawplugindata.details.imageUrl != "string") throw new ODPluginError("Failed to load plugin.json/details/imageUrl")
|
||||
if (typeof rawplugindata.details.projectUrl != "string") throw new ODPluginError("Failed to load plugin.json/details/projectUrl")
|
||||
if (!Array.isArray(rawplugindata.details.tags)) throw new ODPluginError("Failed to load plugin.json/details/tags")
|
||||
|
||||
if (rawplugindata.id != p) throw new ODPluginError("Failed to load plugin, directory name is required to match the id")
|
||||
|
||||
if (openticket.plugins.exists(rawplugindata.id)) throw new ODPluginError("Failed to load plugin, this id already exists in another plugin")
|
||||
|
||||
//plugin.json is valid => load plugin
|
||||
const plugin = new api.ODPlugin(p,rawplugindata)
|
||||
openticket.plugins.add(plugin)
|
||||
|
||||
}catch(e){
|
||||
//when any of the above errors happen, crash the bot when soft mode isn't enabled
|
||||
initPluginError = true
|
||||
openticket.log(e.message+", canceling plugin execution...","plugin",[
|
||||
{key:"path",value:"./plugins/"+p}
|
||||
])
|
||||
openticket.log("You can see more about this error in the ./otdebug.txt file!","info")
|
||||
openticket.debugfile.writeText(e.stack)
|
||||
|
||||
//try to get some crashed plugin data
|
||||
try{
|
||||
const rawplugindata: api.ODPluginData = JSON.parse(fs.readFileSync("./plugins/"+p+"/plugin.json").toString())
|
||||
openticket.plugins.unknownCrashedPlugins.push({
|
||||
name:rawplugindata.name ?? "./plugins/"+p,
|
||||
description:(rawplugindata.details && rawplugindata.details.shortDescription) ? rawplugindata.details.shortDescription : "This plugin crashed :(",
|
||||
})
|
||||
}catch{}
|
||||
}
|
||||
})
|
||||
|
||||
//sorted plugins (based on priority)
|
||||
const sortedPlugins = openticket.plugins.getAll().sort((a,b) => {
|
||||
return (b.priority - a.priority)
|
||||
})
|
||||
|
||||
//check for incompatible & missing plugins/dependencies
|
||||
const incompatibilities: {from:string,to:string}[] = []
|
||||
const missingDependencies: {id:string,missing:string}[] = []
|
||||
const missingPlugins: {id:string,missing:string}[] = []
|
||||
|
||||
//go trough all plugins for errors
|
||||
sortedPlugins.forEach((plugin) => {
|
||||
const from = plugin.id.value
|
||||
plugin.data.incompatiblePlugins.forEach((to) => {
|
||||
//deny incompatibility if it already exists
|
||||
if (incompatibilities.find((p) => (p.from == from && p.to == to) || (p.to == from && p.from == to))) return
|
||||
|
||||
//check for existence of both plugins => add to list
|
||||
if (openticket.plugins.exists(to)) incompatibilities.push({from,to})
|
||||
})
|
||||
plugin.dependenciesInstalled().forEach((missing) => missingDependencies.push({id:from,missing}))
|
||||
plugin.pluginsInstalled(openticket.plugins).forEach((missing) => missingPlugins.push({id:from,missing}))
|
||||
})
|
||||
|
||||
//handle all incompatibilities
|
||||
incompatibilities.forEach((match) => {
|
||||
const fromPlugin = openticket.plugins.get(match.from)
|
||||
if (fromPlugin && !fromPlugin.crashed){
|
||||
fromPlugin.crashed = true
|
||||
fromPlugin.crashReason = "incompatible.plugin"
|
||||
}
|
||||
const toPlugin = openticket.plugins.get(match.to)
|
||||
if (toPlugin && !toPlugin.crashed){
|
||||
toPlugin.crashed = true
|
||||
toPlugin.crashReason = "incompatible.plugin"
|
||||
}
|
||||
|
||||
openticket.log(`Incompatible plugins => "${match.from}" & "${match.to}", canceling plugin execution...`,"plugin",[
|
||||
{key:"path1",value:"./plugins/"+match.from},
|
||||
{key:"path2",value:"./plugins/"+match.to}
|
||||
])
|
||||
initPluginError = true
|
||||
})
|
||||
|
||||
//handle all missing dependencies
|
||||
missingDependencies.forEach((match) => {
|
||||
const plugin = openticket.plugins.get(match.id)
|
||||
if (plugin && !plugin.crashed){
|
||||
plugin.crashed = true
|
||||
plugin.crashReason = "missing.dependency"
|
||||
}
|
||||
|
||||
openticket.log(`Missing npm dependency "${match.missing}", canceling plugin execution...`,"plugin",[
|
||||
{key:"path",value:"./plugins/"+match.id}
|
||||
])
|
||||
initPluginError = true
|
||||
})
|
||||
|
||||
//handle all missing plugins
|
||||
missingPlugins.forEach((match) => {
|
||||
const plugin = openticket.plugins.get(match.id)
|
||||
if (plugin && !plugin.crashed){
|
||||
plugin.crashed = true
|
||||
plugin.crashReason = "missing.plugin"
|
||||
}
|
||||
|
||||
openticket.log(`Missing required plugin "${match.missing}", canceling plugin execution...`,"plugin",[
|
||||
{key:"path",value:"./plugins/"+match.id}
|
||||
])
|
||||
initPluginError = true
|
||||
})
|
||||
|
||||
//exit on error (when soft mode disabled)
|
||||
if (!openticket.defaults.getDefault("softPluginLoading") && initPluginError){
|
||||
console.log("")
|
||||
openticket.log("Please fix all plugin errors above & try again!","error")
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
//preload all events required for every plugin
|
||||
for (const plugin of sortedPlugins){
|
||||
if (plugin.enabled) plugin.data.events.forEach((event) => openticket.events.add(new api.ODEvent(event)))
|
||||
}
|
||||
|
||||
//execute all working plugins
|
||||
for (const plugin of sortedPlugins){
|
||||
const status = await plugin.execute(openticket.debug,false)
|
||||
|
||||
//exit on error (when soft mode disabled)
|
||||
if (!status && !openticket.defaults.getDefault("softPluginLoading")){
|
||||
console.log("")
|
||||
openticket.log("Please fix all plugin errors above & try again!","error")
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
for (const plugin of sortedPlugins){
|
||||
if (plugin.enabled){
|
||||
openticket.debug.debug("Plugin \""+plugin.id.value+"\" loaded",[
|
||||
{key:"status",value:(plugin.crashed ? "crashed" : "success")},
|
||||
{key:"crashReason",value:(plugin.crashed ? (plugin.crashReason ?? "/") : "/")},
|
||||
{key:"author",value:plugin.details.author},
|
||||
{key:"version",value:plugin.version.toString()},
|
||||
{key:"priority",value:plugin.priority.toString()}
|
||||
])
|
||||
}else{
|
||||
openticket.debug.debug("Plugin \""+plugin.id.value+"\" disabled",[
|
||||
{key:"author",value:plugin.details.author},
|
||||
{key:"version",value:plugin.version.toString()},
|
||||
{key:"priority",value:plugin.priority.toString()}
|
||||
])
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user