feat: auto code upload (#25)

This commit is contained in:
Evo
2022-10-20 08:17:40 +05:30
committed by GitHub
parent ca69bab0b9
commit d17e3af572
3 changed files with 57 additions and 0 deletions

35
src/events/codeUpload.ts Normal file
View File

@@ -0,0 +1,35 @@
import { eventModule, EventType } from "@sern/handler";
import type { Message } from "discord.js";
import { upload } from "#utils";
export default eventModule({
type: EventType.Discord,
name: "messageCreate",
async execute(message: Message) {
if (
!message.guild ||
message.webhookId ||
message?.author?.bot ||
!message.channel.isTextBased() ||
message.channel.isDMBased() ||
!message.channel.parent
)
return;
// ! 2 cases, 1 -> normal msg (common), 2 -> uploaded file (rare [free PR])
if (message.channel.parentId !== "989982180837060729") return; //* ONLY WORK IN SUPPORT CATEGORY!
const { content } = message;
if (!content.includes("```")) return;
const code = content.split(/```.*/gi)[1].split("```")[0].trim();
if (code.split("\n").length < 10) return;
const link = await upload(code);
await message.reply({
content: `${message.author} I have uploaded the code at ${link}\nPlease use bins when sharing code!`,
});
if (code.split("\n").length > 20) await message.delete();
},
});

21
src/utils/codeUpload.ts Normal file
View File

@@ -0,0 +1,21 @@
import { fetch } from "undici";
export async function upload(code: string, name?: string) {
const response = await fetch("https://sourceb.in/api/bins", {
body: JSON.stringify({
title: "Code",
description: "Because I am lazy",
files: [{ name, content: code, languageId: 378 }],
}),
method: "POST",
headers: {
"Content-Type": "application/json",
},
});
const data = (await response.json()) as PostData;
return `<https://sourceb.in/${data.key}>`;
}
interface PostData {
key: string;
}

View File

@@ -5,3 +5,4 @@ export * from "./TicTacToe.js";
export * from "./Timestamp.js";
export * from "./pagination.js";
export * from "./randomStatus.js";
export * from "./codeUpload.js";