Discord bot to open dong files
1import {
2 AttachmentBuilder,
3 ChatInputCommandInteraction,
4 MessageFlags,
5 SlashCommandBuilder,
6} from "discord.js";
7import type { customClient } from "../../index.ts";
8import { download } from "../../lib/download.ts";
9import { readDong } from "../../lib/dong-io.ts";
10import { Mime } from "mime";
11import standardTypes from "mime/types/standard.js";
12import otherTypes from "mime/types/other.js";
13import { Buffer } from "node:buffer";
14
15const mime = new Mime(standardTypes, otherTypes);
16mime.define({ "audio/mpeg": ["mp3"] });
17
18export const data = new SlashCommandBuilder()
19 .setName("open")
20 .setDescription("Open a dong file!")
21 .addAttachmentOption((opt) =>
22 opt.setName("dong").setDescription("The dong file").setRequired(true)
23 );
24export const execute = async (
25 interaction: ChatInputCommandInteraction & { client: customClient }
26) => {
27 const dong = interaction.options.getAttachment("dong", true);
28 await interaction.deferReply({ flags: MessageFlags.Ephemeral });
29
30 const downloadedDong = await download(dong);
31
32 const output = await readDong(downloadedDong);
33 if (typeof output === "string") {
34 await interaction.editReply(output);
35 return;
36 }
37 const { image, audio } = output;
38
39 await interaction.editReply({
40 files: [
41 (() => {
42 const img = new AttachmentBuilder(Buffer.from(image.data.buffer), {
43 name: `${dong.name.match(/^.*(?=\.dong$)/gm)}.${mime.getExtension(
44 image.mime
45 )}`,
46 });
47 img.setSpoiler(true);
48 return img;
49 })(),
50 new AttachmentBuilder(Buffer.from(audio.data.buffer), {
51 name: `${dong.name.match(/^.*(?=\.dong$)/gm)}.${mime.getExtension(
52 audio.mime
53 )}`,
54 }),
55 ],
56 });
57};