Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

javascript - await only valid with async function urban dictionary api discord js

Any idea how i can fix this problem? I tried putting an async function in and I couldn't get it to work. I'm not quite sure on how or where to add the async function, and how to get the two to work together. Any help is much appreciated, and this is for a discord javascript bot.

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);
}
});
question from:https://stackoverflow.com/questions/65920988/await-only-valid-with-async-function-urban-dictionary-api-discord-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In order to run your await command, you have to put async at the top of your code. For more info about how await and async work, look at the link provided by 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);
}
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...