Kord/src/services/MimicService.ts

65 lines
2.1 KiB
TypeScript

import { Message, TextChannel, PermissionFlagsBits } from 'discord.js';
import { WebhookService } from './WebhookService';
import { logger } from '../utils/logger';
export class MimicService {
public static async handleMessage(message: Message) {
if (message.author.bot) return;
if (!(message.channel instanceof TextChannel)) return;
let content = message.content;
let modified = false;
// Feature 1: Big Emoji
// If message is exactly one custom discord emoji, we enlarge it.
const customEmojiRegex = /^<a?:.+:(\d+)>$/i;
const match = content.match(customEmojiRegex);
if (match) {
const emojiId = match[1];
const isAnimated = content.startsWith('<a:');
const ext = isAnimated ? 'gif' : 'png';
const emojiUrl = `https://cdn.discordapp.com/emojis/${emojiId}.${ext}?size=256`;
// Replace the emoji string with its raw image URL
content = emojiUrl;
modified = true;
}
// Feature 2: Prank / Word Mimic
// Example logic replacing a keyword to alter user message
if (content.includes('kord')) {
content = content.replace(/kord/gi, 'Kord(최고존엄)');
modified = true;
}
if (modified) {
try {
// Ensure we have permissions to manage webhooks and messages
const me = message.guild?.members.me;
if (!me?.permissionsIn(message.channel).has(PermissionFlagsBits.ManageWebhooks)) {
logger.warn(`Missing ManageWebhooks in ${message.channel.id}`);
return; // Can't send mimic
}
const webhookClient = await WebhookService.getWebhookClient(message.channel);
if (webhookClient) {
// Send modified message copying the user's name and avatar
await webhookClient.send({
content,
username: message.member?.displayName || message.author.username,
avatarURL: message.author.displayAvatarURL(),
});
// Delete the original message silently
if (message.deletable) {
await message.delete();
}
}
} catch (error) {
logger.error(`MimicService Error:`, error);
}
}
}
}