Skip to content

Commit

Permalink
feat: XFixing capability
Browse files Browse the repository at this point in the history
  • Loading branch information
maamokun committed Dec 13, 2024
1 parent 9c136e8 commit 4c29dd4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
24 changes: 13 additions & 11 deletions src/api/server.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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);
Expand Down
37 changes: 37 additions & 0 deletions src/handlers/xfix.ts
Original file line number Diff line number Diff line change
@@ -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 },
});
};
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -22,6 +23,7 @@ const client = new Client({
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.DirectMessageReactions,
GatewayIntentBits.MessageContent,
],
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
});
Expand Down Expand Up @@ -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) => {});
Expand Down

0 comments on commit 4c29dd4

Please sign in to comment.