From 79156d092c812233bf52b1e415675a9b906885bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Serna?= Date: Thu, 14 Jul 2022 13:10:09 -0300 Subject: [PATCH] Add node example --- examples/node.js | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 examples/node.js diff --git a/examples/node.js b/examples/node.js new file mode 100644 index 0000000..aedac40 --- /dev/null +++ b/examples/node.js @@ -0,0 +1,42 @@ +/** + * Biscuit node example +*/ + +/** @type {NodeJS.Process} process */ +const process = require("node:process"); +const { Session, GatewayIntents } = require("@oasisjs/biscuit"); + +/** @type {string} token */ +const token = process.env.TOKEN || "YOUR_TOKEN_HERE"; + +if (token === "") { + return new Error("Please set the TOKEN environment variable"); +} + +const intents = GatewayIntents.MessageContent | GatewayIntents.Guilds | GatewayIntents.GuildMessages; +const session = new Session({ token, intents }); +const PREFIX = ">"; + +session.on("ready", (data) => { + console.log("Ready! Let's start chatting!"); + console.log("Connected as: " + data.user.username); +}) + +session.on("messageCreate", (message) => { + if (message.author?.bot || !message.content.startsWith(PREFIX)) { + return; + } + + const args = message.content.substring(PREFIX.length).trim().split(/\s+/gm); + const name = args.shift()?.toLowerCase(); + + if (name === "ping") { + message.reply({ content: "pong!" }); + } +}); + +try { + session.start(); +} catch(err){ + throw err; +} \ No newline at end of file