简体   繁体   English

Discord 机器人多次响应事件

[英]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 .示例:*play Gabibbo Mashup 播放: Gabibbo Mashup - 00:26 On the second time: *play Gabibbo Mashup Playing: Gabibbo Mashup - 00:26 Playing: Gabibbo Mashup - 00:26 )第二次:*播放 Gabibbo Mashup 播放: Gabibbo Mashup - 00:26播放: 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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