我做了一个Discord.js机器人,但是当我告诉DM某人时,它会在DM中键入该人的ID。

时间:2019-03-22 18:14:38

标签: javascript node.js discord discord.js

所以我制作了这个Discord.js机器人,并通过他的userID或提及将其编程为DM用户...当我告诉DM某人[$ send]时,它可以工作,但在消息中它发送该用户ID。

Screenshot:

代码:

".runtimeconfig.json"

1 个答案:

答案 0 :(得分:1)

最简单的方法是替换:

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