1
votes

So, I´ve tried to create a RichEmbed Message which should be executed, right after the User has left the Discord. In this RichEmbed should´ve been a random GIF from giphy, which I´ve created in the getGiphyPic() function using this node module: https://github.com/risan/giphy-random When this event gets executed, the embed is beeing sent without the .setImage(). I´ve tried to do console.log and it seems like the message is beeing sent, before the URL is successfully created.

I´ve already tried to make the event function async and await the creation of the image var, I´ve also tried to create a promise after the giphyRandom function, but that doesn´t seems to solve my problem.

const Discord = require('discord.js');
const { token, giphyToken } = require('./config.json');
const client = new Discord.Client();
const giphyRandom = require("giphy-random");
  function getGiphyPic() {
    (async () => {

        await giphyRandom(giphyToken, {
            tag: "fail",
            rating: "pg-13"
        }).then(resp => {
            let { data } = resp;

            console.log('Created GiphyURL: ' + JSON.stringify(data.image_url))
            return JSON.stringify(data.image_url);
        });
    })();
};
client.on('guildMemberRemove', async function (member) {

    var logChannel = client.channels.get('60370230389694091');
    if (!logChannel) return;
    var image = await getGiphyPic();
    var  embed = new Discord.RichEmbed()
        .setColor(0xc62828)
        .setAuthor('Someone has left the Server!', member.user.avatarURL);
        .setImage(image);
    logChannel.send(embed).then(message => console.log("Sent message!"));

});
1

1 Answers

0
votes

you could just use the promise supplied to you...

const Discord = require('discord.js');
const { token, giphyToken } = require('./config.json');
const client = new Discord.Client();
const giphyRandom = require("giphy-random");
  function getGiphyPic() {
    return giphyRandom(giphyToken, {
        tag: "fail",
        rating: "pg-13"
    }).then(resp => {
        let { data } = resp;
        console.log('Created GiphyURL: ' + JSON.stringify(data.image_url))
        return data.image_url; // give correct response
    });
};
client.on('guildMemberRemove', function (member) {

    var logChannel = client.channels.get('60370230389694091');
    if (!logChannel) return;
    getGiphyPic().then(image => {
      var  embed = new Discord.RichEmbed()
          .setColor(0xc62828)
          .setAuthor('Someone has left the Server!', member.user.avatarURL);
          .setImage(image);
      logChannel.send(embed).then(message => console.log("Sent message!"));
    });
});

async / await can be useful for complex flow control but seems unneeded here, if you want to use it, you're way overcomplicating it:

  function getGiphyPic() {
    return giphyRandom(giphyToken, {
            tag: "fail",
            rating: "pg-13"
        }).then(resp => {
            let { data } = resp;

            console.log('Created GiphyURL: ' + JSON.stringify(data.image_url))
            return data.image_url;
        });
  };

just return the promise and async / await will handle the rest.