William
William

Reputation: 1175

Sending a DM through a command, to a specific person from a database, discord.js-commando

So, I having an issue of sending a DM to a specific person without an author tag, and without a mention of the person. I tried duplicating the mention array:

/*jshint esversion: 6*/
const commando = require('discord.js-commando');

class Msgowner extends commando.Command {
  constructor(client) {
    super(client, {
      name: 'msgowner',
      group: 'info',
      memberName: 'msgowner',
      description: 'Gives rules on mock or legit duels.',
      examples: ['ladderrules type'],
    });
  }

  async run(message) {
    var user = {
      id: '12345',
      username: 'Bloodmorphed',
      discriminator: '12345',
      avatar: 'd510ca3d384a25b55d5ce7f4c259b2d0',
      bot: false,
      lastMessageID: null,
      lastMessage: null,
    };
    user.send('Test');
  }
}

module.exports = Msgowner;

There is a reason why I need to send DMs this way, but I can't seem to figure out how. (The error it gives now is a unknown function). Also replacing id and discriminator with generic numbers, but they are correct in my code.

Upvotes: 1

Views: 7526

Answers (2)

Blundering Philosopher
Blundering Philosopher

Reputation: 6805

Try something like this - get the member you're looking for using message.channel.members.find() method:

async run(message) {
    // get Collection of members in channel
    let members = message.channel.members;

    // find specific member in collection - enter user's id in place of '<id number>'
    let guildMember = members.find('id', '<id number>');

    // send Direct Message to member
    guildMember.send('test message');
}

Edit: It looks like it's also possible to find users outside the current channel by doing something like this:

async run(message) {
    // get client from message's channel
    let client = message.channel.client;

    // fetch user via given user id
    let user = client.fetchUser('<id number>')
    .then(user => {
        // once promise returns with user, send user a DM
        user.send('Test message'); 
    });
}

Upvotes: 1

William
William

Reputation: 1175

Okay, found my answer:

  async run(message) {
    var user = {
      id: '12345,
      username: 'Bloodmorphed',
      discriminator: '12345',
      avatar: 'd510ca3d384a25b55d5ce7f4c259b2d0',
      bot: false,
      lastMessageID: null,
      lastMessage: null,
    };
    console.log(user);
    message.member.user.send('Test');
  }
}

module.exports = Msgowner;

EDIT: This was NOT the answer, still looking for one.

Upvotes: 0

Related Questions