Skip to content

Instantly share code, notes, and snippets.

@itsanishjain
Created January 21, 2025 18:34
Show Gist options
  • Save itsanishjain/a5e6c2a9b3267216f60612016a75de2f to your computer and use it in GitHub Desktop.
Save itsanishjain/a5e6c2a9b3267216f60612016a75de2f to your computer and use it in GitHub Desktop.
discord-mod-bot
import OpenAI from "openai";
import fs from "fs";
const openai = new OpenAI({
baseURL: "https://api.deepseek.com",
apiKey: process.env.DEEPSEEK_API_KEY,
});
interface ClassificationResult {
isSpam: boolean;
confidence: number;
category: "job_post" | "product_promotion" | "normal_message";
}
export async function classifyMessage(
content: string
): Promise<ClassificationResult> {
try {
const response = await openai.chat.completions.create({
model: "deepseek-chat",
messages: [
{
role: "user",
content: `Is this message a question or related to alchemy, and does it NOT describe personal expertise, job postings, job searches, or product promotions? Answer true/false only: "${content}"`,
},
],
max_tokens: 10,
});
console.log({ response });
// Save the response to a file for debugging
fs.writeFileSync("response.json", JSON.stringify(response, null, 2));
// Extract the model's response
const modelResponse = response.choices[0]?.message?.content
?.trim()
.toLowerCase();
// Determine if the message is spam
const isSpam = modelResponse === "false"; // "false" means it's a job post, product promotion, etc.
// Assign the correct category
let category: "job_post" | "product_promotion" | "normal_message";
if (isSpam) {
// You can add additional logic here to differentiate between job posts and product promotions
category = "job_post"; // Default to job_post for spam
} else {
category = "normal_message";
}
return {
isSpam,
confidence: 1.0, // Confidence is high since it's a direct true/false response
category,
};
} catch (error) {
console.error("Error analyzing message:", error);
return {
isSpam: false,
confidence: 0,
category: "normal_message",
};
}
}
import {
Client,
Events,
GatewayIntentBits,
Message,
TextChannel,
} from "discord.js";
import dotenv from "dotenv";
import { classifyMessage } from "./aiClassifier";
dotenv.config();
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
// Add a ready event to confirm bot is running
client.once(Events.ClientReady, (readyClient) => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
});
client.on(Events.MessageCreate, async (message: Message) => {
console.log({ message });
// Ignore bot messages
if (message.author.bot) return;
try {
const classification = await classifyMessage(message.content);
if (classification.isSpam) {
await message.delete();
// Optionally notify the user
if (message.channel instanceof TextChannel) {
await message.channel.send({
content: `Message from ${message.author} was removed as it appears to be a job posting or promotion.`,
});
}
}
} catch (error) {
console.error("Error processing message:", error);
}
});
client.login(process.env.DISCORD_TOKEN);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment