How can I make a bot that can DM someone?

I’ve tried to make a code myself but I just can’t figure it out. I can’t find anything online that works for Glitch. Does anyone know of a way to make a bot that can Dm what I want to someone using Glitch?

If it works then generally speaking it will work on Glitch.

Search: discord bot that can dm someone and there other people asking the question, answers to those messages and tutorial videos.

What part hasn’t been working? Maybe the person you are trying to DM isn’t in a guild that your bot is connected to? I doubt very much that you can just direct message any stranger on Discord.

Ok, thanks

So I went back and got the code. Can you tell me what is wrong with it? ```

bot.on('message', message => {
    if (message.content.startsWith('/dm ') && message.mentions.users.size) {
        var v=message.toString().split(' ').shift().shift().join(' ') // Takes the DM content from the message
        var member=message.mentions.users[0] // The mentioned user
        member.send(v) // send that user a DM

I can but you may not like to hear it. :slight_smile: First what is “it doesn’t work” mean? Do you get an error or simply there is no dm?

Checking if the message starts with ‘/dm ’ not a great way to do that because the prefix is hard coded but okay it will work. You then check for a non-zero users.size. I assume this is “were there any user mentions”? The split(’ ').shift().shift() assumes a lot I think. The message segments might not all be there.

So what I do any time I do anything this unusual is to console.log() the parts as I go. So first the message.content so you can see whether it starts with ‘/dm’ which we know it will but confirm it. Have it log users.size as well and see what you get when you include a user and don’t include a user. Log the variable v. to see if it looks like what you intended. And finally log member and see if it looks fully formed. Some data isn’t fetched without you explicitly fetching it but I believe that only happens if you are using partials.

But if you want to see if the basics work then test if it starts with '/dm ’ and hard code the other values. Instantiate a member and send “hello world” that way all the parsing is eliminated as being the culprit.

And my recommendation is to avoid the var keyword. You can use let but you may as well use const in these cases. And if you haven’t already set yourself up a second Discord account id for testing the code.

as mentions will be a map, you cannot do message.mentions.users[0], what you must do is message.mentions.users.first()

I went ahead and wrote a version to test it. I really don’t have a use for it now but who knows. As you mentioned one gets the first member from the mentions of the message. From that one derives the user which can be sent the message.

const reply = ‘parse this part as needed’;

const targetMember = message.mentions.members.first();

Oh I just saw from your message I can get the user directly, let me give that try.

A bit of follow up might be useful for others in the future. I used users.first() and it worked as expected. A few other things to note however.

.first() is not defined as returning the first user mentioned if more than one is mentioned they are returned in whatever order the API decides. For my purposes I control the message so there won’t be more than one mention.

There is a property to that tests for @everyone being in the mention. I assume sending a dm to everyone would make a mess so add code to trap for that.

The “message” could contain multiple words which would typically be parsed when slice is used to extract the parameters. Joining the elements again will handle multiple word messages (if they make up the last parameter of the command).

And finally if you don’t already use it try logging JSON.stringify(message.mentions)) and other objects when you want to inspect them. It eliminates a lot of guessing.

So. --dm @ this message here
would dm “this message here” to the @ account

and --dm @ this wouldn’t
will not dm anyone anything

the property is message.mentions.everyone BTW

So, by “it doesn’t work” I mean it says “Parsing Error: Unexpected Token”

You’ve read the other replies correct? We have just about rewritten it for you. Plus I pointed out what can go wrong. See above, “users is a map” not an array.