From a36f534757011e9bf32d478a3cc78397bf9bdbe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikuro=E3=81=95=E3=81=84=E3=81=AA?= Date: Fri, 13 Oct 2023 14:37:33 +0900 Subject: [PATCH] fix: Fix to respond quickly (#48) * Extract task as pinMessage * Format --- README.md | 3 +- deno.json | 22 ++++++------ src/commands.ts | 92 +++++++++++++++++++++++++++---------------------- 3 files changed, 64 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index 6259422..5bd9c35 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,8 @@ ピン留めちゃんです. 仲良くしてね. -ウェブフックを作成して環境変数にその ID やトークンを設定すれば, いつでもメッセージを複製して転送できます. +ウェブフックを作成して環境変数にその ID やトークンを設定すれば, +いつでもメッセージを複製して転送できます. ## 設定 diff --git a/deno.json b/deno.json index 878984e..dc9e60a 100644 --- a/deno.json +++ b/deno.json @@ -1,13 +1,13 @@ { - "imports": { - "crypto": "https://deno.land/std@0.194.0/node/crypto.ts", - "dotenv": "https://deno.land/std@0.194.0/dotenv/mod.ts", - "ed25519": "https://deno.land/x/ed25519@2.0.0/mod.ts" - }, - "tasks": { - "dev": "deno run -A src/main.ts" - }, - "compilerOptions": { - "strict": true - } + "imports": { + "crypto": "https://deno.land/std@0.194.0/node/crypto.ts", + "dotenv": "https://deno.land/std@0.194.0/dotenv/mod.ts", + "ed25519": "https://deno.land/x/ed25519@2.0.0/mod.ts" + }, + "tasks": { + "dev": "deno run -A src/main.ts" + }, + "compilerOptions": { + "strict": true + } } diff --git a/src/commands.ts b/src/commands.ts index 9757ca7..a336d37 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -5,6 +5,7 @@ import { InteractionHandlers, InteractionResponseType, InteractionType, + PartialMessage, } from "./types.ts"; const errorResponse = (reason: string) => ({ @@ -157,13 +158,61 @@ const cutContent = (content: string): string => { return cut; }; +async function pinMessage( + message: PartialMessage, + interaction: Interaction, + options: WebhookOptions, +) { + const form = new FormData(); + form.append( + "payload_json", + JSON.stringify({ + ...message, + content: `${message.content}\nby ${message.author.username}`.trim(), + allowed_mentions: { + parse: [], + }, + message_reference: { + message_id: message.id, + }, + attachments: message.attachments.map((attachment, index) => ({ + id: index, + filename: attachment.filename, + })), + }), + ); + for (let index = 0; index < message.attachments.length; ++index) { + const attachment = message.attachments[index]; + const res = await fetch(attachment.url); + const blob = await res.blob(); + + const UPLOAD_SIZE_LIMIT = 8 * 1024 * 1024; + if (UPLOAD_SIZE_LIMIT < blob.size) { + return errorResponse("アップロード上限を超えているから"); + } + form.append(`files[${index}]`, blob, attachment.filename); + } + + let previewContent = ""; + if (message.content.length !== 0) { + previewContent += cutContent(message.content); + } + + await sendWebhook({ + previewContent, + message: form, + interactionId: interaction.id, + interactionToken: interaction.token, + }, options); +} + export const makeCommands = (options: WebhookOptions): InteractionHandlers => [ [ { type: ApplicationCommandType.Message, name: "ピン留め", }, - async (interaction: Interaction) => { + (interaction: Interaction) => { if (interaction.type !== InteractionType.ApplicationCommand) { return errorResponse("コマンドの種類が違うから"); } @@ -172,46 +221,7 @@ export const makeCommands = (options: WebhookOptions): InteractionHandlers => [ return errorResponse("間に合わなかったから"); } const [message] = Object.values(messages); - const form = new FormData(); - form.append( - "payload_json", - JSON.stringify({ - ...message, - content: `${message.content}\nby ${message.author.username}`.trim(), - allowed_mentions: { - parse: [], - }, - message_reference: { - message_id: message.id, - }, - attachments: message.attachments.map((attachment, index) => ({ - id: index, - filename: attachment.filename, - })), - }), - ); - await Promise.all(message.attachments.map(async (attachment, index) => { - const res = await fetch(attachment.url); - const blob = await res.blob(); - - const UPLOAD_SIZE_LIMIT = 8 * 1024 * 1024; - if (UPLOAD_SIZE_LIMIT < blob.size) { - return errorResponse("アップロード上限を超えているから"); - } - form.append(`files[${index}]`, blob, attachment.filename); - })); - - let previewContent = ""; - if (message.content.length !== 0) { - previewContent += cutContent(message.content); - } - - void sendWebhook({ - previewContent, - message: form, - interactionId: interaction.id, - interactionToken: interaction.token, - }, options); + void pinMessage(message, interaction, options); return { type: InteractionResponseType.DeferredChannelMessageWithSource,