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