javascript - await 仅在异步函数 discord.js 中有效
问题描述
我正在为我的服务器制作音乐播放机器人,但我认为我做错了什么。我正在做那个视频,但我收到await is only valid in async function
错误
module.exports = (msg) => {
const ytdl = require('ytdl-core');
if (!msg.member.voiceChannel) return msg.channel.send('Please Connect to a voice channel');
if (msg.guild.me.voiceChannel) return msg.channel.send('Im in another channel');
if(!args[1]) return msg.channel.send('no URL no music');
let validate = await ytdl.validateURL(args[1]);
if(!validate) return msg.channel.send('Please input a valid url following the command');
let info = await ytdl.getInfo(args[1]);
let connection = await msg.member.voiceChannel.join();
let dispatcher = await connection.play(ytdl(args[1], {filet: 'audioonly'}));
msg.channel.send(`Now playing : ${info.title}`);
}
解决方案
错误说明了一切,您使用的函数await
需要是异步的(async
)
module.exports = async (msg) => {
const ytdl = require('ytdl-core');
if (!msg.member.voiceChannel) return msg.channel.send('Please Connect to a voice channel');
if (msg.guild.me.voiceChannel) return msg.channel.send('Im in another channel');
if(!args[1]) return msg.channel.send('no URL no music');
let validate = await ytdl.validateURL(args[1]);
if(!validate) return msg.channel.send('Please input a valid url following the command');
let info = await ytdl.getInfo(args[1]);
let connection = await msg.member.voiceChannel.join();
let dispatcher = await connection.play(ytdl(args[1], {filet: 'audioonly'}));
msg.channel.send(`Now playing : ${info.title}`);
}
推荐阅读
- javascript - 如何在 Jquerydatatable 中获取大数据
- find - find(1) 列出文件的顺序是什么?
- amazon-web-services - Dynamo 数据库读/写
- qt - QML ComboBox 弹出问题
- c - 如何从 apache Web 服务器输入过滤器返回 HTTP 403?
- android - 导航抽屉项目不可点击
- ldap - user_filter vs base 限制对 LDAP 用户的访问
- spring-mvc - 每个用户租户使用不同的时区进行杰克逊序列化
- c# - C# DGV - 显示实时 MYSQL DB 表
- android - 使用张量流和机器学习套件通过声音识别人