JavaScript and TypeScript basics
This page teaches just enough JavaScript and TypeScript to follow the rest of the wiki. It is not a full course, but if you read it you will recognise...
This page teaches just enough JavaScript and TypeScript to follow the rest of the wiki. It is not a full course, but if you read it you will recognise everything in the bot examples. If you already know JS, skip to Your first bot.
Values and variables
A variable is a named box holding a value. Use const when the value will not be reassigned, and let when it will.
const name = 'Athena'; // a string (text)
let count = 0; // a number
count = count + 1; // now 1
const isReady = true; // a boolean (true or false)Prefer const. Reach for let only when you truly need to reassign.
Strings and template literals
Backtick strings let you drop values inside text with ${ }:
const user = 'Alex';
console.log(`Hello, ${user}!`); // Hello, Alex!console.log(...) prints to your terminal. It is your main tool for seeing what your bot is doing.
Objects and arrays
An object groups named values. An array is an ordered list.
const member = { name: 'Alex', roles: ['admin', 'member'] };
console.log(member.name); // Alex
console.log(member.roles[0]); // admin (arrays start at 0)
console.log(member.roles.length); // 2Functions
A function is reusable code. Modern JavaScript usually uses "arrow functions":
const add = (a, b) => a + b;
console.log(add(2, 3)); // 5
const greet = (who) => {
const message = `Hi ${who}`;
return message;
};You will pass functions to Athena all the time, for example "when a message arrives, run this function."
Events and callbacks
Athena is event driven. You register a function and Athena calls it when something happens:
client.on('messageCreate', (message) => {
// this runs every time a message is created
});client.on('eventName', fn) means "whenever eventName happens, call fn." The value Athena hands your function (here message) is the thing that happened.
Promises and async / await
Some actions take time (talking to Discord's servers). Those return a Promise, a placeholder for a value that arrives later. await waits for it. A function that uses await must be marked async.
client.on('messageCreate', async (message) => {
const sent = await client.createMessage(message.channel.id, 'Hello');
// 'sent' is the message after Discord confirms it
});If you do not need the result, you can write void in front to say "I am intentionally not waiting":
void client.createMessage(channelID, 'Hi');If you forget await and the call fails, you may get a confusing "unhandled promise rejection." When in doubt, await inside an async function.
What TypeScript adds
TypeScript is JavaScript plus types. A type describes what a value is, so the editor can warn you before you run anything.
const count: number = 0;
const name: string = 'Athena';
function double(n: number): number {
return n * 2;
}You rarely have to write types yourself with Athena, because Athena already describes its own shapes. When you type message. in VS Code, it shows you every property and method available. That autocomplete is the fastest way to learn the library.
Importing libraries
At the top of a file you bring in what you need:
import { Client, GatewayIntentBits } from 'athena';This pulls Client and GatewayIntentBits out of the athena library so you can use them.
You know enough
That is the whole toolkit for the examples in this wiki: variables, strings, objects, functions, events, async/await, and imports. Continue to Your first bot and you will see each of these in action.