# Writing your music commands

# Reading messages

To read messages you only need to write a messageCreate (opens new window) listener. Here you create a listener for the message event and get the message and save it into a message object if it is triggered.

client.on('messageCreate', message => {})

If the message either doesn't start with the prefix or was sent by a bot, exit early.

client.on('messageCreate', message => {
    if (!message.content.startsWith(prefix) || message.author.bot)
        return
})

Then create an args variable that slices off the prefix entirely, removes the leftover whitespaces and then splits it into an array by spaces.

const args = message.content.slice(prefix.length).trim().split(' ')

After that, create a command variable by calling args.shift(), which will take the first element in array and return it while also removing the command name from the original array.

const command = args.shift().toLowerCase()

Now your messageCreate listener will look like that:

client.on('messageCreate', message => {
    if (!message.content.startsWith(prefix) || message.author.bot)
        return

    const args = message.content
        .slice(prefix.length)
        .trim()
        .split(' ')
    const command = args.shift().toLowerCase()
})

# Adding a command

To add a command, simply compare command variable to your command name.

# Import DisTube package

Before adding your music commands, you need to import DisTube package, which manage your queue and make your commands very simple. Edit the head of the bot file when import packages to make your code clean. You can go to DisTube documentation (opens new window) to learn about DisTube's classes, events, etc.

const Discord = require('discord.js')
const DisTube = require('distube')
const { prefix, token } = require('./config.json')
const client = new Discord.Client({
    intents: ['Guilds', 'GuildVoiceStates', 'GuildMessages'],
})
const distube = new DisTube.default(client)