Sending private messages to user

后端 未结 8 1519
清歌不尽
清歌不尽 2020-12-14 18:39

I\'m using the discord.js library and node.js to create a Discord bot that facilitates poker. It is functional except the hands are shown to everyone, and I need to loop thr

相关标签:
8条回答
  • 2020-12-14 19:09

    Make the code say if (msg.content === ('trigger') msg.author.send('text')}

    0 讨论(0)
  • 2020-12-14 19:10

    In order for a bot to send a message, you need <client>.send() , the client is where the bot will send a message to(A channel, everywhere in the server, or a PM). Since you want the bot to PM a certain user, you can use message.author as your client. (you can replace author as mentioned user in a message or something, etc)

    Hence, the answer is: message.author.send("Your message here.")

    I recommend looking up the Discord.js documentation about a certain object's properties whenever you get stuck, you might find a particular function that may serve as your solution.

    0 讨论(0)
  • 2020-12-14 19:10

    If your looking to type up the message and then your bot will send it to the user, here is the code. It also has a role restriction on it :)

    case 'dm':
      mentiondm = message.mentions.users.first();
      message.channel.bulkDelete(1);
      if (!message.member.roles.cache.some(role => role.name === "Owner")) return message.channel.send('Beep Boing: This command is way too powerful for you to use!');
      if (mentiondm == null) return message.reply('Beep Boing: No user to send message to!');
      mentionMessage = message.content.slice(3);
      mentiondm.send(mentionMessage);
      console.log('Message Sent!')
      break;

    0 讨论(0)
  • 2020-12-14 19:10

    A simple way would be to do something like:

    const channel = client.channels.cache.get("CHANNEL_ID");
    channel.send("Hello");
    

    Channel Name

    const channel = client.channels.cache.get((channel) => channel.name == "CHANNEL_NAME");
    channel.send("Hello");
    

    Channel ID

    const channel = client.channels.cache.get((channel) => channel.id == "CHANNEL_ID");
    channel.send("Hello");
    
    0 讨论(0)
  • 2020-12-14 19:24

    To send a message to a user you first need a User instance representing the user you want to send the message to.


    Obtaining a User instance

    • You can obtain a User instance from a message the user sent by doing message.autor
    • You can obtain a User instance from a user id with client.fetchUser

    Once you got a user instance you can send the message with .send

    Examples

    client.on('message', (msg) => {
     if (!msg.author.bot) msg.author.send('ok ' + msg.author.id);
    });
    
    client.fetchUser('487904509670337509', false).then((user) => {
     user.send('heloo');
    });
    
    0 讨论(0)
  • 2020-12-14 19:27

    The above answers work fine too, but I've found you can usually just use message.author.send("blah blah") instead of message.author.sendMessage("blah blah").

    -EDIT- : This is because the sendMessage command is outdated as of v12 in Discord Js

    .send tends to work better for me in general than .sendMessage, which sometimes runs into problems. Hope that helps a teeny bit!

    0 讨论(0)
提交回复
热议问题