Skip to main content
Version: v2.10.0

Creating the main file

tip

This page assumes you've already set up the configuration files from the previous page. We're using the config.js approach, but feel free to substitute your own.

Open your code editor and create a new file. We suggest naming it index.js, but you may name it whatever you wish.

Here's the base code to get you started:

const { Client, Events, GatewayIntentBits } = require('discord.js');
const { Manager, SearchPlatform, AutoPlayPlatform, TrackPartial } = require("magmastream");
const config = require('./config.js');

// Create a new client instance
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
],
});

// Assign Manager to the client
client.manager = new Manager({
playNextOnEnd: true, // optional โ€” recommended to be true
enablePriorityMode: false, // optional โ€” recommended if you have more than 1 node
normalizeYouTubeTitles: true, // optional โ€” recommended to be true
trackPartial: [
TrackPartial.Author,
TrackPartial.ArtworkUrl,
TrackPartial.Duration,
TrackPartial.Identifier,
TrackPartial.PluginInfo,
TrackPartial.Requester,
TrackPartial.SourceName,
TrackPartial.Title,
TrackPartial.Track,
TrackPartial.Uri,
], // optional but recommended
defaultSearchPlatform: SearchPlatform.YouTube,
autoPlaySearchPlatforms: [
AutoPlayPlatform.Spotify,
AutoPlayPlatform.Deezer,
AutoPlayPlatform.YouTube,
],
nodes: config.nodes,
send: (packet) => {
const guild = client.guilds.cache.get(packet.d.guild_id);
if (guild) guild.shard.send(packet);
},
});

client.once(Events.ClientReady, async (client) => {
await client.manager.init({ clientId: client.user.id });
console.log(`Ready! Logged in as ${client.user.tag}`);
});

client.on(Events.Raw, async (data) => {
await client.manager.updateVoiceState(data);
});

// Log in the client
client.login(config.token);

This code creates a Discord client and a MagmaStream manager, then logs in to Discord. GatewayIntentBits.Guilds and GatewayIntentBits.GuildVoiceStates are required โ€” the voice states intent is specifically needed by MagmaStream to track voice channel activity.

note

GatewayIntentBits.MessageContent is only needed if your bot reads the content of messages (e.g. for prefix commands). It is not required by MagmaStream and has been omitted here.

Intents define which events Discord sends to your bot. You can read more about them on the Intents topic .

note

The term "guild" is used by the Discord API and discord.js to refer to a Discord server.


Running your applicationโ€‹

Run the following in your terminal to start the bot:

node index.js

If you see Ready! after a few seconds, you're good to go. The next step is adding slash commands to build out your bot's functionality.

You can also edit the "main" field in your package.json to point to your main file, then use node . as a shorthand. After stopping the process with Ctrl + C, pressing the up arrow and Enter is a quick way to restart it.


Resulting codeโ€‹

You can review the complete example on the GitHub repository here .