There is an error of design in what you are trying to do.
In Discord, you are not able to mention a user if he is not on that same server, so what you are checking will never work at all, currently your code is pretty much checking if the mentioned user exists, which will always happen unless he leaves the guild at the same time the command is executed.
Say for example, to make your command work you want to mention the user user123, who is in your server, you do the following:
@user123 blablabla
And that works because Discord internal cache can find user123 in your server and it is mentioned, which means that clicking on "@user123" will show us a target with his avatar, his roles or whatsoever.
But if you try to do the same for an invalid user, let's say notuser321:
@notuser321 blablabla
This is not a mention, take for example that you know who notuser321 is but he is in the same server as the bot is. Discord cannot retrieve this user from the cache and it is not considered a mention, just a single string, so not a single part of your code will be triggered, because len(message.mentions)
will be 0
.
Something that you could try would be using regular expressions to find an attempt to tag within message.content
. An example would be something like this:
import re
message = "I love @user1 and @user2"
mention_attempt = re.findall(r'[@]S*', message) # ['@user1', '@user2']
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…