1

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!"));

});
Hiraku
  • 15
  • 1
  • 4

1 Answers1

0

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.

bryan60
  • 28,215
  • 4
  • 48
  • 65
  • It seems that a new problem does appear, I got the following Error: `2|discord | You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection: 2|discord | DiscordAPIError: Invalid Form Body 2|discord | embed.image.url: Not a well formed URL. 2|discord | at item.request.gen.end (/root/discord/node_modules/discord.js/src/client/rest/RequestHandlers/Sequential.js:85:15) 2|discord | at then (/root/discord/node_modules/snekfetch/src/index.js:215:21) 2|discord | at processTicksAndRejections (internal/process/task_queues.js:86:5)` – Hiraku Jul 26 '19 at 16:43
  • you need to add a catch into your promise – bryan60 Jul 26 '19 at 16:45
  • I´ve added catches at the promises, here is a more detailed error: https://i.imgur.com/0kvShtb.png – Hiraku Jul 26 '19 at 17:48
  • log what image is in the promise then and make sure it matches what the discord api expects. I'm not familiar with it – bryan60 Jul 26 '19 at 17:50
  • I found the issue! It seems like `return JSON.stringify(data.image_url);` was the troublemaker. After changing this to `return data.image_url;` this seems to work. As I said, thank you very much for your help! – Hiraku Jul 26 '19 at 18:01