-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
103 lines (87 loc) · 2.87 KB
/
bot.js
File metadata and controls
103 lines (87 loc) · 2.87 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
import "dotenv/config";
import config from "./config.js";
import * as moltbook from "./strategies/moltbook/index.js";
const MODES = [
{
name: "moltbook-growth",
enabled: true,
cycleHours: 4,
module: moltbook,
strategies: {
bootstrapMemory: true,
subscribeToSubmolts: false,
replyToComments: false,
networkWithTopAgents: false,
upvoteGoodContent: false,
createViralPost: true,
commentOnHotPosts: false,
commentOnSubmolts: false,
},
},
];
function log(msg) {
console.log(`[${new Date().toISOString()}] ${msg}`);
}
function sleep(ms) {
return new Promise((r) => setTimeout(r, ms));
}
let cycleCount = 0;
async function runCycle() {
log(`\n========== CYCLE ${cycleCount + 1} ==========\n`);
for (const mode of MODES) {
if (!mode.enabled) continue;
log(`--- Running mode: ${mode.name} ---`);
await mode.module.run(mode.strategies, cycleCount);
}
cycleCount++;
log(`\n========== CYCLE ${cycleCount} COMPLETE ==========`);
}
async function main() {
log(`=== ${config.agentName} Bot Starting ===`);
for (const mode of MODES) {
if (!mode.enabled) continue;
const active = Object.entries(mode.strategies).filter(([, v]) => v).map(([k]) => k);
log(`Mode: ${mode.name} | Strategies: ${active.join(", ")}`);
await mode.module.init(cycleCount);
}
const enabledModes = MODES.filter((m) => m.enabled);
if (!enabledModes.length) {
log("No modes enabled. Idling.");
setInterval(() => {}, 60 * 60 * 1000);
return;
}
const interval = Math.min(...enabledModes.map((m) => m.cycleHours));
log(`Cycle interval: ${interval} hours\n`);
// Chatroom keepalive — send a message every N minutes so the room stays alive
if (config.chatroom?.apiBase) {
const keepAliveMs = (config.chatroom.keepAliveMinutes || 10) * 60 * 1000;
log(`Chatroom keepalive: every ${config.chatroom.keepAliveMinutes || 10} min → ${config.chatroom.apiBase}`);
async function sendKeepAlive() {
try {
const res = await fetch(`${config.chatroom.apiBase}/api/messages`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
name: config.agentName,
text: `[keepalive] ${config.agentName} is online — come chat! Docs: ${config.product.docsUrl}`,
}),
});
if (res.ok) {
log("Chatroom keepalive sent");
} else {
log(`Chatroom keepalive failed: ${res.status}`);
}
} catch (err) {
log(`Chatroom keepalive error: ${err.message}`);
}
}
await sendKeepAlive();
setInterval(sendKeepAlive, keepAliveMs);
}
await runCycle();
setInterval(() => runCycle().catch((err) => log(`Cycle error: ${err.message}`)), interval * 60 * 60 * 1000);
}
main().catch((err) => {
console.error("Fatal:", err.message);
process.exit(1);
});