diff --git a/config/example.yaml b/config/example.yaml index 71cdc52..5dd5dda 100644 --- a/config/example.yaml +++ b/config/example.yaml @@ -2,6 +2,11 @@ command: # コマンド処理の間隔(分) number # 自然数のみが有効です。 interval: 10 + # コマンドの最大並列処理数 number + # 例: 2を指定すると、コマンドを並列に2つずつ処理します。 + # 並列処理の数であるため、最終的なコマンドの処理数は変わりません。 + # 自然数のみが有効です。 + maxParallels: 3 earthquake: # 地震発生情報を投稿することに必要な最大震度 number # 例: 30を指定すると、最大震度が震度3以上の地震発生情報のみを投稿します。 diff --git a/src/feature/command/index.ts b/src/feature/command/index.ts index d7bcd73..9b18e70 100644 --- a/src/feature/command/index.ts +++ b/src/feature/command/index.ts @@ -8,6 +8,7 @@ import followCommand from "@/feature/command/follow"; import unfollowCommand from "@/feature/command/unfollow"; import miqCommand from "@/feature/command/miq"; import initI18n from "@/lib/i18n"; +import config from "@/lib/config"; await initI18n(); console.log("コマンドの処理を行います"); @@ -84,60 +85,64 @@ try { ueuses = [...new Set(ueuses)]; - await Promise.all(ueuses.map(async (ueuse) => { - const mem = Memory.memory; - let text = ueuse.text; - text = text.replace(`@${mem.userid}`, ""); - text = text.trim(); - - const rows = text.split(/\r\n|\r|\n/).map(row => row.trim()); - const commandRow = rows.filter(row => row.startsWith("/"))[0]; - - if (!commandRow || commandRow === "") { - console.warn("コマンドが本文から参照できません"); - - const response = await client.request("ueuse/create", { - text: i18next.t("commandNotFound"), - replyid: ueuse.uniqid, - }); - - if (!response.success) - console.warn("ユーズの作成に失敗しました:", response.error_code); - - return; - } - - const args = commandRow.replace("/", "").split(" "); + for (let i = 0; i < ueuses.length; i += config.command.maxParallels) { + const chunk = ueuses.slice(i, i + config.command.maxParallels); - switch (args[0]) { - case "help": - await helpCommand(ueuse, args); - break; - case "weather": - await weatherCommand(ueuse); - break; - case "follow": - await followCommand(ueuse); - break; - case "unfollow": - await unfollowCommand(ueuse); - break; - case "miq": - await miqCommand(ueuse, args); - break; - default: - console.warn("不明なコマンドが入力されました:", args[0]); + await Promise.all(chunk.map(async (ueuse) => { + const mem = Memory.memory; + let text = ueuse.text; + text = text.replace(`@${mem.userid}`, ""); + text = text.trim(); + + const rows = text.split(/\r\n|\r|\n/).map(row => row.trim()); + const commandRow = rows.filter(row => row.startsWith("/"))[0]; + + if (!commandRow || commandRow === "") { + console.warn("コマンドが本文から参照できません"); const response = await client.request("ueuse/create", { - text: i18next.t("unknownCommand", { command: args[0] }), + text: i18next.t("commandNotFound"), replyid: ueuse.uniqid, }); if (!response.success) console.warn("ユーズの作成に失敗しました:", response.error_code); - break; - } - })); + + return; + } + + const args = commandRow.replace("/", "").split(" "); + + switch (args[0]) { + case "help": + await helpCommand(ueuse, args); + break; + case "weather": + await weatherCommand(ueuse); + break; + case "follow": + await followCommand(ueuse); + break; + case "unfollow": + await unfollowCommand(ueuse); + break; + case "miq": + await miqCommand(ueuse, args); + break; + default: + console.warn("不明なコマンドが入力されました:", args[0]); + + const response = await client.request("ueuse/create", { + text: i18next.t("unknownCommand", { command: args[0] }), + replyid: ueuse.uniqid, + }); + + if (!response.success) + console.warn("ユーズの作成に失敗しました:", response.error_code); + break; + } + })); + } process.exit(0); } catch (err: any) { diff --git a/src/lib/config.ts b/src/lib/config.ts index 7e0fa71..20ce3d4 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -6,6 +6,7 @@ import { EOL } from "node:os"; const schema = z.object({ command: z.object({ interval: z.number().int().positive(), + maxParallels: z.number().int().positive(), }), earthquake: z.object({ requireMaxScale: z.union([