Hot reload code (not finished)
This commit is contained in:
parent
cdaba3c951
commit
6a4021d5ae
|
@ -1,5 +1,7 @@
|
||||||
prefixes:
|
prefixes:
|
||||||
- id: "**"
|
- id: "*"
|
||||||
spaced: false
|
spaced: false
|
||||||
- id: cdebug
|
- id: cosmic
|
||||||
spaced: true
|
spaced: true
|
||||||
|
- id: c
|
||||||
|
spaced: false
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
debug: true
|
debug: true
|
||||||
enableConsole: true
|
enableConsole: true
|
||||||
enableMPP: true
|
enableMPP: false
|
||||||
enableDiscord: false
|
enableDiscord: false
|
||||||
enableSwitchChat: false
|
enableSwitchChat: false
|
||||||
|
|
|
@ -46,7 +46,7 @@ export type BaseCommandMessage<T = unknown> = Omit<
|
||||||
|
|
||||||
export class CommandHandler {
|
export class CommandHandler {
|
||||||
public static commandGroups = new Array<CommandGroup>();
|
public static commandGroups = new Array<CommandGroup>();
|
||||||
public static prefixes = new Array<Prefix>();
|
public static prefixes = new Set<Prefix>();
|
||||||
|
|
||||||
public static logger = new Logger("Command Handler");
|
public static logger = new Logger("Command Handler");
|
||||||
|
|
||||||
|
@ -172,5 +172,5 @@ export class CommandHandler {
|
||||||
|
|
||||||
// Add prefixes
|
// Add prefixes
|
||||||
for (const prefix of prefixConfig.prefixes) {
|
for (const prefix of prefixConfig.prefixes) {
|
||||||
CommandHandler.prefixes.push(prefix);
|
CommandHandler.prefixes.add(prefix);
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,3 +50,5 @@ export function loadCommands() {
|
||||||
]);
|
]);
|
||||||
CommandHandler.addCommandGroup(utility);
|
CommandHandler.addCommandGroup(utility);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export { CommandHandler, CommandGroup };
|
||||||
|
|
45
src/index.ts
45
src/index.ts
|
@ -1,12 +1,47 @@
|
||||||
import { loadCommands } from "./commands";
|
import { loadCommands, CommandHandler, type CommandGroup } from "./commands";
|
||||||
import { loadRoleConfig } from "./permissions";
|
import { loadRoleConfig } from "./permissions";
|
||||||
import { ServiceLoader } from "./services";
|
import { ServiceLoader } from "./services";
|
||||||
|
import { ConsoleAgent } from "./services/console";
|
||||||
import { printStartupASCII } from "./util/ascii";
|
import { printStartupASCII } from "./util/ascii";
|
||||||
|
|
||||||
printStartupASCII();
|
declare global {
|
||||||
loadRoleConfig();
|
var loaded: boolean;
|
||||||
loadCommands();
|
var serviceLoader: any;
|
||||||
ServiceLoader.loadServices();
|
var commandHandler: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set on first run
|
||||||
|
globalThis.loaded ??= false;
|
||||||
|
globalThis.serviceLoader ??= ServiceLoader;
|
||||||
|
globalThis.commandHandler ??= CommandHandler;
|
||||||
|
|
||||||
|
function load() {
|
||||||
|
printStartupASCII();
|
||||||
|
loadRoleConfig();
|
||||||
|
loadCommands();
|
||||||
|
globalThis.serviceLoader.loadServices();
|
||||||
|
|
||||||
|
globalThis.loaded = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for hot reload
|
||||||
|
if (!globalThis.loaded) {
|
||||||
|
load();
|
||||||
|
console.log("Command list:", globalThis.commandHandler.commandGroups);
|
||||||
|
} else {
|
||||||
|
console.log("Hot reload triggered...");
|
||||||
|
// Set console prompt
|
||||||
|
globalThis.serviceLoader.agents.forEach(agent => {
|
||||||
|
if (agent.platform === "console")
|
||||||
|
(agent as ConsoleAgent).client.prompt();
|
||||||
|
});
|
||||||
|
|
||||||
|
// Reload commands
|
||||||
|
globalThis.commandHandler.commandGroups = new Array<CommandGroup>();
|
||||||
|
loadCommands();
|
||||||
|
|
||||||
|
console.log("Command list:", globalThis.commandHandler.commandGroups);
|
||||||
|
}
|
||||||
|
|
||||||
export function scopedEval(code: string) {
|
export function scopedEval(code: string) {
|
||||||
return eval(code);
|
return eval(code);
|
||||||
|
|
|
@ -37,7 +37,7 @@ export class ConsoleAgent extends ServiceAgent<readline.ReadLine> {
|
||||||
public stop() {
|
public stop() {
|
||||||
if (!this.started) return;
|
if (!this.started) return;
|
||||||
this.started = false;
|
this.started = false;
|
||||||
this.client.setPrompt("");
|
this.client.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected bindEventListeners(): void {
|
protected bindEventListeners(): void {
|
||||||
|
|
|
@ -50,6 +50,10 @@ export class ServiceLoader {
|
||||||
this.agents.push(agent);
|
this.agents.push(agent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static getAgent(index: number) {
|
||||||
|
return this.agents[index];
|
||||||
|
}
|
||||||
|
|
||||||
public static loadServices() {
|
public static loadServices() {
|
||||||
if (config.enableMPP) {
|
if (config.enableMPP) {
|
||||||
for (const uri of Object.keys(mppConfig.agents)) {
|
for (const uri of Object.keys(mppConfig.agents)) {
|
||||||
|
|
Loading…
Reference in New Issue