Events ๐ก
Events are the real MVPs in bot development! They're the magical building blocks that make your bot come aliveโeverything, including commands, are built on top of events!
Setting Up Events ๐๏ธโ
Setting up events is as easy as creating a file with the name of the event you want to listen to in the events
directory. Let's say we want to respond when our bot is ready:
File structure:
/src
/events
ready.js
Code for ready.js
:
export default () => {
console.log('Bot is ready!');
};
Bam! Now, when your bot is ready, it will log 'Bot is ready!' in the console.
Btw, this is just for example purposes. Your Robo will automatically log when ready by default!
Stacked Events ๐ฅโ
Need to listen to the same event with multiple files? No problem! Robo.js allows you to stack events. This is great for separating out different logic or features related to the same event. Let's say we want to do something else when the bot is ready:
File structure:
/src
/events
ready.js
/ready
playStatus.js
Code for playStatus.js
:
export default () => {
bot.user.setActivity('with code');
};
Now, when the bot is ready, it will not only log 'Bot is ready!' in the console, but also set its status to 'Playing with code'!
For larger bots with more complex use cases, check out our Modules Documentation to group your events and commands.
Use Cases ๐ ๏ธโ
Listening to events opens up a world of possibilities! Here are a few examples:
- ready.js: Sets up initial bot status, schedules tasks, or sends a message to a specific channel when the bot starts.
- guildMemberAdd.js: Sends a welcome message or assigns a default role to a member when they join the guild.
- messageCreate.js: Listens to messages and responds based on the content.
- interactionCreate.js: Listens to button clicks and reactions. You can create a bot that responds to user interactions with your custom buttons or emoji reactions!
- messageReactionAdd.js: Detects when a new reaction is added to a message. Useful for reaction roles or interactive messages!
Different events will pass different parameters. For example, the messageCreate
event will pass the message
object, and you can use it to reply, react, or do whatever you want! Some events, like voiceStateUpdate
, even pass down multiple parameters:
// voiceStateUpdate.js
export default (oldState, newState) => {
// do something with oldState and newState
};
Intents & Privileged Intents ๐ก๏ธโ
Depending on the events you're trying to listen to, you might need to add more intents in your bot's configuration. Note: some events like messageContent
may require privileged intents to be toggled on in your bot's Discord application settings.
Privileged intents include GUILD_PRESENCES
and GUILD_MEMBERS
, which cover events related to guild members' presence and activities. To enable these, you need to:
- Go to the Discord Developer Portal
- Select your application
- Navigate to the "Bot" section
- Scroll down to the "Privileged Gateway Intents" section
- Toggle on the intents you need
Common Event Names ๐โ
For a complete list of event names, check out this handy Event Names Cheat Sheet. Remember, you don't need to use client.on
like the examples in the sheetโjust create a file named after the event!
Robo Lifecycle ๐โ
In Robo.js, we've got some special events to give you an extra level of control over your bot's shenanigans: _start
, _stop
, and _restart
. Each one of these handy events receive the client
object as a parameter.
Here's the lowdown: _start
gets called before your bot has logged in. You can return a Promise in these events and Robo.js will patiently wait for them to finish up, but don't leave it hanging too long โ there's a 5-second time limit. Check out the details on timeouts here.
Use these lifecycle events to tidy up or get things ready for your Robo's grand entrance or exit. Perhaps you need to fire up an API server or disable buttons while your bot's snoozing.