|
@@ -1,5 +1,6 @@
|
|
|
import telegraf from "./telegraf";
|
|
import telegraf from "./telegraf";
|
|
|
import { Middleware, ContextMessageUpdate } from "telegraf";
|
|
import { Middleware, ContextMessageUpdate } from "telegraf";
|
|
|
|
|
+import { isMainThread } from "worker_threads";
|
|
|
|
|
|
|
|
type middlewareFun = Middleware<ContextMessageUpdate>;
|
|
type middlewareFun = Middleware<ContextMessageUpdate>;
|
|
|
type commandArgs = [string|Array<string>, middlewareFun, ...middlewareFun[]]
|
|
type commandArgs = [string|Array<string>, middlewareFun, ...middlewareFun[]]
|
|
@@ -9,13 +10,18 @@ type commandArgs = [string|Array<string>, middlewareFun, ...middlewareFun[]]
|
|
|
*
|
|
*
|
|
|
* @class CommandRegister
|
|
* @class CommandRegister
|
|
|
*/
|
|
*/
|
|
|
-class CommandRegister {
|
|
|
|
|
|
|
|
|
|
- commands: string[];
|
|
|
|
|
|
|
+type CommandEntry = {
|
|
|
|
|
+ name: string,
|
|
|
|
|
+ middlewares: Function[],
|
|
|
|
|
|
|
|
- constructor() {
|
|
|
|
|
- this.commands = [];
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ help?: string
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+class CommandRegister {
|
|
|
|
|
+
|
|
|
|
|
+ commands: string[] = [];
|
|
|
|
|
+ registerdCommands: Map<string, CommandEntry> = new Map();
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Telegraf.command wrapper
|
|
* Telegraf.command wrapper
|
|
@@ -32,13 +38,54 @@ class CommandRegister {
|
|
|
}
|
|
}
|
|
|
const args: commandArgs = [commands, middleware, ...middlewares];
|
|
const args: commandArgs = [commands, middleware, ...middlewares];
|
|
|
if (typeof commands == "string") {
|
|
if (typeof commands == "string") {
|
|
|
- this.commands.push(commands)
|
|
|
|
|
|
|
+ if(this.registerdCommands.has(commands)){
|
|
|
|
|
+ throw new Error("Command `"+commands+"` already registred");
|
|
|
|
|
+ }
|
|
|
|
|
+ this.commands.push(commands);
|
|
|
|
|
+ this.registerdCommands.set(commands, {
|
|
|
|
|
+ name: commands,
|
|
|
|
|
+ middlewares: [middleware, ...middlewares]
|
|
|
|
|
+ });
|
|
|
} else if (Array.isArray(commands)) {
|
|
} else if (Array.isArray(commands)) {
|
|
|
|
|
+ for(let cmd of commands){
|
|
|
|
|
+ if(this.registerdCommands.has(cmd)){
|
|
|
|
|
+ throw new Error("Command `"+cmd+"` already registred");
|
|
|
|
|
+ }
|
|
|
|
|
+ this.registerdCommands.set(cmd, {
|
|
|
|
|
+ name: cmd,
|
|
|
|
|
+ middlewares: [middleware, ...middlewares]
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
this.commands = this.commands.concat(commands);
|
|
this.commands = this.commands.concat(commands);
|
|
|
}
|
|
}
|
|
|
return telegraf.bot.command.apply(telegraf.bot, args);
|
|
return telegraf.bot.command.apply(telegraf.bot, args);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ registerHelp(cmd: string, args: string[], description: string){
|
|
|
|
|
+ let argsJoined: string;
|
|
|
|
|
+ if(args.length > 0){
|
|
|
|
|
+ args = args.map(e => `<${e}>`);
|
|
|
|
|
+ args.push("");
|
|
|
|
|
+ argsJoined = args.join(" ");
|
|
|
|
|
+ }else{
|
|
|
|
|
+ argsJoined = "";
|
|
|
|
|
+ }
|
|
|
|
|
+ let line: string = `/${cmd} ${argsJoined}- ${description}`;
|
|
|
|
|
+ let entry = this.registerdCommands.get(cmd);
|
|
|
|
|
+ if(!entry){
|
|
|
|
|
+ throw new Error("Command `"+cmd+"` is not registred");
|
|
|
|
|
+ }
|
|
|
|
|
+ entry.help = line;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ getHelp(): string {
|
|
|
|
|
+ let helpLines: string[] = [];
|
|
|
|
|
+ for(let it of this.registerdCommands.values()){
|
|
|
|
|
+ if(it.help) helpLines.push(it.help)
|
|
|
|
|
+ }
|
|
|
|
|
+ return helpLines.sort().join("\n")
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Sort commands array
|
|
* Sort commands array
|
|
|
*
|
|
*
|
|
@@ -56,7 +103,7 @@ class CommandRegister {
|
|
|
* @memberof CommandRegister
|
|
* @memberof CommandRegister
|
|
|
*/
|
|
*/
|
|
|
isReserved(cmd: string): boolean {
|
|
isReserved(cmd: string): boolean {
|
|
|
- return this.commands.includes(cmd);
|
|
|
|
|
|
|
+ return this.registerdCommands.has(cmd);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|