mirror of
https://github.com/SrIzan10/sern-community.git
synced 2026-05-01 11:05:19 +00:00
feat: auto code upload (#25)
This commit is contained in:
35
src/events/codeUpload.ts
Normal file
35
src/events/codeUpload.ts
Normal 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
21
src/utils/codeUpload.ts
Normal 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;
|
||||
}
|
||||
@@ -5,3 +5,4 @@ export * from "./TicTacToe.js";
|
||||
export * from "./Timestamp.js";
|
||||
export * from "./pagination.js";
|
||||
export * from "./randomStatus.js";
|
||||
export * from "./codeUpload.js";
|
||||
|
||||
Reference in New Issue
Block a user