問題描述
我知道很多人已經問過如何在 discord 語音頻道中播放來自 youtube 的音樂,但我找不到任何關于在 djs 版本 13.2.0 上播放本地文件的信息!我嘗試使用此代碼:
I know a lot of people already asked how to play music from youtube in discord voice channel, but I can't find anything about playing local files on djs version 13.2.0! I tried using this code:
const { createReadStream } = require('fs');
const { join } = require('path');
const { createAudioResource, StreamType, createAudioPlayer, joinVoiceChannel } = require('@discordjs/voice');
joinVoiceChannel({
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator
});
message.guild.me.voice.setRequestToSpeak(true);
let resource = createAudioResource(join(../music/audio.mp3, 'audio.mp3'));
const player = createAudioPlayer();
player.play(resource);
當我嘗試 eval()
時 - 我的機器人加入頻道(舞臺頻道)并說一切正常,但它沒有播放任何東西!如何讓我的機器人在舞臺頻道播放本地音樂文件?
When I try to eval()
it - my bot joins the channel (stage channel) and says everything worked, but it's not playing anything! How can I make my bot play local music files in stage channel?
推薦答案
這里有2個問題.
首先,路徑完全錯誤.它不是字符串,即使您嘗試將其更改為字符串,它也會無效,因為第一個參數以 audio.mp3
結尾,第二個參數是 audio.mp3代碼>.請改用此路徑:
Firstly, the path is completely wrong. It is not a string and even if you try to change it to a string it will be invalid as the first argument ends with audio.mp3
, and the second one is audio.mp3
. Use this path instead:
let resource = createAudioResource(join('..', 'music', 'audio.mp3'));
其次,您在播放器中播放音頻,而不是語音連接.您必須訂閱音頻播放器.
Secondly, you are playing audio in the player, but not the voice connection. You must subscribe to the audio player.
這應該是最終代碼:
const player = createAudioPlayer()
joinVoiceChannel({
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator
}).subscribe(player)
message.guild.me.voice.setRequestToSpeak(true);
let resource = createAudioResource(join('..', 'music', 'audio.mp3'));
player.play(resource)
這篇關于使用 djs v13 播放本地音樂文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!