Skip to content Skip to sidebar Skip to footer

Discordjs - Cannot Send Messages To This User

I am making a discord.js bot for a server I am in, but after attempting to send a DM to a user (who was just kicked, to notify them of it,) the bot won't send it at all and it will

Solution 1:

You should not be using delays or timers to solve this issue. Asynchronous calls in Discord.js return Promises, you should use those promises.

Promises are something that can basically turn your asynchronous workflow synchronous: when something happens -> do this.

This is the workflow:

  1. Send a message to a GuildMember
  2. Kick after the message is properly sent

Don't worry, they won't have time to react to the message as these actions will most likely happen instantaneously, thanks to Promises. There are no additional delays.

So basically:

GuildMember
  .createDM()
  .then((DMChannel) => {
    // We have now a channel ready.// Send the message.DMChannel
      .send(reason)
      .then(() => {
        // Message sent, time to kick.GuildMember.kick(reason);
      });
  });

Here's the referenced GuildMember

You can also catch, and you should. Catching happens when a Promise fails to execute the task it was assigned.

GuildMember
  .createDM()
  .then((DMChannel) => {
    // We have now a channel ready.// Send the message.DMChannel
      .send(reason)
      .then(() => {
        // Message sent, time to kick.GuildMember
          .kick(reason)
          .catch((e) => {
            console.log('Failed to kick!', e);
            // Do something else instead...
          });
      });
  });

(You can catch the message sending too, or creating the channel.)

Here you can learn more about Promises, which are an essential part of JavaScript: https://javascript.info/promise-basics

Solution 2:

Expanding from the comments: Maybe add a delay between the message and the actual kick?

Since your function is async, it's a good idea to write a small async delay helper:

constdelay = (msec) => newPromise((resolve) =>setTimeout(resolve, msec));

Now you can simply

userReslv.send(botembed);
await delay(100); // 100 msec = 0.1 seconds
memberKick.kick(reasonKick); 
// ...

Solution 3:

It looks like a lot of the functions you are using are asynchronous.

Specifically:

Additionally, you are trying to send to a GuildMember. I'm not super familiar with this API, but I think they may be considered not a GuildMember anymore after you kick in which case the reference may be stale. I think you should instead send directly to the User, after you kick.

Additionally, I don't think you are waiting enough in general. kick and send both return Promises which you should probably await to resolve, especially kick.

After you have that reference then use the send with the constructed message:

asyncfunctionkicker(bot, message, args) {
  /** @type GuildMember */const guildMemberToKick = message.mentions.members.first();

  /** @type User */const guildMemberUser = guildMemberToKick.user;

  /** @type RichEmbed */const kickRichMessage = newDiscord.RichEmbed;

  /** @type String */const kickReason = "You've been a baaaad person";

  // Build the rich kick message
  kickRichMessage.setDescription("❌ You Were Kicked From Roscord!");
  kickRichMessage.setColor("FF0000");
  kickRichMessage.setThumbnail(guildMemberUser.displayAvatarURL);
  kickRichMessage.addField("Reason:", kickReason);

  // Kick and waitawait guildMemberToKick.kick(kickReason);

  // Tell the user we kicked them and waitawait guildMemberUser.send(kickRichMessage);

  // Tell the channel we kicked and waitawait message.channel.send(`✅ ${guildMemberToKick.displayName} was kicked! ✅`);
}

Solution 4:

continuing AKX's response...

For anyone else struggling with this!

I put the const delay right above my var userReslv and I put the await delay(100) between my kick and message send. (There are probably many better ways to phrase that..)

Post a Comment for "Discordjs - Cannot Send Messages To This User"