简体   繁体   English

如何让我的 Discord Bot 提到我提到的某个人?

[英]How can I make my Discord Bot mention someone I mentionned?

I just started coding a Discord bot and I have a small problem with mentioning someone.我刚开始编写一个 Discord 机器人,但提到某人时遇到了一个小问题。 I want my bot to mention someone and say how cool he is when I mention someone on Discord in any channel, for example:我希望我的机器人在任何频道的 Discord 上提到某人时,会提到某人并说他有多酷,例如:

 Dahkris: lul howcool @Myfriend Bot: @Myfriend is 80% cool !

( The random part is functional ) (随机部分是功能性的)

Since I'm new to js, I don't know how those arguments work, so I don't know if I should use @member or "member.displayName", etc ( I tried different types ).由于我是 js 的新手,我不知道这些参数是如何工作的,所以我不知道我是否应该使用 @member 或“member.displayName”等(我尝试了不同的类型)。 I've already searched for similar codes but usually the bot only mention the author of the message.我已经搜索过类似的代码,但通常机器人只提到消息的作者。

bot.on('message', message => {
    if (message.startsWith === 'lul howcool') {
      if ( message.content === @member ) {
      message.channel.send ( @member + ' is ' + ( Math.floor(Math.random() * 100) + 1 ) + "% cool ! " )
    }
  }
  })

This code doesn't seem to have any errors but doesn't work because the message never contains the " @member" ( I think ).此代码似乎没有任何错误,但不起作用,因为该消息从不包含“@member”(我认为)。

A message has a mentions property which is a MessageMentions which have 2 property that might interest you: users and members .一条消息有一个mentions属性,它是一个MessageMentions ,它有 2 个您可能感兴趣的属性: usersmembers The difference will be with what you want to do with it.不同之处在于你想用它做什么。 members is a collection of GuildMember while users is a collection of user . membersGuildMember的集合,而usersuser的集合。

Note: you can access the user from the GuildMember with the varGuildMember.user .注意:您可以访问userGuildMembervarGuildMember.user

Here you want to mention someone.在这里你想提一个人。 Both type has a toString() method which return a string mentionning the user.这两种类型都有一个toString()方法,该方法返回一个提及用户的字符串。 For example, if you have an instance of someone in the variable oneUser , and you do channel.send('Hello ' + oneUser) , the output will be Hello @TheUser .例如,如果变量oneUser有某人的实例,并且执行channel.send('Hello ' + oneUser) ,则输出将为Hello @TheUser

How to use it will depend on how your command work (checking if there's only one mention, how many arguments, etc).如何使用它取决于你的命令是如何工作的(检查是否只有一个提及,有多少参数等)。 I'll do the simplest form, ie if the message start with lul howcool and if it contains the mention of an user.我会做最简单的形式,即如果消息以lul howcool并且它包含用户的提及。 If there's other message, it'll still work.如果有其他消息,它仍然有效。

bot.on('message', message => {
  if (message.startsWith('lul howcool')) { // this is how you use startsWith https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith
    if (message.mentions.users.length > 0) { // check if an user is mentionned
      message.mentions.users.forEach((k, v) => { // do something for each mentions
        message.channel.send( v + ' is ' + ( Math.floor(Math.random() * 100) + 1 ) + "% cool ! " );
      })
    }
  }
})

It will send a message for each mentions (user mentions, not channel or role) in the message.它将为消息中的每个提及(用户提及,而不是频道或角色)发送一条消息。

Disclaimer : I can't test the code so there might be error.免责声明:我无法测试代码,因此可能存在错误。 The logic behind it is still viable.其背后的逻辑仍然可行。 If you want to handle possible error after sending a message, you souldn't use forEach but a for loop because forEach doesn't work with promise see this .如果您想在发送消息后处理可能的错误,则不应使用forEach而是使用 for 循环,因为forEach不适用于 promise, 请参阅此内容

So I fixed the code and tested it and it works.所以我修复了代码并测试了它并且它有效。 I chose a different approach, but the outcome is the same.我选择了不同的方法,但结果是一样的。

 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!`)
}});

Here's a simple code snippet that evaluates a mention kick:这是一个评估提及踢的简单代码片段:

documentation is on: https://discordjs.guide/creating-your-bot/commands-with-user-input.html#mentions文档位于: https : //discordjs.guide/creating-your-bot/commands-with-user-input.html#mentions

else if (command === 'kick') {
    // grab the "first" mentioned user from the message
    // this will return a `User` object, just like `message.author`
    const taggedUser = message.mentions.users.first();
    message.channel.send(`You wanted to kick: ${taggedUser.username}`);
}

You can use @USER ID in a string for example @766369570550186036 you can also format.您可以在字符串中使用@USER ID例如@766369570550186036您也可以格式化。 I use python so my format for string is {}.format() but you should be able to use a format to.我使用 python,所以我的字符串格式是{}.format()但你应该能够使用格式。 (Also the @USER ID thing works across all types of code because its a discord function. so if you were to type a message that contained @USER ID (Replacing "USER ID" with a users ID It will mention that user)) (此外, @USER ID东西适用于所有类型的代码,因为它是一个不和谐的功能。因此,如果您要键入包含@USER ID的消息(将“用户 ID”替换为用户 ID,它将提及该用户))

Here is a python example:这是一个python示例:

await ctx.send("hello <@{}>".format(ctx.author.id)
const user = message.mentions.users.first();
message.channel.send(`${user} //code`

with //code being the rest of the text of the message and the rest of the code //代码是消息文本的其余部分和代码的其余部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM