From 4c29dd4a0055fb17c40d1ce0af7f4a2eb1c9399b Mon Sep 17 00:00:00 2001 From: maamokun/MikanDev Date: Fri, 13 Dec 2024 21:28:14 +0900 Subject: [PATCH] feat: XFixing capability --- prisma/schema.prisma | 1 + src/api/server.ts | 24 +++++++++++++----------- src/handlers/xfix.ts | 37 +++++++++++++++++++++++++++++++++++++ src/index.ts | 10 +++++++++- 4 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 src/handlers/xfix.ts diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 2986394..a2b28da 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -43,4 +43,5 @@ model server { levelsEnabled Boolean @default(false) levelsMessage String @default("Congratulations, {user}! You've leveled up to level {level}!") flagTrans Boolean @default(false) + xfix Boolean @default(true) } diff --git a/src/api/server.ts b/src/api/server.ts index 4805c82..d1572ba 100644 --- a/src/api/server.ts +++ b/src/api/server.ts @@ -1,5 +1,5 @@ import { Elysia } from "elysia"; -import { swagger } from '@elysiajs/swagger' +import { swagger } from "@elysiajs/swagger"; import { AcclinkEndpoint } from "./routes/account-link.ts"; import { dmEndpoint } from "./routes/dm.ts"; @@ -10,16 +10,18 @@ export const app = new Elysia({ aot: false }).onError(({ code, error }) => { }); }); -app.use(swagger({ - path: '/', - documentation: { - info: { - title: 'MikanBot API', - version: '1.0.0' - } - }, - exclude: ['/admin/*'], -})); +app.use( + swagger({ + path: "/", + documentation: { + info: { + title: "MikanBot API", + version: "1.0.0", + }, + }, + exclude: ["/admin/*"], + }), +); app.use(AcclinkEndpoint); app.use(dmEndpoint); diff --git a/src/handlers/xfix.ts b/src/handlers/xfix.ts new file mode 100644 index 0000000..a345998 --- /dev/null +++ b/src/handlers/xfix.ts @@ -0,0 +1,37 @@ +import type { Message } from "discord.js"; +import { PrismaClient } from "@prisma/client"; + +const prisma = new PrismaClient(); + +export const xfix = async (message: Message) => { + const guildData = await prisma.server.findUnique({ + where: { + id: message.guildId as string, + }, + }); + + if (!guildData?.xfix) return; + + const content = message.content; + + if (!content) return; + + const urlRegex = /https?:\/\/[^\s]+/g; + + let messageURL = content.match(urlRegex); + + if (!messageURL || messageURL.length === 0) return; + + let url = messageURL[0]; + + url = url + .replace("https://x.com", "https://fixupx.com") + .replace("https://twitter.com", "https://twittpr.com"); + + await message.suppressEmbeds(true); + + await message.reply({ + content: `[Enhanced embed](${url})`, + allowedMentions: { repliedUser: false }, + }); +}; diff --git a/src/index.ts b/src/index.ts index ff0bbfc..6fed751 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ import { setPresence } from "./presence"; import { handleCommand } from "./handlers/command"; import { handleLevel } from "./handlers/lvl"; import { translateMessage } from "./handlers/flagTranslation"; +import { xfix } from "./handlers/xfix.ts"; import { emojiCountryCode } from "country-code-emoji"; import { Client, @@ -22,6 +23,7 @@ const client = new Client({ GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.GuildMembers, GatewayIntentBits.DirectMessageReactions, + GatewayIntentBits.MessageContent, ], partials: [Partials.Message, Partials.Channel, Partials.Reaction], }); @@ -88,7 +90,13 @@ client.on("interactionCreate", async (interaction) => { client.on("messageCreate", async (message) => { if (message.author.bot) return; console.log("Received message!"); - await handleLevel(message); + handleLevel(message); + if ( + message.content.startsWith("https://x.com/") || + message.content.startsWith("https://twitter.com/") + ) { + xfix(message); + } }); client.on("guildCreate", async (guild) => {});