Skip to content
Advertisement

Refactoring Embed message generation for Discord Bot

I am trying to refactor the code that I am using to generate embeds (user input makes an API call for info, which I display through the embed) with my bot in Discord. I have both a slash command and prefix command version of the code (so that people can either ask the bot with a prefix command or use the slash commands) and I am trying to pull the duplicated code for both those requests into a helper file.

The helper file: (/utils/itemHelper.js)

JavaScript

At this point, the console.log(itemEmbed) gives me what I want:

JavaScript

But by the time I call the function within the file were I create the slash command, I get an error message (which is discord telling me that the embed I want to send is lacking the description field):

JavaScript

The slash command file where I call the async function: (/commands/item.js)

JavaScript

The console.log output is itemEmbed:[object Promise]

My hunch is that I am somehow misunderstanding/misapplying how async functions and promises work (and that the information I need is hidden somewhere inside [object Promise]). I have tried to do ‘return itemEmbed’ at the end of the helper file, but that didn’t solve the issue. I have also considered if I might be missing a param in my async function, but I don’t think I need another param, or if I did need one what role it would fill.

Thanks for any/all help in advance!

Advertisement

Answer

In your getItem function, there’s two problems:

  1. You made it async. When a function is asynchronous, it is considered as a Promise that you have to use the await keyword on await getItem(..) So that it will wait for the fetch to finish to finally continue executing your code.
  2. You don’t return anything on your function. You log the embed in your console. Try using it like so :
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement