Skip to content

Commit

Permalink
feat: Change to respond with editing sent (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikuroXina authored Sep 10, 2023
1 parent e27cf0f commit d313b91
Showing 1 changed file with 47 additions and 9 deletions.
56 changes: 47 additions & 9 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,31 @@ const errorResponse = (reason: string) => ({
},
});

const sendFollowup = (
const sendResponse = (
{ interactionId, interactionToken, content }: {
interactionId: string;
interactionToken: string;
content: string;
},
) =>
fetch(
[
ENDPOINT,
"interactions",
interactionId,
interactionToken,
"callback",
].join("/"),
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ content }),
},
);

const editSentResponse = (
{ applicationId, interactionToken, content }: {
applicationId: string;
interactionToken: string;
Expand All @@ -27,9 +51,11 @@ const sendFollowup = (
"webhooks",
applicationId,
interactionToken,
"messages",
"@original",
].join("/"),
{
method: "POST",
method: "PATCH",
headers: {
"Content-Type": "application/json",
},
Expand All @@ -47,13 +73,19 @@ const USER_AGENT =
"pindome-chan Bot (https://github.com/approvers/pindome-chan)";

const sendWebhook = async (
{ previewContent, message, interactionToken }: {
{ previewContent, message, interactionId, interactionToken }: {
previewContent: string;
message: FormData;
interactionId: string;
interactionToken: string;
},
{ applicationId, webhookId, webhookToken }: WebhookOptions,
): Promise<void> => {
await sendResponse({
interactionId,
interactionToken,
content: "ピン留め中…",
});
const res = await fetch(
[ENDPOINT, "webhooks", webhookId, webhookToken].join("/"),
{
Expand All @@ -64,17 +96,17 @@ const sendWebhook = async (
body: message,
},
);
if (!res.ok) {
console.error(await res.text());
const followupRes = await sendFollowup({
if (!res || !res.ok) {
console.error(await res?.text());
const followupRes = await editSentResponse({
applicationId,
interactionToken,
content: "ピン留めに失敗しちゃった……",
});
console.log(await followupRes.text());
return;
}
const followupRes = await sendFollowup({
const followupRes = await editSentResponse({
applicationId,
interactionToken,
content: `ピン留めできたよ!\n${previewContent}`,
Expand All @@ -97,7 +129,10 @@ const cutContent = (content: string): string => {
}
++i;
}
if (chars[i] === '`' && chars[i + 1] === '`' && chars[i + 2] === '`' && chars[i + 3] === '\n') {
if (
chars[i] === "`" && chars[i + 1] === "`" && chars[i + 2] === "`" &&
chars[i + 3] === "\n"
) {
isInCodeBlock = !isInCodeBlock;
if (isInCodeBlock) {
spoilerMarks.pop();
Expand All @@ -107,7 +142,9 @@ const cutContent = (content: string): string => {
}

const PREVIEW_LENGTH = 20;
const isCuttingSpoiler = spoilerSpans.some(([start, end]) => start <= PREVIEW_LENGTH && PREVIEW_LENGTH < end);
const isCuttingSpoiler = spoilerSpans.some(([start, end]) =>
start <= PREVIEW_LENGTH && PREVIEW_LENGTH < end
);

let cut = "";
cut += content.substring(0, PREVIEW_LENGTH);
Expand Down Expand Up @@ -172,6 +209,7 @@ export const makeCommands = (options: WebhookOptions): InteractionHandlers => [
void sendWebhook({
previewContent,
message: form,
interactionId: interaction.id,
interactionToken: interaction.token,
}, options);

Expand Down

0 comments on commit d313b91

Please sign in to comment.