提问者:小点点

我如何制作一个discord js机器人,让DM成为一个被提及的用户?


我在做一个不和谐机器人,让你使用“!dm"命令。每当有人使用“!dm"命令,放入一个用户的用户ID/提及,并放入一条消息,它DMs该用户/提及的用户ID。这是我的代码:

const { Client } = require('discord.js');
const bot = new Client();

const TOKEN = process.env.TOKEN;

const PREFIX = '!';

bot.on('ready', () => {
 console.log('This bot is active!');
});

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

bot.login(TOKEN);

共2个答案

匿名用户

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)
  }

匿名用户

集合客户端。用户。隐藏物get()是由id映射的,因此它应该是客户机。用户。隐藏物get(taggedUser.id)。然而,一个更大的问题是,您称您的客户机为bot而不是client,这就是客户机的原因。用户。隐藏物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');
 }
});

确保您记得您所称的变量!