我如何制作一个不和谐的 js 机器人,DM 是一个提到的用户?

时间:2021-05-12 18:10:22

标签: javascript node.js discord discord.js

我正在制作一个 Discord 机器人,让您可以使用“!dm”命令。我正在努力做到这一点,每当有人使用“!dm”命令,放置用户ID/提及用户,并放置一条消息时,它都会向提到的用户/用户ID发送消息。这是我的代码:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row">
   <div class="col-md-12"><strong>subject</strong></div>
   <div id="lessText" class="disp-cont col-md-auto">
      Lorem Ipsum is simply dummy text of the printing an
   </div>
   <div id="moreText" class="more-cont col-md-auto" style="display:none;">
      Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s
   </div>
   <a href="#" class="more col-md-auto">read more</a>
   <div class="col-md-12">no record</div>
</div>

2 个答案:

答案 0 :(得分:0)

集合 client.users.cache.get() 是由 id 映射的,所以它应该是 client.users.cache.get(taggedUser.id);。但是,一个更大的问题是,您将客户称为 bot 而不是 client,这就是 client.users.cache.get() 不起作用的原因。完整代码应如下所示:

bot.on('message', (message) => {
 if (message.content === '!dm') {
  const taggedUser = message.mentions.users.first();
  const user = bot.users.cache.get(taggedUser.id);
  user.send('test');
 }
});

确保你记住你对变量的称呼!

答案 1 :(得分:0)

const target = message.mentions.users.first()
if(!target) return message.reply('You Didnt State the User you want to send the dm to')

const msgs = args[1]
if(!msgs) return message.reply('You Didnt State the Message you want to send the user')

if(message.attachments.size > 0){
    var Attachment = message.attachments.array();
    var OurAttachment = Attachment[0];
    const attachment = new MessageAttachment(OurAttachment.url);

    target.send(msgs);
    target.send(attachment);
    message.channel.send('Sent a Dm to ' + target.username)

  } else {
     target.send(msgs);
     message.reply('Sent a Dm to ' + target.username)
  }