【问题标题】: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 机器人,但是当我告诉它给某人 DM 时,它会在 DM 中输入那个人的 ID
【发布时间】:2019-08-13 19:03:03
【问题描述】:

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

代码:

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');

【问题讨论】:

  • message.content 是一个字符串,对它进行切片/转换,以便删除提及。您的 Discord ( Bot ) 收到 <@userid> 格式的提及,然后客户端将其转换为 @Name 显然您的 Bot 没有这样做。
  • 我该如何解决这个问题?

标签: javascript node.js discord discord.js


【解决方案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

【讨论】:

  • 现在机器人只能提及服务器中的人:p 它被用来提及共同服务器中的人,即使我从我的服务器执行命令。
  • 要添加什么脚本吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-04
  • 1970-01-01
  • 1970-01-01
  • 2018-08-22
  • 2019-10-20
  • 2019-02-19
  • 2020-10-14
相关资源
最近更新 更多