Dreylo Bot
Der Dreylo Bot ist in Node.js geschrieben und nutzt die revolt.js-Library (via dem eigenen dreylo.js NPM-Paket).
Pfad und Struktur
/home/revolt/dreylo-bot/
├── index.js # Bot-Hauptdatei
├── .env # Secrets (Bot-Token, API-Keys)
├── package.json
└── node_modules/
├── dreylo.js/ # Eigenes NPM-Paket (revolt.js Wrapper)
└── revolt.js/ # Stoat/Revolt Client Library
ENV-Variablen (.env)
TOKEN=BOT_SESSION_TOKEN
OPENAI_API_KEY=sk-...
API_URL=https://chat.dreylo.com/api
Bot deployen
cd /home/revolt && docker compose up -d --build dreylo-bot
Logs überwachen
docker logs dreylo-bot -f
Bot-Code: Grundstruktur
const { Client } = require("dreylo.js");
require("dotenv").config();
const client = new Client({
apiURL: process.env.API_URL,
});
// Bot ist bereit
client.on("ready", () => {
console.log(Bot eingeloggt als ${client.user.username});
});
// Nachricht empfangen
client.on("message", async (message) => {
if (message.author?.bot) return;
// Prefix-Commands
if (message.content.startsWith("!ping")) {
await message.channel.sendMessage("Pong! 🏓");
}
// @Mentions
if (message.mentionIds?.has(client.user.id)) {
// Bot wurde erwähnt
}
});
// Einloggen mit Session-Token
client.loginWithToken(process.env.TOKEN);
Nachrichten senden
// Einfache Nachricht
await message.channel.sendMessage("Hello!");
// Mit Embed
await message.channel.sendMessage({
content: "Schau mal:",
embeds: [{
title: "Embed Titel",
description: "Beschreibung",
colour: "#5865F2",
}]
});
// Reply
await message.channel.sendMessage({
content: "Antwort!",
replies: [{ id: message.id, mention: true }]
});
OpenAI Integration
const OpenAI = require("openai").default;
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
// Konversations-History (pro Channel)
const history = new Map();
async function chatWithGPT(channelId, userMessage) {
const msgs = history.get(channelId) || [];
msgs.push({ role: "user", content: userMessage });
// Auf max. 20 Messages begrenzen
const trimmed = msgs.slice(-20);
const res = await openai.chat.completions.create({
model: "gpt-4o-mini", // oder gpt-4, etc.
messages: [
{ role: "system", content: "Du bist ein hilfreicher Bot." },
...trimmed,
],
max_tokens: 500,
});
const reply = res.choices[0].message.content;
msgs.push({ role: "assistant", content: reply });
history.set(channelId, msgs.slice(-20));
return reply;
}
Purge-Command (Nachrichten löschen)
if (message.content.startsWith("!purge")) {
const amount = parseInt(message.content.split(" ")[1]) || 10;
// Nachrichten laden
const msgs = await message.channel.fetchMessages({ limit: amount + 1 });
// Löschen
for (const [id] of msgs) {
try {
await message.channel.deleteMessage(id);
} catch (e) {
console.error("Löschen fehlgeschlagen:", id);
}
}
}