Przeglądaj źródła

- Add command registry

Parad0x 7 lat temu
rodzic
commit
939e0530e0
3 zmienionych plików z 55 dodań i 3 usunięć
  1. 19 1
      src/bot/app.js
  2. 3 2
      src/bot/auth.js
  3. 33 0
      src/commandRegistry.js

+ 19 - 1
src/bot/app.js

@@ -2,6 +2,7 @@
 
 
 const moment = require("moment");
 const moment = require("moment");
 
 
+const cmdRegistry = require("../commandRegistry");
 const telegraf = require("../telegraf");
 const telegraf = require("../telegraf");
 const Extra = require('telegraf/extra');
 const Extra = require('telegraf/extra');
 const Markup = require('telegraf/markup');
 const Markup = require('telegraf/markup');
@@ -28,7 +29,7 @@ function app() {
     ctx.reply(`${app.name} deleted`);
     ctx.reply(`${app.name} deleted`);
   });
   });
 
 
-  telegraf.bot.command("pendingApps", async (ctx) => {
+  cmdRegistry.register("pendingApps", async (ctx) => {
     let pendingApps = await db.App.findAll({
     let pendingApps = await db.App.findAll({
       where: {
       where: {
         activated: false
         activated: false
@@ -52,5 +53,22 @@ function app() {
       .resize()
       .resize()
       .extra());
       .extra());
   });
   });
+
+  cmdRegistry.register("registerApp", async (ctx) => {
+    const args: string = await ctx.state.command.args;
+    if (args.length > 25) {
+      ctx.reply("Name to long!");
+      return;
+    }
+    const appName = args;
+    const instance = await db.App.create({
+      name: appName,
+      activated: true
+    });
+    ctx.reply(`App registered\n` +
+      `Name: ${instance.name}\n` +
+      `Auth: ${instance.auth}`);
+    return;
+  });
 }
 }
 module.exports = app;
 module.exports = app;

+ 3 - 2
src/bot/auth.js

@@ -2,6 +2,7 @@
 
 
 const moment = require("moment");
 const moment = require("moment");
 
 
+const cmdRegistry = require("../commandRegistry");
 const telegraf = require("../telegraf");
 const telegraf = require("../telegraf");
 const db = require("../db");
 const db = require("../db");
 const authCode = require("../authCode");
 const authCode = require("../authCode");
@@ -26,7 +27,7 @@ function auth() {
     next();
     next();
   });
   });
 
 
-  telegraf.bot.command("auth", async (ctx, next) => {
+  cmdRegistry.register("auth", async (ctx, next) => {
     if (ctx.user && ctx.user.activated == true) {
     if (ctx.user && ctx.user.activated == true) {
       ctx.reply("Already authorised");
       ctx.reply("Already authorised");
       return
       return
@@ -66,7 +67,7 @@ function auth() {
     }
     }
   })
   })
 
 
-  telegraf.bot.command("getAuthCode", (ctx) => {
+  cmdRegistry.register("getAuthCode", (ctx) => {
     ctx.reply(`Actual authentication code: ${authCode.get()}`);
     ctx.reply(`Actual authentication code: ${authCode.get()}`);
   });
   });
 }
 }

+ 33 - 0
src/commandRegistry.js

@@ -0,0 +1,33 @@
+//@flow
+
+const telegraf = require("./telegraf");
+
+class CommandRegistry {
+
+  commands: string[];
+
+  constructor() {
+    this.commands = [];
+  }
+
+  register(...argsRaw) {
+    const args = [].concat(argsRaw);
+    const command = args[0];
+    if (typeof command == "string") {
+      this.commands.push(command)
+    } else if (Array.isArray(command)) {
+      this.commands = this.commands.concat(command);
+    }
+    return telegraf.bot.command.apply(telegraf.bot, args);
+  }
+
+  sort() {
+    this.commands = this.commands.sort();
+  }
+
+  isReserved(cmd: string): boolean {
+    return this.commands.includes(cmd);
+  }
+}
+
+module.exports = new CommandRegistry();