我制作了一个 Discord.js 机器人,但是当我将它告诉某人 DM 时,它会在 DM 中输入该人的 ID

I made a Discord.js bot, but when i tell it to DM someone, it types that person's ID in the DM

所以我做了这个 Discord.js 机器人,我将它编程为通过用户 ID 或提及的 DM 用户......当我告诉它给 DM 某人 [$send ] 它有效,但在消息中它发送该用户 ID。

代码:

const Discord = require('discord.js');

const client = new Discord.Client();

client.on ("ready", () => {
    console.log("the bot is ready...");

    client.user.setGame ("prefix is $");
});

const prefix = "$";
client.on ("message", (message) => {

    msg = message.content.toLowerCase();

    if (message.author.bot) return;

    mention = message.mentions.users.first();

    if (msg.startsWith (prefix + "send")) {
        if (mention == null) { return; }
        message.delete();
        mentionMessage = message.content.slice(8);
        mention.sendMessage(mentionMessage);
        message.channel.send("sent");
    }

});

client.login('tokenHereLmao');

最简单的方法是替换:

msg = message.content.toLowerCase();

if (message.author.bot) return;

mention = message.mentions.users.first();

if (msg.startsWith (prefix + 'send')) {
    if (mention == null) return;
    message.delete();
    mentionMessage = message.content.slice(8);
    mention.sendMessage(mentionMessage);
    message.channel.send('sent');
}

到正确的命令处理结构,如:

const args = message.content.slice(prefix.length).trim().split(/ +/); 
// transforms message like "$sEnd <@486615250376851477> Hello!" into an Array
// like ['sEnd', '<@486615250376851477>' 'Hello!']
const cmdname = args.shift().toLowerCase();
// takes the first element of the array and makes it toLowerCase
// cmdname = send
// args = ['<@486615250376851477>' 'Hello!']

mention = message.mentions.users.first();


if (cmdname == 'send')) {  // Checks for the Commmand
        if (mention == null) return message.channel.send('You need to mention someone');
        message.delete();
        args.shift(); // args = ['Hello!']
        mention.send(args.join(' '));  
        // args.join(' '); would transform ['Hey', 'how', 'are', 'you?'] to 
        // "Hey how are you?" and then send it
        message.channel.send("sent");
}

如果您需要良好的资源来设置具有良好结构的良好机器人,请使用 discord.js 的社区和创建者维护的官方和开源指南:http://discordjs.guide