[英]await only valid with async function urban dictionary api discord js
知道如何解决这个问题吗? 我尝试将异步 function 放入,但无法正常工作。 我不太确定如何或在何处添加异步 function,以及如何让两者一起工作。 非常感谢任何帮助,这是针对 discord javascript 机器人的。
client.on('message', message => {
if (message.content.startsWith(`${PREFIX}urban`)) {
if (!args.length) {
return message.channel.send('You need to supply a search term!');
}
const query = querystring.stringify({ term: args.join(' ') });
const { list } = await fetch(`https://api.urbandictionary.com/v0/define?${query}`).then(response => response.json());
if (!list.length) {
return message.channel.send(`No results found for **${args.join(' ')}**.`);
}
const [answer] = list;
const embed = new Discord.MessageEmbed()
.setColor('#EFFF00')
.setTitle(answer.word)
.setURL(answer.permalink)
.addFields(
{ name: 'Definition', value: trim(answer.definition, 1024) },
{ name: 'Example', value: trim(answer.example, 1024) },
{ name: 'Rating', value: `${answer.thumbs_up} thumbs up. ${answer.thumbs_down} thumbs down.` },
);
message.channel.send(embed);
}
});
为了运行您的await
命令,您必须将async
放在代码的顶部。 有关await
和async
如何工作的更多信息,请查看VLAZ 提供的链接。
//put the async right before the second message, that's it
client.on('message', async message => {
if (message.content.startsWith(`${PREFIX}urban`)) {
if (!args.length) {
return message.channel.send('You need to supply a search term!');
}
const query = querystring.stringify({ term: args.join(' ') });
const { list } = await fetch(`https://api.urbandictionary.com/v0/define?${query}`).then(response => response.json());
if (!list.length) {
return message.channel.send(`No results found for **${args.join(' ')}**.`);
}
const [answer] = list;
const embed = new Discord.MessageEmbed()
.setColor('#EFFF00')
.setTitle(answer.word)
.setURL(answer.permalink)
.addFields(
{ name: 'Definition', value: trim(answer.definition, 1024) },
{ name: 'Example', value: trim(answer.example, 1024) },
{ name: 'Rating', value: `${answer.thumbs_up} thumbs up. ${answer.thumbs_down} thumbs down.` },
);
message.channel.send(embed);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.