Skip to content

Commit

Permalink
add: toggleable flag translation
Browse files Browse the repository at this point in the history
  • Loading branch information
maamokun committed Dec 13, 2024
1 parent 6895b2d commit eca7c5a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ model server {
verificationChannel String @default("none")
levelsEnabled Boolean @default(false)
levelsMessage String @default("Congratulations, {user}! You've leveled up to level {level}!")
flagTrans Boolean @default(false)
}
25 changes: 19 additions & 6 deletions src/handlers/flagTranslation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@ const translate = new Translate({

const vision = new ImageAnnotatorClient({
apiKey: process.env.GOOGLE_API_KEY as string,
})
});

const prisma = new PrismaClient();

export async function translateMessage(reaction: MessageReaction, user: User) {
const server = reaction.message.guildId;
const serverData = await prisma.server.findUnique({
where: {
id: server as string,
},
});
if (!serverData?.flagTrans) return;
const country = emojiCountryCode(reaction.emoji.name as string);
const language = countryToLanguage(country);
if (!language) {
Expand All @@ -40,7 +47,7 @@ export async function translateMessage(reaction: MessageReaction, user: User) {
message.reactions.removeAll();
await dmUser(
user.id,
"MikanBot",
"MikanBot Flag Translation",
"You are being ratelimited! Please wait a bit before translating another message. You can speed this up by becoming a premium user.",
);
return;
Expand All @@ -62,7 +69,7 @@ export async function translateMessage(reaction: MessageReaction, user: User) {
},
},
features: [{ type: "TEXT_DETECTION" }],
})
});
const [image] = visionResult;
if (!image.textAnnotations) {
message.reactions.removeAll();
Expand All @@ -83,7 +90,10 @@ export async function translateMessage(reaction: MessageReaction, user: User) {
.setDescription(translation)
.setColor("#FF7700")
.setTimestamp();
await message.reply({ embeds: [embed], allowedMentions: { repliedUser: false } });
await message.reply({
embeds: [embed],
allowedMentions: { repliedUser: false },
});
message.reactions.removeAll();
const userDb = await prisma.user.findUnique({
where: {
Expand Down Expand Up @@ -111,7 +121,10 @@ export async function translateMessage(reaction: MessageReaction, user: User) {
.setDescription(translation)
.setColor("#FF7700")
.setTimestamp();
await message.reply({ embeds: [embed], allowedMentions: { repliedUser: false } });
await message.reply({
embeds: [embed],
allowedMentions: { repliedUser: false },
});
message.reactions.removeAll();
const userDb = await prisma.user.findUnique({
where: {
Expand All @@ -123,4 +136,4 @@ export async function translateMessage(reaction: MessageReaction, user: User) {
} else {
setTranslationRatelimit("translate", user.id, 30);
}
}
}
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Partials,
EmbedBuilder,
MessageReaction,
type User,
} from "discord.js";

const client = new Client({
Expand Down Expand Up @@ -62,8 +63,8 @@ client.on("messageReactionAdd", async (reaction: MessageReaction, user) => {
}
}
try {
if (emojiCountryCode(reaction.emoji.name)) {
await translateMessage(reaction, user);
if (emojiCountryCode(reaction.emoji.name as string)) {
await translateMessage(reaction, user as User);
}
} catch (error) {
return;
Expand Down

0 comments on commit eca7c5a

Please sign in to comment.