0
votes

I am working on a Discord bot and need to return a value after the reaction promise finishes.

function sendDM(userTag) {
  let sentMessage = undefined;
  let moderator = undefined;
  let status = "";

  try {
    client.users.fetch(userTag, false).then((user) => {
        user.send("User Authentication Request -\n```Discord Tag:" + userTag + "\n" + "Date Sent: " + getDate() + "\n" + "```" + "Authorize?").then((message => {
            moderator = user
            sentMessage = message
            message.react("✅");
            message.react("❌");
        }));
    })

    client.on("messageReactionAdd", (reaction, user) => {
      if (reaction.emoji.name == "✅") {
        const authorTag = client.users.cache.find(u => u.tag === moderator.tag);
        
        if (user.id  == authorTag) {
          status = "complete";
          console.log("Auth Request Complete");
          moderator.send("Authentication request was successful.");
        }
      } else {
        if (reaction.emoji.name == "❌") {
          const authorTag = client.users.cache.find(u => u.tag === moderator.tag);
        
          if (user.id  == authorTag) {
            status = "dropped";
            console.log("Auth Request Dropped: User Denied");
            moderator.send("Authentication request was dropped.");
          }
        }
      }
    }).then(someShit => console.log("test: " + someShit))
  } catch(error) {
    console.log("DM ERROR_" + error);
  }

// Return would be here
}

The problem is once the by the time the user reacts, the function has already returned the empty "status" string.

2

2 Answers

0
votes

I'm not an expert in the field, but it looks like you could wrap your code block in a Promise and wait until the promise has been resolved by your code. Something like this might work:

async function sendDM(userTag) {
  let sentMessage = undefined;
  let moderator = undefined;
  let status = "";

  const statusPromise = new Promise((resolve, reject) => {
    try {
      client.users.fetch(userTag, false).then((user) => {
        user
          .send(
            "User Authentication Request -\n```Discord Tag:" +
              userTag +
              "\n" +
              "Date Sent: " +
              getDate() +
              "\n" +
              "```" +
              "Authorize?"
          )
          .then((message) => {
            moderator = user;
            sentMessage = message;
            message.react("✅");
            message.react("❌");
          });
      });

      client
        .on("messageReactionAdd", (reaction, user) => {
          if (reaction.emoji.name == "✅") {
            const authorTag = client.users.cache.find(
              (u) => u.tag === moderator.tag
            );

            if (user.id == authorTag) {
              resolve("complete");
              console.log("Auth Request Complete");
              moderator.send("Authentication request was successful.");
            }
          } else {
            if (reaction.emoji.name == "❌") {
              const authorTag = client.users.cache.find(
                (u) => u.tag === moderator.tag
              );

              if (user.id == authorTag) {
                resolve("dropped");
                console.log("Auth Request Dropped: User Denied");
                moderator.send("Authentication request was dropped.");
              }
            }
          }
        })
        .then((someShit) => console.log("test: " + someShit));
    } catch (error) {
      console.log("DM ERROR_" + error);
      reject(error);
    }
  });

  status = await statusPromise;
  // Return would be here
}

This might introduce problems elsewhere in your code since you have to make your function async to use the await keyword, but this way your code should wait until a value has been returned from the promise.

0
votes

Use async/await to halt the execution of the function until the promise has resolved:

async function sendDM(userTag) {
  ...
  await client.on("messageReactionAdd", (reaction, user) => {
  ...
  // Promise has resolved so the data is available
}