I'm creating a discord bot, and trying to structure a bit better.
message.channel.send(require('./commands/' + inputs[0] + '.js')(inputs));
This will require a file and send the returned message to the channel. This is example of a such file:
module.exports = commands => {
let returnM = '';
axios.get(some url).then(response => {
returnM = response.data.whatever;
}).catch(error => {
returnM = "API error.";
});
}
return returnM;
I get a "cannot display empty message", because the function hasn't returned anything yet, and the message sender is already trying to execute. How can make it so, it waits, till the response is pulled and processed?
EDIT: Got IT! Thanks for the help! Made the exported function async and the whole axios pull return await, and the handler, look like this:
require('./commands' + inputs[0] + '.js')(inputs).then(m => message.channel.send(m));