私人消息用户

私人消息用户

问题描述:

我目前正在使用discord.js库和node.js制作具有一个功能的discord机器人-私人消息传递人员。

I am currently using the discord.js library and node.js to make a discord bot with one function - private messaging people.

我希望这样做当用户在频道中说 / talkto @ bob#2301时,机器人会在@ bob#2301上显示一条消息。

I would like it so that when a user says something like "/talkto @bob#2301" in a channel, the bot PMs @bob#2301 with a message.

所以我想知道是...我该如何使bot向特定用户发送消息(我目前所知道的是如何向'/ talkto'的作者发送消息),以及如何使bot能够找到所需的用户命令中的消息。 (因此,/ talkto @ryan消息ryan和/ talkto @daniel消息daniel等。)

So what I would like to know is... how do I make the bot message a specific user (all I know currently is how to message the author of '/talkto'), and how do I make it so that the bot can find the user it needs to message within the command. (So that /talkto @ryan messages ryan, and /talkto @daniel messages daniel, etc.)

我当前的代码(错误代码)是:

My current (incorrect code) is this:

client.on('message', (message) => {
    if(message.content == '/talkto') {
        if(messagementions.users) { //It needs to find a user mention in the message
            message.author.send('Hello!'); //It needs to send this message to the mentioned user
    }
}

我已经阅读了文档,但发现很难理解,我将不胜感激!

I've read the documentation but I find it hard to understand, I would appreciate any help!

发送方法可以在 User 对象中找到..因此,为什么可以使用 message.author.send ... message.author指向发送消息的人的用户对象,您要做的就是发送给指定的用户。此外,还可以使用 if(message.content == / talkto) 意味着它只会在整个混乱中运行年龄是/ talkto。意思是,您不能拥有/ talkto @me。使用 message.content.startsWith()

The send method can be found in a User object.. hence why you can use message.author.send... message.author refers to the user object of the person sending the message. All you need to do is instead, send to the specified user. Also, using if(message.content == "/talkto") means that its only going to run IF the whole message is /talkto. Meaning, you can't have /talkto @me. Use message.content.startsWith().

client.on('message', (message) => {
    if(message.content.startsWith("/talkto")) {
        let messageToSend = message.content.split(" ").slice(2).join(" ");
        let userToSend = message.mentions.users.first();

        //sending the message
        userToSend.send(messagToSend);
    }
}

示例用法:

/ talkto @wright您好,这是一个dm!

/talkto @wright Hello there this is a dm!