Discordjs
Discord.js works out of the box with JSTime. Let’s write a simple bot. First create a directory and initialize it with jstime init.
mkdir my-botcd my-botjstime initNow install Discord.js.
jspm add discord.jsBefore we go further, we need to go to the Discord developer portal, login/signup, create a new Application, then create a new Bot within that application. Follow the official guide for step-by-step instructions.
Once complete, you’ll be presented with your bot’s private key. Let’s add this to a file called .env.local. JSTime automatically reads this file and loads it into process.env.
This is an example token that has already been invalidated.
DISCORD_TOKEN=NzkyNzE1NDU0MTk2MDg4ODQy.X-hvzA.Ovy4MCQywSkoMRRclStW4xAYK7IBe sure to add .env.local to your .gitignore! It is dangerous to check your bot’s private key into version control.
node_modules.env.localNow let’s actually write our bot in a new file called bot.ts.
// import discord.jsimport {Client, Events, GatewayIntentBits} from 'discord.js';
// create a new Client instanceconst client = new Client({intents: [GatewayIntentBits.Guilds]});
// listen for the client to be readyclient.once(Events.ClientReady, (c) => { console.log(`Ready! Logged in as ${c.user.tag}`);});
// login with the token from .env.localclient.login(process.env.DISCORD_TOKEN);Now we can run our bot with jstime run. It may take a several seconds for the client to initialize the first time you run the file.
$ jstime run bot.tsReady! Logged in as my-bot#1234You’re up and running with a bare-bones Discord.js bot! This is a basic guide to setting up your bot with JSTime; we recommend the official Discord docs for complete information on the discord.js API.