Home > other >  msg.author.dmChannel does not exist
msg.author.dmChannel does not exist

Time:10-30

so i need to get msg.author.dmChannel
it doesnt work
the msg.author object is the following:

User {
  id: '0000000000000000',
  bot: false,
  system: false,
  flags: UserFlags { bitfield: 0 },
  username: 'username'   ,
  discriminator: '0000',
  avatar: '8eb21315f800000000000000000000008a78ab77e',
  banner: undefined,
  accentColor: undefined
}

so there really is no dmChannel? how do i make one? msg.author.createDM() doesnt work
EDIT: msg.author.send('What is the password?' '\n If this is not you or you did not use the confim-subscription command please disregard this message. You have 1 minute'); has already been used

    msg.author.send('What is the password?'   '\n If this is not you or you did not use the `confim-subscription` command please disregard this message. You have 1 minute');
    console.log(msg.author);
    let channel = msg.author.dmChannel;
    console.log(channel);
      channel.awaitMessages(m => m.author.id == msg.author.id, {
          max: 1,
          time: 60000,
          errors: ['time']
        })
        .then(dm => {
          dm = dm.first()
          if (dm.content == password || dm.content.toUpperCase() == 'Y') {
            dm.channel.send(`Your respone has been processed :thumbsup:`);
            var role = message.guild.roles.find(role => role.name === "Member");
            msg.guild.members.cache.get(msg.author.id).roles.add(role)
          } else {
            dm.channel.send(`Terminated: Invalid Response`)
          }
        })

CodePudding user response:

In v13, DMs need their own intent (DIRECT_MESSAGES). Also, User.dmChannel can be null. To force a DM, you can use User.createDM which returns the channel. Here is what could be the final code:

let channel = await msg.author.createDM();
channel.send('What is the password?'   '\n If this is not you or you did not use the `confim-subscription` command please disregard this message. You have 1 minute');
      channel.awaitMessages(m => m.author.id == msg.author.id, {
          max: 1,
          time: 60000,
          errors: ['time']
        })
        .then(dm => {
          dm = dm.first()
          if (dm.content == password || dm.content.toUpperCase() == 'Y') {
            dm.channel.send(`Your respone has been processed :thumbsup:`);
            var role = message.guild.roles.find(role => role.name === "Member");
            msg.guild.members.cache.get(msg.author.id).roles.add(role)
          } else {
            dm.channel.send(`Terminated: Invalid Response`)
          }
        })

Note your client does need the DM intents. You can provide it like this:

const client = new Client({
    intents: [
        Intents.FLAGS.DIRECT_MESSAGES //you can add other intents as needed
    ]
})

CodePudding user response:

Here's an example of how this is implemented.

// first we check if the msg was sent by a bot, if that's not the case
// then we proceeded to send a message to the author.
if (!msg.author.bot) msg.author.send('ok '   msg.author.id);
  • Related