Search code examples
javascriptdiscorddiscord.jsbotsdistube

discord music bot with distube | discord.js


I have a problem with my code, i try to code a discord music bot but i always shows a error message in visual studio code. I tried multiple things but none of them worked. I use distube for the bot because its way more easier.

My code:

const Discord = require('discord.js')
    DisTube = require('distube')
    module.exports = {
        execute: async (client, message, args) => { 

    const distube = new DisTube(client, { searchSongs: false, emitNewSongOnly: false });


client.on("message", async (message) => {
    if (message.author.bot) return;
    if (!message.content.startsWith(config.prefix)) return;
    const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
    const command = args.shift();

    if (command == "play")
        distube.play(message, args.join(" "));
    
    if (command == "stop") {
        distube.stop(message);
        message.channel.send(`**<:miraimusic:828978156512739378> ⁓ Stopped the music!** 
> If you want me to rejoin, type m!play.`);
    }
    if (command == "queue") {
        let queue = distube.getQueue(message);
        message.channel.send(`**<:miraimusic:828978156512739378> ⁓ Current queue**:
> ` + queue.songs.map((song, id) => `**${id + 1}**. ${song.name} - \`${song.formattedDuration}\``
        ).slice(0, 10).join("\n"));
    }

    if ([`3d`, `bassboost`, `echo`, `karaoke`, `nightcore`, `vaporwave`].includes(command)) {
        let filter = distube.setFilter(message, command);
        message.channel.send(`**<:miraimusic:828978156512739378> ⁓ Added filter: **
> ` + (filter || "off"));
    }
    }
);

// DisTube event listeners, more in the documentation page
distube
    .on("playSong", (message, queue, song) => message.channel.send(
        `**<:miraimusic:828978156512739378> ⁓ Playing now:**
> ${song.name}\` - \`${song.formattedDuration}\``
    ))
    .on("addSong", (message, queue, song) => message.channel.send(
        `**<:miraimusic:828978156512739378> ⁓ Added: **
> ${song.name} - \`${song.formattedDuration}\` **to the queue.**`
    ))
    .on("playList", (message, queue, playlist, song) => message.channel.send(
        `**<:miraimusic:828978156512739378> ⁓ Playing now:**
>` `${song.name}\` - \`${song.formattedDuration}\``
    ))
    .on("addList", (message, queue, playlist) => message.channel.send(
        `**<:miraimusic:828978156512739378> ⁓ Added: **
> ${song.name} - \`${song.formattedDuration}\` **to the queue.**`
    ))
    // DisTubeOptions.searchSongs = true
    .on("searchResult", (message, result) => {
        let i = 0;
        message.channel.send(`**Choose an option from below**\n${result.map(song => `**${++i}**. ${song.name} - \`${song.formattedDuration}\``).join("\n")}\n*Enter anything else or wait 60 seconds to cancel*`);
    })
    // DisTubeOptions.searchSongs = true
    .on("searchCancel", (message) => message.channel.send(`Searching canceled`))
    .on("error", (message, e) => {
        console.error(e)
        message.channel.send("An error encountered: " + e);
  }

Error:

music.js(64, 34): The parser expected to find a '}' to match the '{' token here. music.js(5, 51): The parser expected to find a '}' to match the '{' token here. music.js(4, 22): The parser expected to find a '}' to match the '{' token here

I don't know what the problem is so yeah, please help me!


Solution

  • There were a few issues with closing ")" and "}". Try to keep your code properly formatted and with good identation so that finding these issues was easier.

    Here's the correct code:

    const Discord = require('discord.js')
    DisTube = require('distube')
    module.exports = {
      execute: async (client, message, args) => {
    
        const distube = new DisTube(client, { searchSongs: false, emitNewSongOnly: false });
    
        client.on("message", async (message) => {
          if (message.author.bot) return;
          if (!message.content.startsWith(config.prefix)) return;
          const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
          const command = args.shift();
    
          if (command == "play")
            distube.play(message, args.join(" "));
          
          if (command == "stop") {
            distube.stop(message);
            message.channel.send(`**<:miraimusic:828978156512739378> ⁓ Stopped the music!** 
            > If you want me to rejoin, type m!play.`);
          }
          if (command == "queue") {
            let queue = distube.getQueue(message);
            message.channel.send(`**<:miraimusic:828978156512739378> ⁓ Current queue**:
            > ` + queue.songs.map((song, id) => `**${id + 1}**. ${song.name} - \`${song.formattedDuration}\``
            ).slice(0, 10).join("\n"));
          }
    
          if ([`3d`, `bassboost`, `echo`, `karaoke`, `nightcore`, `vaporwave`].includes(command)) {
            let filter = distube.setFilter(message, command);
            message.channel.send(`**<:miraimusic:828978156512739378> ⁓ Added filter: **
            > ` + (filter || "off"));
          }
        });
    
        // DisTube event listeners, more in the documentation page
        distube
          .on("playSong", (message, queue, song) => message.channel.send(`**<:miraimusic:828978156512739378> ⁓ Playing now:**> ${song.name}\` - \`${song.formattedDuration}\``))
          .on("addSong", (message, queue, song) => message.channel.send(`**<:miraimusic:828978156512739378> ⁓ Added: **> ${song.name} - \`${song.formattedDuration}\` **to the queue.**`))
          .on("playList", (message, queue, playlist, song) => message.channel.send(`**<:miraimusic:828978156512739378> ⁓ Playing now:**>` `${song.name}\` - \`${song.formattedDuration}\``))
          .on("addList", (message, queue, playlist) => message.channel.send(`**<:miraimusic:828978156512739378> ⁓ Added: **> ${song.name} - \`${song.formattedDuration}\` **to the queue.**`))
          // DisTubeOptions.searchSongs = true
          .on("searchResult", (message, result) => {
            let i = 0;
            message.channel.send(`**Choose an option from below**\n${result.map(song => `**${++i}**. ${song.name} - \`${song.formattedDuration}\``).join("\n")}\n*Enter anything else or wait 60 seconds to cancel*`);
          })
          // DisTubeOptions.searchSongs = true
          .on("searchCancel", (message) => message.channel.send(`Searching canceled`))
          .on("error", (message, e) => {
            console.error(e)
            message.channel.send("An error encountered: " + e);
          })
      }
    }