问题描述
我正在尝试制作一个不和谐的音乐bot,到目前为止它可以正常运行,但是不能播放音乐。该机器人会获取视频等,但不会播放音乐。我安装了youtube-api和ytdl东西,还尝试安装opus,但是存在问题。它没有安装。
我尝试过:“ npm install node-opus”,但是随后出现了
错误:C:\Program Files (x86)\MSBuild\14.0\bin\MSBuild.exe Failed with exit code: 1
错误文本文件:https://cdn.discordapp.com/attachments/730078688669990965/754778841905627186/message.txt(我希望它能那样工作)
Opusscript已安装,但这还不够。有谁知道该如何解决?如果您需要机器人的代码,请看我的最后一个问题……没关系。这里的代码:
const ytdl = require('ytdl-core');
const YouTube = require('simple-youtube-api');
const youtube = new YouTube('API_KEY');
bot.on('message',async (msg) => {
if (msg.author.bot) return;
if (!msg.content.startsWith(prefix)) return;
const args = msg.content.substring(prefix.length).split(' ');
const searchString = args.slice(1).join();
const url = args[1] ? args[1].replace(/<(-_)>/g,'$1') : '';
if (msg.content.startsWith(`${prefix}play`)) {
const voiceChannel = msg.member.voice.channel;
if (!voiceChannel)
return msg.channel.send('You need to be in a voice channel.');
const permissions = voiceChannel.permissionsFor(msg.client.user);
if (!permissions.has('CONNECT'))
return msg.channel.send(
"I don't have permissions to connect to a voice channel. help."
);
if (!permissions.has('SPEAK'))
return msg.channel.send(
"I don't have permissions to speak in a voice channel"
);
try {
var video = await youtube.getVideoByID(url);
} catch {
try {
var videos = await youtube.searchVideos(searchString,1);
var video = await youtube.getVideoByID(videos[0].id);
} catch {
return msg.channel.send('I Could not find any search results.');
}
}
const song = {
id: video.id,title: discord.Util.escapeMarkdown(video.title),url: 'https://www.youtube.com/watch?v=' + video.id,};
try {
var connection = await voiceChannel.join();
} catch (error) {
return msg.channel.send(
`There was an error connecting to the voice channel ${error}`
);
}
const dispatcher = connection
.play(ytdl(video.id,{ filter: `audioonly` }))
.on('finish',() => {
voiceChannel.leave();
})
.on('error',(error) => {
msg.channel.send('There was an error: ' + error);
});
if (dispatcher) return msg.channel.send(`Video is playing: \n` + video.url);
dispatcher.setVolumeLogarithmic(5 / 5);
} else if (msg.content.startsWith(`${prefix}stop`)) {
if (!msg.member.voice.channel)
return msg.channel.send('You need to be in a voice channel to stop the bot');
msg.member.voice.channel.leave();
return undefined;
}
});
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)