【发布时间】:2022-01-05 15:25:29
【问题描述】:
在我使用旧功能之前,我的机器人无法连接到语音通道,但我将其更改为新功能,它仍然无法正常工作。
我尝试将它放在If(video),但它不起作用。
代码:
const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');
module.exports = {
name: 'play',
description: '',
async execute(message, args){
const voiceChannel = message.member.voice.channel;
const { joinVoiceChannel } = require('@discordjs/voice');
if (!voiceChannel) return message.channel.send('You need to be in a voice channel');
const permissions = voiceChannel.permissionsFor(message.client.user);
if (!permissions.has('CONNECT')) return message.channel.send('You dont have a permission to do that');
if (!permissions.has('SPEAK')) return message.channel.send('You dont have a permission to do that');
if (!args.length) return message.channel.send('You have to add a link');
const connection = 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'});
connection.play(stream, {seek: 0, volume: 1})
.on('finish', () =>{
voiceChannel.leave();
})
await message.reply(`Now playing: ***${video.title}***`)
} else {
message.channel.send('No video redults found');
}
}
}
【问题讨论】:
标签: node.js discord.js