简体   繁体   中英

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. I want my bot to mention someone and say how cool he is when I mention someone on Discord in any channel, for example:

 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 ). 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 ).

A message has a mentions property which is a MessageMentions which have 2 property that might interest you: users and members . 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 .

Note: you can access the user from the GuildMember with the varGuildMember.user .

Here you want to mention someone. Both type has a toString() method which return a string mentionning the user. 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 .

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. 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 .

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

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. I use python so my format for string is {}.format() but you should be able to use a format to. (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))

Here is a python example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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