Skip to content

Commit

Permalink
fix: Fix to respond quickly (#48)
Browse files Browse the repository at this point in the history
* Extract task as pinMessage

* Format
  • Loading branch information
MikuroXina authored Oct 13, 2023
1 parent d313b91 commit a36f534
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 53 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

ピン留めちゃんです. 仲良くしてね.

ウェブフックを作成して環境変数にその ID やトークンを設定すれば, いつでもメッセージを複製して転送できます.
ウェブフックを作成して環境変数にその ID やトークンを設定すれば,
いつでもメッセージを複製して転送できます.

## 設定

Expand Down
22 changes: 11 additions & 11 deletions deno.json
Original file line number Diff line number Diff line change
@@ -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
}
}
92 changes: 51 additions & 41 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
InteractionHandlers,
InteractionResponseType,
InteractionType,
PartialMessage,
} from "./types.ts";

const errorResponse = (reason: string) => ({
Expand Down Expand Up @@ -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("コマンドの種類が違うから");
}
Expand All @@ -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,
Expand Down

0 comments on commit a36f534

Please sign in to comment.