Browse Source

Update dead chat and quotes to sqlite

ghorsington 5 years ago
parent
commit
1c3fda2a92
2 changed files with 54 additions and 39 deletions
  1. 13 4
      src/commands/dead_chat.ts
  2. 41 35
      src/commands/quote.ts

+ 13 - 4
src/commands/dead_chat.ts

@@ -1,5 +1,6 @@
-import { db, IRandomElementMixin } from "../db";
 import { ICommand } from "./command";
+import { getRepository } from "typeorm";
+import { DeadChatReply } from "../entity/DeadChatReply";
 
 const triggers = [
     "dead server",
@@ -9,7 +10,7 @@ const triggers = [
 ];
 
 export default {
-    onMessage: (actionsDone, msg, content) => {
+    onMessage: async (actionsDone, msg, content) => {
         if (actionsDone)
             return false;
     
@@ -18,9 +19,17 @@ export default {
         if(!triggers.some(s => lowerContent.includes(s)))
             return false;
         
-        let deadChatReplies = db.get("deadChatReplies") as IRandomElementMixin;
+        let repo = getRepository(DeadChatReply);
 
-        msg.channel.send(deadChatReplies.randomElement().value());
+        let reply = await repo.query(`  select message
+                                        from dead_chat_reply
+                                        order by random()
+                                        limit 1`) as DeadChatReply[];
+        
+        if(reply.length == 0)
+            return false;
+
+        msg.channel.send(reply[0]);
         return true;
     }
 } as ICommand;

+ 41 - 35
src/commands/quote.ts

@@ -1,8 +1,7 @@
-import { db, IRandomElementMixin } from "../db";
-import { isAuthorised } from "../util";
+import { isAuthorisedAsync } from "../util";
 import { ICommand } from "./command";
-import { Collection } from "discord.js";
-import { CollectionChain } from "lodash";
+import { getRepository } from "typeorm";
+import { Quote } from "../entity/Quote";
 
 const quotePattern = /add quote by "([^"]+)"\s*(.*)/i;
 
@@ -13,17 +12,12 @@ function minify(str: string, maxLength: number) {
     return result;
 }
 
-interface Quote {
-    author: string,
-    message: string
-}
-
 export default {
     commands: [
         {
             pattern: "add quote",
-            action: (msg, c) => {
-                if (!isAuthorised(msg.member))
+            action: async (msg, c) => {
+                if (!isAuthorisedAsync(msg.member))
                     return;
         
                 let result = quotePattern.exec(c);
@@ -34,53 +28,71 @@ export default {
                 let author = result[1].trim();
                 let message = result[2].trim();
         
-                (db.get("quotes") as CollectionChain<any>).push({
+                let repo = getRepository(Quote);
+
+                let newQuote = await repo.save(repo.create({
                     author: author,
                     message: message
-                }).write();
+                }));
         
-                msg.channel.send(`${msg.author.toString()} Added quote #${db.get("quotes").size().value()}!`);
+                msg.channel.send(`${msg.author.toString()} Added quote (ID: ${newQuote.id})!`);
             }
         },
         {
             pattern: "random quote",
-            action: (msg) => {
-                if (db.get("quotes").size().value() == 0) {
+            action: async (msg) => {
+                let repo = getRepository(Quote);
+
+                let quotes = await repo.query(`  select *
+                                                from quote
+                                                order by random()
+                                                limit 1`) as Quote[];
+
+                if (quotes.length == 0) {
                     msg.channel.send("I have no quotes!");
                     return;
                 }
-                let quote = (db.get("quotes") as IRandomElementMixin).randomElement().value();
-                let index = (db.get("quotes") as CollectionChain<any>).indexOf(quote).value();
-                msg.channel.send(`Quote #${index + 1}:\n*"${quote.message}"*\n- ${quote.author}`);
+
+                let quote = quotes[0];
+                msg.channel.send(`Quote #${quote.id}:\n*"${quote.message}"*\n- ${quote.author}`);
             }
         },
         {
             pattern: "remove quote",
-            action: (msg, c) => {
+            action: async (msg, c) => {
                 let quoteNum = c.substring("remove quote".length).trim();
                 let val = parseInt(quoteNum);
-                if (isNaN(val) || db.get("quotes").size().value() < val - 1)
+                if (isNaN(val))
                     return;
-        
-                (db.get("quotes") as CollectionChain<any>).pullAt(val - 1).write();
+
+                let repo = getRepository(Quote);
+
+                let res = await repo.delete({ id: val });
+                if(res.affected == 0)
+                    return;
+
                 msg.channel.send(`${msg.author.toString()} Removed quote #${val}!`);
             }
         },
         {
             pattern: "quotes",
-            action: msg => {
-                if (!isAuthorised(msg.member)) {
+            action: async msg => {
+                if (!isAuthorisedAsync(msg.member)) {
                     msg.channel.send(`${msg.author.toString()} To prevent spamming, only bot moderators can view all quotes!`);
                     return;
                 }
+
+                let repo = getRepository(Quote);
+
+                let quotes = await repo.find();
         
-                if (db.get("quotes").size().value() == 0) {
+                if (quotes.length == 0) {
                     msg.channel.send("I have no quotes!");
                     return;
                 }
                 
-                let quotes = (db.get("quotes") as CollectionChain<any>).reduce((prev: string, curr: Quote, i: number) => `${prev}[${i+1}] "${minify(curr.message, 10)}" by ${curr.author}\n`, "\n").value();
-                msg.channel.send(`${msg.author.toString()}I know the following quotes:\n\`\`\`${quotes}\`\`\``);
+                let quotesListing = quotes.reduce((p, c) => `${p}[${c.id}] "${minify(c.message, 10)}" by ${c.author}\n`, "\n");
+                msg.channel.send(`${msg.author.toString()}I know the following quotes:\n\`\`\`${quotesListing}\`\`\``);
             }
         }
     ],
@@ -102,10 +114,4 @@ export default {
             description: "Shows a random quote by someone special..."
         }
     }
-} as ICommand;
-
-
-// module.exports = {
-//     commands: commands,
-//     documentation: documentation
-// };
+} as ICommand;