如何在Discord JS中循环声音



Discord JS更新到第13版,整个语音库都发生了变化。由于该版本相对较新,代码示例很少。我不知道如何正确地循环播放一个声音。在DiscordJS 13中如何做到这一点?

试试下面的代码:

const { Client, Intents } = require('discord.js')
const DiscordStream = require('@discordjs/voice')
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.DIRECT_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES]})
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`)
})
client.on('messageCreate', message => {
if (message.content === '/play') {
const voicechannel = message.member.voice.channel
const connection = DiscordStream.joinVoiceChannel({
channelId: voicechannel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator,
})
const player = DiscordStream.createAudioPlayer()
connection.subscribe(player)

// player.play(DiscordStream.createAudioResource(Ytstream(url, {filter : 'audioonly'})))
player.play(DiscordStream.createAudioResource('test.opus'))
player.on(DiscordStream.AudioPlayerStatus.Idle, () => {
player.play(DiscordStream.createAudioResource('test.opus'))
// player.play(DiscordStream.createAudioResource(Ytstream(url, {filter : 'audioonly'})))
})
}
})
client.login('your-token here')

连接到音频通道并使用'/play'命令。

npm list
roger@1.0.0 /root/dev/Roger
├── @discordjs/opus@0.6.0
├── @discordjs/voice@0.6.0
├── discord.js@13.2.0
├── sodium@3.0.2

我相信您正在寻找这个文档。https://discordjs.github.io/voice/他们把它分成自己独立的模块。

最新更新