-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathagent.ts
More file actions
114 lines (93 loc) · 3.16 KB
/
agent.ts
File metadata and controls
114 lines (93 loc) · 3.16 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env bun
/**
* Discord Agent - A full-featured AI agent running on Discord
*
* This agent:
* - Responds to @mentions and replies
* - Handles slash commands (/ping, /about, /help)
* - Persists conversations and memories to SQL database
* - Uses OpenAI for language understanding
*/
import { AgentRuntime } from "@elizaos/core";
import { config } from "dotenv";
import { character } from "./character";
import { registerDiscordHandlers, registerSlashCommands } from "./handlers";
// Load environment variables
config({ path: "../.env" });
config(); // Also check current directory
/**
* Validate required environment variables
*/
function validateEnvironment(): void {
const required = ["DISCORD_APPLICATION_ID", "DISCORD_API_TOKEN"];
const missing = required.filter((key) => !process.env[key]);
if (missing.length > 0) {
console.error(
`❌ Missing required environment variables: ${missing.join(", ")}`,
);
console.error(" Copy env.example to .env and fill in your credentials.");
process.exit(1);
}
// Check for model provider
const hasModelProvider =
process.env.OPENAI_API_KEY || process.env.ANTHROPIC_API_KEY;
if (!hasModelProvider) {
console.error(
"❌ No model provider configured. Set OPENAI_API_KEY or ANTHROPIC_API_KEY.",
);
process.exit(1);
}
// Optional: Check for Telegram token (for multi-bot setup)
if (process.env.TELEGRAM_BOT_TOKEN) {
console.log(" Telegram token detected (for multi-platform setup)");
}
}
/**
* Main entry point
*/
async function main(): Promise<void> {
console.log("🤖 Starting Discord Agent...\n");
validateEnvironment();
const [{ default: sqlPlugin }, { openaiPlugin }, { default: discordPlugin }] =
await Promise.all([
import("@elizaos/plugin-sql"),
import("@elizaos/plugin-openai"),
import("@elizaos/plugin-discord"),
]);
// Create the runtime with all required plugins
const runtime = new AgentRuntime({
character,
plugins: [
sqlPlugin, // Database persistence
openaiPlugin, // LLM provider
discordPlugin, // Discord client
],
logLevel: "info",
});
// Register custom event handlers
registerDiscordHandlers(runtime);
// Initialize the runtime (starts all services)
await runtime.initialize();
// Register slash commands after Discord is connected
await registerSlashCommands(runtime);
console.log(`\n✅ Agent "${character.name}" is now running on Discord!`);
console.log(` Application ID: ${process.env.DISCORD_APPLICATION_ID}`);
console.log(` Responds to: @mentions and replies`);
console.log(` Slash commands: /ping, /about, /help`);
console.log("\n Press Ctrl+C to stop.\n");
// Handle graceful shutdown
const shutdown = async (signal: string) => {
console.log(`\n🛑 ${signal} received. Shutting down gracefully...`);
await runtime.stop();
console.log("👋 Goodbye!");
process.exit(0);
};
process.on("SIGINT", () => shutdown("SIGINT"));
process.on("SIGTERM", () => shutdown("SIGTERM"));
// Keep the process running
await new Promise(() => {});
}
main().catch((error) => {
console.error("Fatal error:", error);
process.exit(1);
});