1

I am using node.js for discord.

After I make a command, I want my bot to send a direct/private message to a specific person, not the author who makes the command (me).

Right now I have the person's <@000000000000000000> (I think this is called an ID), which is in String format.

For instance, this code client.sendMessage(message.author, "Hello!"); sends the author the message Hello. But I want one like client.sendMessage(message.user("<@000000000000000000>"), "Hello!");

Does a function like that exist?

For background information, I'm making a werewolf game bot where players are randomly assigned a role, and after I command w!play I want the players to receive their roles in the DM.

ricola
  • 61
  • 2
  • 9

2 Answers2

2

Yes just get the user object and send to that. You will need their id, so parse out the id part of the string "<@0000>". Also, sendMessage is deprecated. Use channel.send(). In the case of a user:

let str = "<@123456789>"; //Just assuming some random tag.

//removing any sign of < @ ! >... 
//the exclamation symbol comes if the user has a nickname on the server.
let id = str.replace(/[<@!>]/g, '');

client.fetchUser(id)
    .then(user => {user.send("Hello I dmed you!")})
Wright
  • 3,370
  • 10
  • 27
1

I would not write it that way unless you have a reason for doing so specifically.

I have used webhooks with git for discord, used hashtags to communicate on a private channel, (and create dm channels) thus I can add in rules for deletion/exclusions (for admins or otherwise)

(This wouldn't be applicable to Facebook if you need Facebook integration)

kbrackson
  • 7
  • 3
  • (you don't need a working knowledge of python or php, you can write webhooks with node in this case) and thus I would write users with an ID vs a string, u don't *need* a bool though obviously that's up to you, this method is easier for your brain later because you can specify if it's a bot or a person and find any hashtag (channel), bot, or user later. – kbrackson Aug 05 '17 at 18:51
  • So I need to use webhooks? Can I have a sample code of a hashtag? – ricola Aug 06 '17 at 02:24
  • This article describes the method best and gives you a rundown on its use. To me it's just simpler to use than the discord string format and I think it could come in useful when it comes to deprecation and upgrades. The only reason I know about it is because I wanted to integrate slack in and this was how I went about it. https://support.discordapp.com/hc/en-us/articles/228383668-Intro-to-Webhooks **Note that you can integrate webhooks in discord without git though this tutorial wants you to do so. I wouldn't say either way is better or worse, both have benefits and drawbacks of course. :) – kbrackson Aug 06 '17 at 02:34