如何停止播放机器人音乐?(Discord.js)



这段代码是用来播放音乐的,我想在我的机器人上有一个停止命令。你能帮我吗?

const Discord = require("discord.js");
const client = new Discord.Client();
const config = require("./config.json")
const ytdl = require("ytdl-core")
const streamOptions = {seek:0, volume:1}
client.on("message", async message => {
if(message.author.bot) return
if(message.channel.type === "dm") return
if(!message.content.startsWith(config.prefix)) return
const args = message.content.slice(config.prefix.length).trim().split(/ +/g)
const comando = args.shift().toLocaleLowerCase()
if(comando === "play") {
var voiceChannel = message.guild.channels.cache.get("733122593019789314")
let file = args[0]
if(voiceChannel == null) {
console.log("Canal não encontrado.")
}
if(voiceChannel != null) {
console.log("Canal encontrado.")
await voiceChannel.join().then(connection => {
const stream = ytdl(file, {filter:"audioonly", quality:"highestaudio"})
const DJ = connection.play(stream, streamOptions)
DJ.on("end", end => {
voiceChannel.leave()
})
}).catch(console.error)
}
}/* I was trying to do the command this way:
else if(comando === "stop") {
voiceChannel.leave()
}*/
}
client.login(config.token);

一切正常,我只想停止音乐。

(我是巴西人,有些单词或句子可能错了。)

谢谢你的帮助!

您可以使用StreamDispatcher.pause()和StreamDispatcher.resume().

StreamDispatcher被定义为DJ,因此您可以使用:

DJ.pause(); // Pauses the stream.
DJ.resume(); // Resumes the stream.
DJ.destroy(); // Ends the stream.

最新更新