【问题标题】:Discord bot doesn't message the user that I taggedDiscord 机器人不会向我标记的用户发送消息
【发布时间】:2021-03-23 12:11:56
【问题描述】:

我对 node js 很陌生,我无法让我的 discord 机器人向我标记的用户发送消息。以下是代码应该如何的示例:

用户:howcool @MyFriend 机器人:@我的朋友是 75% 的酷!

下面是我不成功的代码:

    client.on('message', async message => {
    if (message.author.bot) return;

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

    if (msg.startsWith(".pfx howcool") && mention) {
        message.channel.send(`${mention} is ${Math.floor(Math.random() * 100) + 1}% cool!`)
        
    } else if (message.content === ".pfx howcool"){
        message.channel.send(`You are ${Math.floor(Math.random() * 100) + 1}% cool!`)
}});

【问题讨论】:

  • 当您没有提及用户时,您可能会收到错误 - Cannot read property first of undefined 如果不是这样,请告知错误

标签: javascript node.js discord discord.js bots


【解决方案1】:

而不是(`${mention} is ${Math.floor(Math.random() * 100) + 1}% cool 你可以使用:(``<@{mention.id}> is ${Math.floor(Math.random() * 100) + 1}% cool

Mention 是一个 Discord.User 对象,包括 id、系统、语言环境、标志、用户名、机器人、鉴别器、avator、lastmessadeID、lastmessagechannelid 的数据

要 ping 某人,您需要使用以下格式的 id: 这也是 discord 以用户身份 ping 某人时所做的。

    if(message.content.startsWith('.pfx howcool') && mention){
        message.channel.send(`<@${mention.id}> is ${Math.floor(Math.random() * 100) + 1}% cool`)
    }
    if(message.content.startsWith('.pfx howcool') && !mention){
        message.channel.send(`You are ${Math.floor(Math.random() * 100) + 1}% cool!`)
    }

【讨论】:

    【解决方案2】:

    你可以这样做。

    client.on('message', async message => {
        if (message.author.bot) return; //Ignore the bot messages
    
        if(message.content.startsWith(".pfx howcool"){ //Sorting the command name
          if(message.mention.members){ //If there are mentions
              message.channel.send(`<@${message.mention.members.first().id}> is ${Math.floor(Math.random() * 100) + 1}% cool`)
          }else if(!message.mention.members){ //If there no mentions.
            message.channel.send(`You are ${Math.floor(Math.random() * 100) + 1}% cool!`)
        }
    }
    

    请阅读评论,你会更好地理解我写的代码!

    【讨论】:

      猜你喜欢
      • 2022-08-11
      • 2021-10-27
      • 2021-08-19
      • 2021-01-19
      • 2018-04-28
      • 2020-11-23
      • 2021-01-08
      • 2019-06-27
      • 1970-01-01
      相关资源
      最近更新 更多