Spiele mit Schaltflächen
/coinflip postet zwei Schaltflächen, die nur der Spieler drücken kann; die Nachricht ändert sich an Ort und Stelle.
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
// Games with buttons: `/coinflip` asks heads or tails; only the player can
// answer, and the message updates in place with the result.
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("coinflip", (i) =>
i.reply({
content: "Heads or tails?",
components: [
// `restrict` makes the server refuse clicks from anyone else
{
type: "button",
style: "primary",
label: "Heads",
customId: "flip:heads",
restrict: { users: [i.user._id] },
},
{
type: "button",
style: "secondary",
label: "Tails",
customId: "flip:tails",
restrict: { users: [i.user._id] },
},
],
}),
);
bot.component("flip:", (i) => {
const guess = i.customId.split(":")[1];
const result = Math.random() < 0.5 ? "heads" : "tails";
// update the message the button is on and remove the buttons
return i.update({
content: `You said ${guess}, it was ${result}. ${guess === result ? "You win!" : "Try again."}`,
components: [],
});
});
await bot.login();
await bot.syncCommands([{ name: "coinflip", description: "Flip a coin" }]);
console.log("ready");
Starte es mit node games.mjs.
Probiere es aus
Führe /coinflip aus und drücke eine Schaltfläche. Bitte jemand anderen, sie auch zu drücken: Der Server lehnt ab.
Was es zeigt
restrictbeschränkt eine Schaltfläche serverseitig auf bestimmte Benutzer oder Rollen.i.update()ändert die Nachricht der Schaltfläche;components: []entfernt die Schaltflächen.- Punkte und Spiele müssen kostenlos bleiben: keine Wetten, kein Kauf oder Verkauf von Punkten.