Quick start
This guide builds a small discord.js bot with a /play command. For other libraries, see connectors.
-
Create the client and Raya. Create the connector before
client.login()so Raya sees every gateway event from the start.bot.js const { Client, GatewayIntentBits } = require('discord.js');const { Raya, Connectors } = require('raya.js');const client = new Client({intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates],});const raya = new Raya({nodes: [{ name: 'main', host: 'localhost', port: 2333, password: 'youshallnotpass' },],connector: new Connectors.DiscordJS(client),defaultSearchSource: 'youtube', // or 'spsearch' with LavaSrcplayerDefaults: { autoplay: true },voiceStatus: true,}); -
Listen to events. Raya emits typed events for nodes, players, tracks and the queue.
bot.js raya.on('nodeReady', (node) => console.log(`Lavalink node ${node.name} is ready`));raya.on('trackStart', (player, track) => {const channel = client.channels.cache.get(player.textChannelId);channel?.send(`Now playing **${track.info.title}**`);});raya.on('queueEnd', (player) => {client.channels.cache.get(player.textChannelId)?.send('Queue finished.');}); -
Handle the command.
raya.join()creates the player and waits until voice is connected.enqueue()adds the result and starts playing if the player is idle.bot.js client.on('interactionCreate', async (interaction) => {if (!interaction.isChatInputCommand() || interaction.commandName !== 'play') return;const voiceChannelId = interaction.member.voice.channelId;if (!voiceChannelId) return interaction.reply('Join a voice channel first.');await interaction.deferReply();const player = await raya.join({guildId: interaction.guildId,voiceChannelId,textChannelId: interaction.channelId,});const result = await player.search(interaction.options.getString('query'), {requester: interaction.user,});if (result.tracks.length === 0) return interaction.editReply('Nothing found.');const { added } = await player.enqueue(result);await interaction.editReply(result.type === 'playlist'? `Queued ${added.length} tracks from **${result.playlist.name}**`: `Queued **${added[0].info.title}**`,);});client.login(process.env.DISCORD_TOKEN); -
Run it.
Terminal window node bot.js
Add the usual commands
Section titled “Add the usual commands”const player = raya.getPlayer(interaction.guildId);
await player.skip(); // next trackawait player.pause(!player.paused); // toggle pauseawait player.setVolume(80); // 0-1000, 100 = original loudnessplayer.setLoop('queue'); // 'off' | 'track' | 'queue'player.queue.shuffle();await player.filters.bassBoost('high');await player.destroy(); // leave and clean upKeep going
Section titled “Keep going”- Learn the player and queue APIs.
- Make restarts invisible with snapshots.
- Generate your full setup with the Config Builder.