Spaces:
Running
Running
File size: 6,988 Bytes
91d9d20 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
var utils = require("../lib/utils");
var log = require("../lib/log");
var fs = require("fs");
var Child = require("child_process");
var requiredOptions = {
name: [
"String",
true
],
version: [
"String",
true
],
role: [
"Number",
true
],
author: [
"Array",
true
],
category: [
"String",
true
],
description: [
"Object",
true
],
delay: [
"Number",
true
],
guides: [
"Object",
true
],
dependencies: [
"Array",
false
],
envConfig: [
"Object",
false
]
}
module.exports = function () {
var apis = global.mira.apis;
var { systemOptions } = global.mira.config;
if (systemOptions.autoLoadPlugins.enable) {
var path = require("path");
var fs = require("fs");
var dir = path.resolve(global.mira.dir, "plugins");
var plugins = fs.readdirSync(dir).filter(item => item.endsWith(".js") && !systemOptions.autoLoadPlugins.ignore.includes(item));
function LoadPlugin(pluginPath) {
delete require.cache[pluginPath];
function error(message) {
var error = new Error(message);
error.at = pluginPath;
return error;
}
try {
var pl = new (require(pluginPath))();
var Options = pl.Options;
var Langs = pl.Langs;
var Reply = pl.Reply;
var React = pl.React;
var Schedule = pl.Schedule;
var Events = pl.Events;
var Main = pl.Main;
if (utils.getType(Options) !== "Object")
throw error("Object is required in Options");
for (var item of Object.entries(requiredOptions)) {
if (item[1][1] && !Options[item[0]].toString() || utils.getType(Options[item[0]]) !== item[1][0])
throw error(item[0] + " is not accepted.");
}
if (typeof Main !== "function")
throw error("function is required in Main.");
if (utils.getType(Langs) !== "Object")
throw error("Object is required in " + lang);
for (var lang in Langs) {
if (utils.getType(Langs[lang]) !== "Object")
throw error("Object is required in " + lang);
for (var content in Langs[lang]) {
if (utils.getType(Langs[lang][content]) !== "String")
throw error("String is required in " + content);
}
}
if (Reply && typeof Reply !== "function")
throw error("Reply must be function");
if (React && typeof React !== "function")
throw error("React must be function");
if (Schedule && typeof Schedule !== "function")
throw error("Schedule must be function");
if (Events && typeof Events !== "function")
throw error("Events must be function");
if (Options.dependencies) {
pl.dependencies = {}
var execOptions = {
cwd: dir,
stdio: "inherit",
shell: true
}
for (var dependencie of Options.dependencies) {
try {
pl.dependencies[dependencie] = require(dependencie);
} catch (error) {
Child.execSync("npm install " + dependencie, execOptions);
pl.dependencies[dependencie] = require(dependencie);
}
}
}
if (Options.envConfig) {
var envConfig = {}
envConfig[Options.name] = {}
for (var env in Options.envConfig) {
envConfig[Options.name][env] = Options.envConfig[env];
}
global.mira.configCommands = envConfig;
}
global.modules.cmds = global.modules.cmds.filter(item => item[0] !== Options.name);
global.modules.cmds.push([Options.name, pl]);
} catch (error) {
console.log(error);
}
}
plugins.forEach(plugin => {
var pluginPath = path.resolve(dir, plugin);
LoadPlugin(pluginPath);
if (systemOptions.autoReloadPlugins.enable && !systemOptions.autoReloadPlugins.ignore.includes(plugin)) {
var lastReload = fs.statSync(pluginPath).mtimeMs;
var reloading = false;
function reload() {
if (reloading)
return;
reloading = true;
setTimeout(function () {
reloading = false;
if (lastReload === fs.statSync(pluginPath).mtimeMs)
return;
try {
LoadPlugin(pluginPath);
log.info("control.modules.reload", plugin);
} finally {}
}, 2000);
}
fs.watch(pluginPath, type => {
if (type === "change")
reload();
});
}
});
log.info("control.modules.len", global.modules.cmds.length);
}
var Client = new apis.Client();
var model = require("./model");
log.info("control.model.connect");
log.info("control.Client.connect");
log.wall();
global.mira.Client = Client;
return Client
.on("message", message => {
switch (message.type) {
case "message":
model.Main(message);
case "message_reply":
case "message_unsend":
model.db.createDataBase(message);
model.Events(message);
break;
case "event":
break;
case "message_reaction":
break;
default:
break;
}
})
.on("error", function (error) {
if (error.type === "disconnect")
Client.disconnect();
log.error("control.Client.error", error.message || error);
console.log(error);
});
} |