javascript - 机器人不使用 !play 命令播放声音
问题描述
我的机器人无法在 vc 中播放声音时遇到问题。
即使我将音量调到最大并且机器人的圆圈以绿色亮起,也没有声音响起。
我的代码没有错误,有人可以帮我解决这个问题吗?
我对 ytdl 和 yt 搜索有点陌生,因为我以前也从未尝试过使用它,所以如果你能向我解释一下就好了。
这是我的代码:
const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');
module.exports = {
name: 'play',
description: 'Joins and plays a video from youtube',
async execute(client, message, args) {
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) return message.channel.send('You need to be in a voice channel to execute this command!');
if (!args.length) return message.channel.send('please enter a youtube link or some keywords');
const validURL = (str) => {
var regex = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/;
if (!regex.test(str)) {
return false;
} else {
return true;
}
}
if (validURL(args[0])) {
const connection = await voiceChannel.join();
const stream = ytdl(args[0], { filter: 'audioonly' });
connection.play(stream, { seek: 0, volume: 10 })
.on('finish', () => {
voiceChannel.leave();
message.channel.send('leaving channel');
});
await message.reply(`:thumbsup: Now Playing ***Your Link!***`)
return
}
const connection = await voiceChannel.join();
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(`:thumbsup: Now Playing ***${video.title}***`)
} else {
message.channel.send('No video results found');
}
}
}
解决方案
推荐阅读
- javascript - 如何在单独的 HTML 网站上重用这个响应式导航栏
- python - 如何使postgresql中的每一列分别唯一?
- r - 无法再安装任何 R 包
- android - 在 android MediaPlayer 中播放来自 Firebase 存储的音频文件
- android - 如何在 android Oreo 中自动启用 GPS?
- android - 设备不适用于“main.dart”配置,
- javascript - 如何编写正则表达式不允许开头和结尾有空格,但单词之间需要空格
- java - 有没有办法从矩阵中以数组的形式访问列的引用?
- aws-lambda - 代码管道 lambda 操作永远不会完成
- c# - 即使在 c# 中事件完成后也可以存储和保留变量的值