问题是当歌曲添加到队列时,机器人会多次响应。第一次运行命令时,它只响应一次,当该事件第二次发生时,它将响应两次。
示例:*play Gabibbo Mashup 播放:Gabibbo Mashup - 00:26。第二次:*播放 Gabibbo Mashup 播放:Gabibbo Mashup - 00:26 播放:Gabibbo Mashup - 00:26)
代码:
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);