Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Commit

Permalink
refactor: extract sendResponse into its own function (#366)
Browse files Browse the repository at this point in the history
See #366
  • Loading branch information
danirod authored Jul 13, 2021
1 parent dde2897 commit 6134bc2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
25 changes: 3 additions & 22 deletions src/lib/interaction/basecommand.ts
Original file line number Diff line number Diff line change
@@ -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<Params> {
protected readonly client: Makibot;
Expand All @@ -22,18 +14,7 @@ export default abstract class InteractionCommand<Params> {
abstract name: string;
abstract handle(params?: Params): Promise<void>;

sendResponse(response: string, ephemeral: boolean = false): Promise<void> {
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<void> {
return sendResponse(this.event, response, ephemeral);
}
}
27 changes: 27 additions & 0 deletions src/lib/interaction/response.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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",
},
}
);
}

0 comments on commit 6134bc2

Please sign in to comment.