Discord.js v13,@discordjs/voice 播放音乐命令
Posted
技术标签:
【中文标题】Discord.js v13,@discordjs/voice 播放音乐命令【英文标题】:Discord.js v13, @discordjs/voice Play Music Command 【发布时间】:2021-12-04 17:29:44 【问题描述】:这是我的代码, 该命令执行完美,机器人加入语音通道,还发送了即将播放的歌曲名称,但它没有播放语音通道中的歌曲。 这是我第一次在 *** 上提出问题,所以不要介意格式和内容。但我在这里真的需要帮助。 Discord v13 和最新的节点模块。
const ytsearch = require('yt-search');
const Discord = require('discord.js')
const
joinVoiceChannel,
createAudioPlayer,
createAudioResource,
NoSubscriberBehavior
= require('@discordjs/voice');
module.exports =
name: "play",
description: "test command",
async run(client, message, args)
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) return message.channel.send('Please connect to a voice channel!');
if (!args.length) return message.channel.send('Please Provide Something To Play!')
const connection = await joinVoiceChannel(
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator
);
const videoFinder = async (query) =>
const videoResult = await ytsearch(query);
return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;
const video = await videoFinder(args.join(' '));
if (video)
const stream = ytdl(video.url, filter: 'audioonly' );
const player = createAudioPlayer();
const resource = createAudioResource(stream)
await player.play(resource);
connection.subscribe(player);
await message.reply(`:thumbsup: Now Playing ***$video.title***`)
else
message.channel.send('No video results found');
```
【问题讨论】:
【参考方案1】:我建议您查看@discordjs/voice 上的音乐机器人example。
他们在如何从 ytdl 中提取流方面做得很好。
我目前仍在学习这一切是如何工作的,但您要查看的部分是 createAudioResource 函数。
public createAudioResource(): Promise<AudioResource<Track>>
return new Promise((resolve, reject) =>
const process = ytdl(
this.url,
o: '-',
q: '',
f: 'bestaudio[ext=webm+acodec=opus+asr=48000]/bestaudio',
r: '100K',
,
stdio: ['ignore', 'pipe', 'ignore'] ,
);
if (!process.stdout)
reject(new Error('No stdout'));
return;
const stream = process.stdout;
const onError = (error: Error) =>
if (!process.killed) process.kill();
stream.resume();
reject(error);
;
process
.once('spawn', () =>
demuxProbe(stream)
.then((probe) => resolve(createAudioResource(probe.stream, metadata: this, inputType: probe.type )))
.catch(onError);
)
.catch(onError);
);
【讨论】:
以上是关于Discord.js v13,@discordjs/voice 播放音乐命令的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 discord.js 调用 SubCommand 的选项? (v13)