【发布时间】:2021-10-25 07:51:49
【问题描述】:
const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');
const { joinVoiceChannel } = require('@discordjs/voice');
const { getVoiceConnection } = require('@discordjs/voice');
const { createAudioPlayer} = require('@discordjs/voice');
const{createAudioResource} = require('@discordjs/voice');
const{NoSubscriberBehavior } = require('@discordjs/voice');
module.exports = {
name: 'play',
description: 'Joins and plays a video from youtube',
execute: async (client, msg, arg, Discord) => {
const voiceChannel = msg.member.voice.channel;
if (!voiceChannel) return msg.channel.send('You need to be in a channel to execute this command!');
const permissions = voiceChannel.permissionsFor(msg.client.user);
if (!permissions.has('CONNECT')) return msg.channel.send('You dont have the correct permissins');
if (!permissions.has('SPEAK')) return msg.channel.send('You dont have the correct permissins');
if (!arg.length) return msg.channel.send('You need to send the second argument!');
// const validURL = (str) =>{
// var regex = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/;
// return false;
// } else {
// return true;
// }
// }
// if(validURL(arg[0])){
// const connection = await voiceChannel.join();
// const stream = ytdl(arg[0], {filter: 'audioonly'});
// connection.play(stream, {seek: 0, volume: 1})
// .on('finish', () =>{
// voiceChannel.leave();
// msg.channel.send('leaving channel');
// });
// await msg.reply(`:thumbsup: Now Playing ***Your Link!***`)
// return
// }
const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: voiceChannel.guild.id,
adapterCreator: msg.channel.guild.voiceAdapterCreator,
});
const connection1 = getVoiceConnection(voiceChannel.guild.id);
const videoFinder = async (query) => {
const videoResult = await ytSearch(query);
return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;
}
const keywords = await arg.join(' ')
const video = await videoFinder(keywords);
if (video) {
const stream = ytdl(video.url, { filter: 'audioonly' });
const subscription = connection1.subscribe(stream,{seek:0,volume:1});
await msg.reply(`:thumbsup: Now Playing ***${video.title}***`)
}
else{
msg.reply('No Results Found')
}
}
}
现在的问题是消息来了,它正在播放音频,但没有人能听到它 我想可能是由于新的 Discord.js v13 我没有收到任何错误,但有人可以帮助我
同样在 v12 中它工作正常,由于新的 v13,我不得不更改我的代码 我没有从文档中得到任何帮助,但现在我希望来自 Stack
提前致谢!
【问题讨论】:
-
但是音乐正在播放,但没有广播到语音频道。根据documentation,您还需要
Player。您已包含createAudioResource但从未使用过它,您需要它来准备音频流。 -
我对代码做了一些修改,但还是不行你可以参考这个链接srcb.in/6OzLvz4IGp
标签: javascript node.js discord.js