Closed AxeenFR closed 6 years ago
You could always do something with promises, however if you'd like a smaller albeit hacky-er way of avoiding this problem you can always use setTimeout before it attempts the message in order to ensure weather.js has finished gathering data. Could be done like this:
weatherjs.find({search:argument, degreeType: 'C', lang: 'FR'}, function (err, res) {
var result = res[0].location.name + ' : La température est actuellement de ' + res[0].current.temperature + res[0].location.degreetype
+ '\nLe ciel est ' + res[0].current.skytext;
setTimeout(()=>{
bot.sendMessage({
to: channelID,
message: result
});
},1000); //this will force the script to wait 1000ms (1 second) before attempting to send the results
console.log(result);
});
Once again this certainly isn't the best method, and I'd encourage looking into promises.
I'm not sure if @FileStream is on the right path; there's no reason weather.js shouldn't be finished gathering data since the message is being sent in its callback and the console.log apparently shows the correct value. Even if res
was undefined (which it isn't, since its children and properties are being accessed in the first line of the callback), result
would still be a valid string to send in a message.
Are you sure that channelID
is a valid ID string, and that it's a channel the bot has permission to send messages to?
@FileStream Thanks but it's not the solution, because in Debug Mode, my "result" variable is set with the message. It's just the bot.sendMessage who's not fired correctly.
@vegeta897 Yes, i'm sure, with my others commands my Bot send me messages correctly. It seems it's just because bot.sendMessage can"t post a message during async procedure... And I've test it on my server and in private message too.
I can assure you that sendMessage
works in callbacks. Any command you have is already in an .on('message'
callback to begin with.
Without seeing your full code it's hard to tell what else might be happening. Just in-case result
isn't a valid string, try changing it to 'foo'
or something to make sure that's not the issue. You should also be sure that channelID
is what you think it is (maybe it's accidentally being reassigned somewhere else in your code?), or maybe you could change it to your user ID to see if the bot PMs you.
@vegeta897 Ok so... How to say that... I'm just a dumb developer xD
At the top of each exports.run() (Because each command is in a specific file), I pass in parameters bot, user, channelID, argument... Aaaaand I just reversed user and channelID, so it couldn't work at all xD
Sorry for this lost of time, it was just a big mistake from my part ^^"
So, I can close this issue ^^"
Hello, sorry for this question, I'm new on the discord.io package.
I would like to develop a Discord Bot who can send some informations, like weather, youtube links, etc.
The problem is I use weather-js and I must to call the weatherjs.find([...]) function who return me a callback with the result, but it seems it impossible to use bot.sendMessage into an async function.
There is my code :
The console.log(result) send me the right result from weather-js, but bot.sendMessage doesn't send any message on Discord (In Debug mode, I pass into, so it's still "fired")
If someone can help me with this problem, it will be very nice ^^
Thanks in advance, have a good day !