Bladeren bron

Added '/help' command

Parad0x 5 jaren geleden
bovenliggende
commit
d87c3176a4
5 gewijzigde bestanden met toevoegingen van 74 en 7 verwijderingen
  1. 3 0
      src/bot/app.ts
  2. 2 0
      src/bot/auth.ts
  3. 13 0
      src/bot/help.ts
  4. 2 0
      src/bot/index.ts
  5. 54 7
      src/commandRegister.ts

+ 3 - 0
src/bot/app.ts

@@ -106,6 +106,7 @@ function app() {
       ie(3, e);
     }
   });
+  cmdRegister.registerHelp("pendingApps", [], "List notactivated apps")
 
   // Register an App with name passed by argument
   // Return Name and authcode of new app instance
@@ -136,6 +137,7 @@ function app() {
       ie(4, e);
     }
   });
+  cmdRegister.registerHelp("registerApp", ["name"], "Register new, active app named by parameter");
 
   /**
    * Return list of apps paginated
@@ -185,6 +187,7 @@ function app() {
       ie(5, e);
     }
   });
+  cmdRegister.registerHelp("apps", [], "List registered apps")
 
   // Pagination support for list
   telegraf.bot.action(/^apps (\d+)$/, async (ctx) => {

+ 2 - 0
src/bot/auth.ts

@@ -89,6 +89,7 @@ function auth(): void {
       ie(2, e);
     }
   });
+  cmdRegister.registerHelp("auth", ["code"], "Authenticate user with <code>")
 
   // /auth should be the only supported command for not authorised account 
   telegraf.bot.use((ctx: Context, next) => {
@@ -111,5 +112,6 @@ function auth(): void {
       ie(4, e);
     }
   });
+  cmdRegister.registerHelp("getAuthCode", [], "Return actual auth code")
 }
 export default auth;

+ 13 - 0
src/bot/help.ts

@@ -0,0 +1,13 @@
+import cmdRegister from "../commandRegister";
+import telegraf from "../telegraf";
+
+import {Context} from "./auth";
+
+function help(){
+    cmdRegister.register("help", (ctx: Context)=>{
+        ctx.reply(cmdRegister.getHelp())
+    })
+    cmdRegister.registerHelp("help", [], "Return this help")
+}
+
+export default help;

+ 2 - 0
src/bot/index.ts

@@ -1,5 +1,6 @@
 import auth from "./auth";
 import app from "./app";
+import help from "./help";
 import telegraf from "../telegraf";
 import commandParts from 'telegraf-command-parts';
 
@@ -16,6 +17,7 @@ function bot(): void {
 
   // Modules
   auth();
+  help();
   app();
 
   // Start

+ 54 - 7
src/commandRegister.ts

@@ -1,5 +1,6 @@
 import telegraf from "./telegraf";
 import { Middleware, ContextMessageUpdate } from "telegraf";
+import { isMainThread } from "worker_threads";
 
 type middlewareFun =  Middleware<ContextMessageUpdate>;
 type commandArgs = [string|Array<string>, middlewareFun, ...middlewareFun[]]
@@ -9,13 +10,18 @@ type commandArgs = [string|Array<string>, middlewareFun, ...middlewareFun[]]
  *
  * @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
@@ -32,13 +38,54 @@ class CommandRegister {
     }
     const args: commandArgs = [commands, middleware, ...middlewares];
     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)) {
+      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);
     }
     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
    *
@@ -56,7 +103,7 @@ class CommandRegister {
    * @memberof CommandRegister
    */
   isReserved(cmd: string): boolean {
-    return this.commands.includes(cmd);
+    return this.registerdCommands.has(cmd);
   }
 }