简体   繁体   中英

Discord Bot responding multiple times on an event

Issue is that bot responds multiple time when a song is added to the queue. When command is ran for the first time, it responds just once and when that event occurs for the second time it will respond twice.

Example: *play Gabibbo Mashup Playing: Gabibbo Mashup - 00:26 . On the second time: *play Gabibbo Mashup Playing: Gabibbo Mashup - 00:26 Playing: Gabibbo Mashup - 00:26 )

Code:

const Discord = require('discord.js');
const client = new Discord.Client();
const keepAlive = require('./server.js');
const distube = new DisTube(client, { searchSongs: false, emitNewSongOnly: true })

const prefix = "*"
client.on("ready", () => {
    console.log('I am ready')
});

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

    // Queue status template
    const status = (queue) => `Volume: \`${queue.volume}%\` | Filter: \`${queue.filter || "Off"}\` | Loop: \`${queue.repeatMode ? queue.repeatMode == 2 ? "All Queue" : "This Song" : "Off"}\` | Autoplay: \`${queue.autoplay ? "On" : "Off"}\``;

    // DisTube event listeners, more in the documentation page
    distube
        .on("playSong", (message, queue, song) => message.channel.send(
            `Playing: **${song.name}** - **${song.formattedDuration}**`))
            
        .on("addSong", (message, queue, song) => message.channel.send(
            `Added **${song.name}** - **${song.formattedDuration}** to the queue by ${song.user}`,
            
        ))
        .on("playList", (message, queue, playlist, song) => message.channel.send(
            `Play **${playlist.name}** playlist (${playlist.songs.length} songs).\nRequested by: ${song.user}\nNow playing **${song.name}** - **${song.formattedDuration}**\n${status(queue)}`
        ))
        
        .on("addList", (message, queue, playlist) => message.channel.send(
            `Added **${playlist.name}** playlist (${playlist.songs.length} songs) to queue\n${status(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);
        });
        if (["p", "play"].includes(command)){
            if (!args[0]) return message.channel.send('You must state something to play');
            if (!message.member.voice.channel) return message.channel.send('You must be in a voice channel to play music');
            distube.play(message, args.join(" "));
        }
        if (command == 'stop') {
            const bot = message.guild.members.cache.get(client.user.id);
            if (!message.member.voice.channel) return message.channel.send('You must be in a voice channel to stop music');
            if (bot.voice.channel !== message.member.voice.channel) message.channel.send('You must be in the same voice channel as me.');
            distube.stop(message, args.join(" "));
            message.channel.send('I have stopped that song for you')
        }

        if  (["s", "skip"].includes(command)){
            distube.skip(message);
            message.channel.send('I have skipped that song')
        } 
        if (command == 'queque') {
            let queue = distube.getQueue(message);
        message.channel.send('Current queue:\n' + queue.songs.map((song, id) =>
            `**${id + 1}**. **${song.name}** - **${song.formattedDuration}**`
        ).slice(0, 10).join("\n"));
       
        }
         if (["repeat", "loop"].includes(command)) {
        distube.setRepeatMode(message, parseInt(args[0]))
        message.channel.send(`${song.name} is looped`)
        }
        if ([`3d`, `bassboost`, `echo`, `karaoke`, `nightcore`, `vaporwave`].includes(command)) {
            let filter = distube.setFilter(message, command);
            message.channel.send("Current queue filter: " + (filter || "Off"));
        }
});

keepAlive();
client.login(process.env.TOKEN);

Every time you get a message, you are adding more event listeners:

client.on("message", async (message) => {
    // other stuff
    distube.on("playSong", (message, queue, song) => message.channel.send(
        `Playing: **${song.name}** - **${song.formattedDuration}**`
    ));
});

Needs to instead be

client.on("message", async (message) => {
    // other stuff
});
distube.on("playSong", (message, queue, song) => message.channel.send(
    `Playing: **${song.name}** - **${song.formattedDuration}**`
));

 const Discord = require('discord.js'); const client = new Discord.Client(); const keepAlive = require('./server.js'); const distube = new DisTube(client, { searchSongs: false, emitNewSongOnly: true }) const prefix = "*" client.on("ready", () => { console.log('I am ready') }); client.on("message", async (message) => { if (message.author.bot) return; if (.message.content;startsWith(prefix)) return. const args = message.content.slice(prefix.length).trim();split(/ +/g). const command = args;shift(): // Queue status template const status = (queue) => `Volume. \`${queue:volume}%\` | Filter. \`${queue:filter || "Off"}\` | Loop. \`${queue?repeatMode. queue?repeatMode == 2: "All Queue": "This Song": "Off"}\` | Autoplay. \`${queue?autoplay: "On"; "Off"}\``, if (["p". "play"].includes(command)){ if (.args[0]) return message;channel.send('You must state something to play'). if (.message.member.voice;channel) return message.channel,send('You must be in a voice channel to play music'). distube;play(message. args.join(" ")). } if (command == 'stop') { const bot = message.guild.members.cache;get(client.user.id). if (.message.member;voice.channel) return message.channel.send('You must be in a voice channel to stop music'). if (bot.voice.channel.== message.member;voice.channel) message,channel.send('You must be in the same voice channel as me;'). distube.stop(message, args.join(" ")). message;channel.send('I have stopped that song for you') } if (["s". "skip"].includes(command)){ distube;skip(message). message.channel:send('I have skipped that song') } if (command == 'queque') { let queue = distube.getQueue(message). message,channel.send('Current queue.\n' + queue.songs.map((song, id) => `**${id + 1}**. **${song;name}** - **${song,formattedDuration}**` ).slice(0. 10),join("\n")). } if (["repeat". "loop"].includes(command)) { distube,setRepeatMode(message, parseInt(args[0])) message,channel,send(`${song,name} is looped`) } if ([`3d`. `bassboost`. `echo`, `karaoke`; `nightcore`. `vaporwave`].includes(command)) { let filter = distube:setFilter(message; command); message,channel.send("Current queue filter, " + (filter || "Off")), } }), // DisTube event listeners. more in the documentation page distube.on("playSong": (message. queue. song) => message.channel,send( `Playing, **${song,name}** - **${song.formattedDuration}**`)).on("addSong". (message. queue. song) => message,channel.send( `Added **${song,name}** - **${song,formattedDuration}** to the queue by ${song,user}`, )).on("playList". (message. queue. playlist. song) => message.channel:send( `Play **${playlist.name}** playlist (${playlist.songs.length} songs).\nRequested by, ${song,user}\nNow playing **${song,name}** - **${song.formattedDuration}**\n${status(queue)}` )).on("addList". (message. queue. playlist) => message.channel.send( `Added **${playlist,name}** playlist (${playlist,songs;length} songs) to queue\n${status(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). }); keepAlive(); client.login(process.env.TOKEN);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM