From 6134bc2ced939bfb3e3b78d1b800b40db2ba0bba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=20Rodr=C3=ADguez?= Date: Wed, 14 Jul 2021 00:52:51 +0200 Subject: [PATCH] refactor: extract sendResponse into its own function (#366) See /~https://github.com/makigas/clank/pull/366 --- src/lib/interaction/basecommand.ts | 25 +++---------------------- src/lib/interaction/response.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 22 deletions(-) create mode 100644 src/lib/interaction/response.ts diff --git a/src/lib/interaction/basecommand.ts b/src/lib/interaction/basecommand.ts index f39d4bcf..d76a3a2a 100644 --- a/src/lib/interaction/basecommand.ts +++ b/src/lib/interaction/basecommand.ts @@ -1,14 +1,6 @@ -import axios from "axios"; import { APIGuildInteraction } from "discord-api-types"; import Makibot from "../../Makibot"; -import logger from "../logger"; - -const interactionsClient = axios.create({ - baseURL: "https://discord.com/api/v8", - headers: { - "Content-Type": "application/json", - }, -}); +import { sendResponse } from "./response"; export default abstract class InteractionCommand { protected readonly client: Makibot; @@ -22,18 +14,7 @@ export default abstract class InteractionCommand { abstract name: string; abstract handle(params?: Params): Promise; - sendResponse(response: string, ephemeral: boolean = false): Promise { - const payload: any = { - type: 4, - data: { content: response }, - }; - if (ephemeral) { - payload.data.flags = 64; - } - logger.debug("[interactions] sending response: ", payload); - return interactionsClient.post( - `/interactions/${this.event.id}/${this.event.token}/callback`, - payload - ); + sendResponse(response: string, ephemeral = false): Promise { + return sendResponse(this.event, response, ephemeral); } } diff --git a/src/lib/interaction/response.ts b/src/lib/interaction/response.ts new file mode 100644 index 00000000..87732863 --- /dev/null +++ b/src/lib/interaction/response.ts @@ -0,0 +1,27 @@ +import axios from "axios"; +import { APIGuildInteraction, APIInteractionResponse } from "discord-api-types"; +import logger from "../logger"; + +export async function sendResponse( + event: APIGuildInteraction, + response: string, + ephemeral = false +): Promise { + const payload: APIInteractionResponse = { + type: 4, + data: { content: response }, + }; + if (ephemeral) { + payload.data.flags = 64; + } + logger.debug("[interactions] sending response: ", payload); + return axios.post( + `https://discord.com/api/v8/interactions/${event.id}/${event.token}/callback`, + payload, + { + headers: { + "Content-Type": "application/json", + }, + } + ); +}