简体   繁体   中英

discord.js DM command not direct messaging mentioned users

When I was making my discord bot, I came to another problem. Now I am making a command that should DM a person that is mentioned in bot commands channel. But I can't make it to work. I tried a lot of things but they don't seem to work. If someone can help me that help will be really appreciated Here is my code for a DM command:

module.exports = {
    name:'dm',
    description: 'dm mentioned user',
    execute(message, args){
        const user = message.mentions.users.first()

        user.send('test command');
        
        
        
    }
}

and this is my code in main file:

if(command === 'dm'){
        client.commands.get('dm').execute(message, args, Discord, client);
    }

Maybe you need to fetch the user first:

module.exports = {
  name: 'dm',
  description: 'dm mentioned user',
  async execute(message, args) {
    const user = await message.mentions.users.first().fetch();
    const channel = await user.createDM();

    channel.send('test command');
  },
};

2 possible reasons, my first guess is that you can only DM members, not users

const user = message.mentions.users.first()
user.send('test command');

My other guess is that the person you're trying to DM does not allow DMs from strangers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM