소스 검색

Add guide paging; get db entities directly from module

ghorsington 4 년 전
부모
커밋
cfdee2fd0a
2개의 변경된 파일26개의 추가작업 그리고 6개의 파일을 삭제
  1. 19 3
      bot/src/commands/guide.ts
  2. 7 3
      bot/src/main.ts

+ 19 - 3
bot/src/commands/guide.ts

@@ -44,9 +44,25 @@ async function listGuides(msg: Message, guideType: string, message: string) {
                             .where("guide.type = :type", { type: guideType })
                             .getMany();
 
-    let guides = allGuides
-            .reduce((p, c) => `${p}\n${c.displayName} -- ${c.keywords.map(c => c.keyword).join(" ")}`, "\n");
-    msg.channel.send(`${msg.author.toString()} ${message}\n\`\`\`${guides}\`\`\`\n\nTo display the guides, ping me with one or more keywords, like \`@NoctBot sybaris com\``);
+    const MAX_GUIDES_PER_MSG = 30;
+    let guides = `${msg.author.toString()} ${message}\n\`\`\``;
+    let guideNum = 0;
+    for(let guide of allGuides) {
+        guides += `${guide.displayName} -- ${guide.keywords.map(k => k.keyword).join(" ")}\n`;
+        guideNum++;
+
+        if(guideNum == MAX_GUIDES_PER_MSG) {
+            guides += "```";
+            await msg.channel.send(guides);
+            guides = "```\n";
+            guideNum = 0;
+        }
+    }
+
+    if(guideNum != 0) {
+        guides += "```\n\nTo display the guides, ping me with one or more keywords, like `@NoctBot sybaris com`";
+        await msg.channel.send(guides);
+    }
 }
 
 export default {

+ 7 - 3
bot/src/main.ts

@@ -1,3 +1,4 @@
+// We need some kind of module resolver for @db. We use module-alias.
 require("module-alias/register");
 
 import * as fs from "fs";
@@ -5,10 +6,11 @@ import * as path from "path";
 import { client } from "./client";
 import { ICommand, BotEvent, IBotCommand } from "./commands/command"
 import "reflect-metadata";
-import {createConnection} from "typeorm";
+import {createConnection, getConnectionOptions} from "typeorm";
 import { migrate } from "./lowdb_migrator";
 import { documentation } from "./util";
 import dotenv from "dotenv";
+import { DB_ENTITIES } from "@db/entities";
 
 if(process.env.NODE_ENV == "dev") {
     dotenv.config({
@@ -107,8 +109,10 @@ client.on("messageReactionAdd", (r, u) => {
 });
 
 async function main() {
-    await createConnection();
-    await migrate();
+    await createConnection({
+        ...await getConnectionOptions(),
+        entities: DB_ENTITIES 
+    });
 
     let commandsPath = path.resolve(path.dirname(module.filename), "commands");
     let files = fs.readdirSync(commandsPath);