To set a custom status for your bot using Discord.js, you'll need to use the setActivity method provided by the Client object. Here's a step-by-step guide to help you achieve that:
Step 1: Set Up Your Bot
Make sure you have a Discord bot set up and that you have the necessary permissions. If you haven't set up a bot yet, you can follow these steps:
- Go to the Discord Developer Portal.
- Create a new application and configure it as a bot.
- Get the bot token from the "Bot" section.
- Invite your bot to your server using the OAuth2 URL generated in the "OAuth2" section.
Step 2: Install Discord.js
If you haven't already, you'll need to install Discord.js. You can do this by running the following command in your project directory:
npm install discord.js
Step 3: Implement Custom Status
Now you can set up your bot with a custom status. Here’s a simple example:
const { Client, GatewayIntentBits, Events } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds, // Required to work with guilds
GatewayIntentBits.GuildMessages, // Required if you want to read messages
GatewayIntentBits.MessageContent, // Required if you want to read message content
],
});
client.once(Events.ClientReady, (client) => {
console.log(`Logged in as ${client.user.tag}`);
// Set custom status
client.user.setActivity('Playing with Discord.js', { type: 'PLAYING' });
});
client.login('YOUR_BOT_TOKEN'); // Replace with your bot token
Details of setActivity Parameters
- The first parameter is the activity's name, which is the text that users will see.
- The second parameter is an options object where you define the type of activity. The available types are:
PLAYING
STREAMING
LISTENING
WATCHING
- You can also specify
url if you set the activity type to STREAMING.
Example with different activity types
Here’s how you can use different activity types in your bot:
client.once(Events.ClientReady, (client) => {
console.log(`Logged in as ${client.user.tag}`);
// Example of setting different activity types
client.user.setActivity('Playing with Discord.js', { type: 'PLAYING' });
client.user.setActivity('on a stream', {
type: 'STREAMING',
url: 'https://www.twitch.tv/yourstreaminglink',
});
client.user.setActivity('some music', { type: 'LISTENING' });
client.user.setActivity('a movie', { type: 'WATCHING' });
});
Running Your Bot
Once your code is set up, you can run your bot with:
node your-bot-file.js
Make sure to replace your-bot-file.js with the filename where you've saved your bot's code.
Now your Discord bot should be online and will display the custom status you've set! You can change the status dynamically by calling setActivity at any time in your code, for example, in response to user commands or events.