Browse Source

Add support for prefix commands

ghorsington 4 years ago
parent
commit
4a6d82caf4

+ 6 - 1
bot/src/main.ts

@@ -11,6 +11,7 @@ import { logger } from "./logging";
 import { PluginManager } from "./plugin_manager";
 
 export const plgMgr: PluginManager = new PluginManager(path.resolve(path.dirname(module.filename), "plugins"));
+const COMMAND_PREFIX = "/";
 
 client.bot.on("ready", async () => {
     logger.info("Starting up NoctBot");
@@ -29,6 +30,10 @@ client.bot.on("message", async m => {
         return;
     }
 
+    if (m.content.startsWith(COMMAND_PREFIX) && await plgMgr.runCommand("prefix", m, m.content)) {
+        return;
+    }
+
     let content = m.cleanContent.trim();
 
     if (await plgMgr.trigger("message", m, content))
@@ -40,7 +45,7 @@ client.bot.on("message", async m => {
             content = content.substring(`@${client.botUser.username}`.length).trim();
             const lowerCaseContent = content.toLowerCase();
             
-            if (await plgMgr.runCommand(m, content))
+            if (await plgMgr.runCommand("mention", m, content))
                 return;
 
             if (await plgMgr.trigger("directMention", m, lowerCaseContent))

+ 3 - 7
bot/src/model/plugin.ts

@@ -1,11 +1,14 @@
 import { Message, ClientEvents } from "discord.js";
 import { EsModuleClass, isModuleClass } from "../util";
 
+export type CommandType = "prefix" | "mention";
+
 export interface ICommand extends CommandOptions {
     action : MessageCommand;
 }
 
 export interface CommandOptions {
+    type: CommandType;
     pattern: string | RegExp;
     documentation?: CommandDocumentation;
     auth?: boolean;
@@ -40,13 +43,6 @@ export function isCustomEvent(s: string): s is keyof CustomEventType {
     return customEvents.has(s as keyof CustomEventType);
 }
 
-// export class PluginBase {
-//     botCommands: ICommand[] = [];
-//     botEvents: { [action in EventType]?: BotEvent } = {};
-//     async start(): Promise<void> {
-//         // empty on purpose
-//     }
-// }
 export interface IPlugin {
     PLUGIN_TYPE?: string;
     botCommands?: ICommand[];

+ 4 - 2
bot/src/plugin_manager.ts

@@ -1,7 +1,7 @@
 import path from "path";
 import fs from "fs";
 import { Message, Client, ClientEvents } from "discord.js";
-import { EventType, BotEvent, ICommand, isPlugin, BotEventData, isCustomEvent, IPlugin } from "./model/plugin";
+import { EventType, BotEvent, ICommand, isPlugin, BotEventData, isCustomEvent, IPlugin, CommandType } from "./model/plugin";
 import { isAuthorisedAsync } from "./util";
 
 
@@ -106,9 +106,11 @@ export class PluginManager {
         return eventData.actionsDone;
     }
 
-    async runCommand(m: Message, content: string): Promise<boolean> {
+    async runCommand(type: CommandType, m: Message, content: string): Promise<boolean> {
         const lowerCaseContent = content.toLowerCase();
         for (const c of this.commands) {
+            if (c.type != type)
+                continue;
             let match = false;
             let matchData: string | RegExpMatchArray = "";
             if (typeof (c.pattern) == "string" && lowerCaseContent.startsWith(c.pattern)) {

+ 2 - 0
bot/src/plugins/facemorph.ts

@@ -341,6 +341,7 @@ export class Facemorph {
     }
 
     @Command({
+        type: "mention",
         pattern: "caption last image"
     })
     captionLastImage({ message }: ICommandData): void {
@@ -348,6 +349,7 @@ export class Facemorph {
     }
 
     @Command({
+        type: "mention",
         pattern: "look at last image"
     })
     lookLastImage({ message }: ICommandData): void {

+ 1 - 0
bot/src/plugins/forums_news_checker.ts

@@ -290,6 +290,7 @@ React with ✅ (approve) or ❌ (deny).`;
     }
 
     @Command({
+        type: "mention",
         pattern: /^edit (\d+)\s+((.*[\n\r]*)+)$/i
     })
     async editPreview({ message, contents }: ICommandData): Promise<void> {

+ 7 - 2
bot/src/plugins/guide.ts

@@ -76,6 +76,7 @@ export class GuideCommands {
     }
 
     @Command({
+        type: "mention",
         pattern: /^make (\w+)\s+name:(.+)\s*keywords:(.+)\s*contents:((.*[\n\r]*)+)$/i,
         auth: true,
         documentation: {
@@ -154,6 +155,7 @@ export class GuideCommands {
     }
 
     @Command({
+        type: "mention",
         pattern: /^delete (\w+)\s+(.+)$/i,
         auth: true,
         documentation: {
@@ -196,7 +198,8 @@ export class GuideCommands {
         await message.reply(`no such ${type} with keywords \`${keywords.join(" ")}\`! Did you forget to specify all keywords?`);
     }
 
-    @Command({ 
+    @Command({
+        type: "mention",
         pattern: "guides", 
         documentation: { 
             description: "Lists all guides and keywords that trigger them.", 
@@ -207,7 +210,8 @@ export class GuideCommands {
         await this.listGuides(message, "guide", "Here are the guides I have:");
     }
 
-    @Command({ 
+    @Command({
+        type: "mention",
         pattern: "memes", 
         documentation: {
             description: "Lists all memes and keywords that trigger them.", 
@@ -219,6 +223,7 @@ export class GuideCommands {
     }
 
     @Command({
+        type: "mention",
         pattern: "misc", 
         documentation: {
             description: "Lists all additional keywords the bot reacts to.", 

+ 4 - 1
bot/src/plugins/help.ts

@@ -5,7 +5,10 @@ import { client } from "src/client";
 
 @Plugin
 export class Help {
-    @Command({ pattern: "help" })
+    @Command({
+        type: "mention",
+        pattern: "help" 
+    })
     async showHelp({ message }: ICommandData): Promise<void> {
         const isAuthed = await isAuthorisedAsync(message.member);
 

+ 2 - 1
bot/src/plugins/inspire.ts

@@ -5,7 +5,8 @@ import { tryDo } from "src/util";
 
 @Plugin
 export class Inspire {
-    @Command({ 
+    @Command({
+        type: "mention",
         pattern: "inspire me", 
         documentation: {
             description: "Generates an inspiring quote just for you", 

+ 5 - 1
bot/src/plugins/quote.ts

@@ -14,7 +14,8 @@ export class QuoteCommand {
         return result;
     }
 
-    @Command({ 
+    @Command({
+        type: "mention",
         pattern: "add quote", 
         auth: true, 
         documentation: {
@@ -42,6 +43,7 @@ export class QuoteCommand {
     }
 
     @Command({
+        type: "mention",
         pattern: "random quote", 
         documentation: {
             description: "Shows a random quote by someone special...", 
@@ -66,6 +68,7 @@ export class QuoteCommand {
     }
 
     @Command({
+        type: "mention",
         pattern: "remove quote",
         auth: true,
         documentation: {
@@ -89,6 +92,7 @@ export class QuoteCommand {
     }
 
     @Command({
+        type: "mention",
         pattern: "quotes", 
         documentation: {
             description: "Lists all known quotes.", 

+ 1 - 0
bot/src/plugins/rcg.ts

@@ -24,6 +24,7 @@ export class Rcg {
     }
 
     @Command({
+        type: "mention",
         pattern: "random comic",
         auth: false,
         documentation: {description: "Generates a comic just for you!", example: "random comic"}

+ 5 - 2
bot/src/plugins/react.ts

@@ -39,6 +39,7 @@ export class ReactCommands {
     }
 
     @Command({
+        type: "mention",
         pattern: "react to",
         auth: true,
         documentation: {
@@ -70,7 +71,8 @@ export class ReactCommands {
         }
     }
 
-    @Command({ 
+    @Command({
+        type: "mention",
         pattern: "remove reaction to", 
         auth: true, 
         documentation: {
@@ -90,7 +92,8 @@ export class ReactCommands {
         message.reply("removed reaction!");
     }
 
-    @Command({ 
+    @Command({
+        type: "mention",
         pattern: "reactions", 
         documentation: {
             description: "Lists all known messages this bot can react to.",