Datei hochladen
/report erstellt eine CSV-Datei und postet sie.
Bevor du beginnst
Erstelle eine Anwendung, installiere sie auf einem Testserver und setze drei Umgebungsvariablen (siehe Starte deinen Bot):
export ZRZUR_BOT_TOKEN=YOUR_BOT_TOKEN
export ZRZUR_API_URL=https://api.zrzur.com
export ZRZUR_WS_URL=wss://events.zrzur.comDer Code
// File upload: `/report` writes a small CSV and attaches it to a message.
// Files go to the file server first (tag "attachments"); the message then
// carries the returned file ids.
import { Client } from "@zrzur/sdk/bot";
const bot = new Client({
token: process.env.ZRZUR_BOT_TOKEN,
apiUrl: process.env.ZRZUR_API_URL,
wsUrl: process.env.ZRZUR_WS_URL,
});
bot.command("report", async (i) => {
await i.defer();
const csv = `user,when\n${i.user.username},${new Date().toISOString()}\n`;
// 1. upload: POST {files}/attachments with the bot token, returns { id }
const fileId = await bot.rest.uploadFile(
"attachments",
new TextEncoder().encode(csv),
"report.csv",
"text/csv",
);
// 2. send a message that carries the file id
await bot.rest.req("post", `/channels/${i.channelId}/messages`, {
content: "Here is your report.",
attachments: [fileId],
});
await i.editReply("Report posted.");
// Shortcut: bot.messages.send(channelId, { files: [{ name, data, type }] })
// does both steps.
});
await bot.login();
await bot.syncCommands([{ name: "report", description: "Post a CSV report" }]);
console.log("ready");
Starte es mit node upload.mjs.
Probiere es aus
Führe /report aus. Der Bot braucht Upload Files in diesem Kanal.
Was es zeigt
- Dateien gehen zuerst auf den Dateiserver (Tag
attachments). - Die Nachricht trägt dann die zurückgegebene ID in
attachments. bot.messages.send(channel, { files })macht beide Schritte.