-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.js
More file actions
46 lines (38 loc) · 1.57 KB
/
Copy pathagent.js
File metadata and controls
46 lines (38 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import OpenAI from "openai";
import config from "../config.json" assert { type: "json" };
import { logEvent } from "../modules/logger.js";
const client = new OpenAI({ apiKey: config.openai_key });
let memory = [];
function pruneMemory() {
while (memory.length > config.memory_limit) memory.shift();
}
export async function runAgent(botClient, msg) {
try {
if (msg.author.bot) return;
memory.push({ user: msg.author.username, content: msg.content });
pruneMemory();
// quick commands: support tools triggers handled elsewhere
// create strong system prompt and context
const systemPrompt = `
You are the AI Server Manager for a Discord server. You must:
- Be concise and polite in Arabic (Gulf dialect) unless asked otherwise.
- Manage moderation, tickets, roles, and provide admin suggestions.
- Keep answers short when asked for quick help, and expand when asked.
`;
const messages = [
{ role: "system", content: systemPrompt },
...memory.slice(-20).map(m => ({ role: "user", content: `${m.user}: ${m.content}` })),
{ role: "user", content: `User ${msg.author.username} asked: ${msg.content}` }
];
const res = await client.chat.completions.create({
model: "gpt-4o-mini",
messages
});
const reply = res.choices?.[0]?.message?.content || "آسف، ما قدرت أجاوب الحين.";
await msg.reply(reply);
logEvent(`AI replied to ${msg.author.tag}`);
} catch (e) {
logEvent("agent error: " + e);
try { msg.reply("صار خطأ داخلي بالـ AI، تواصل مع المدير."); } catch {}
}
}