Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

python - discord py - message.mentions "else" makes nothing

I want to get the user that was mentioned in a message and send him a private message. That's working without problems, but now I want to add a message for the case, that the mentioned member is not on the same server.

I searched a lot and try now for over 3 hours to find a solution.

Showcase of the problem: https://youtu.be/PYZMVXYtxpE

Heres my code:

@bot.event
async def on_message(message):
 if len(message.mentions) == 1:
   membe1 = message.mentions[0].id
   membe2 = bot.get_user(membe1)
   guild = bot.get_guild(message.author.guild.id)
   if guild.get_member(membe1) is not None:
      await membe2.send(content=f"you was mentioned in the server chat")
   else:
      embed2 = discord.Embed(title=f"? :warning: | PING not possible", description=f"not possible")
      await message.channel.send(content=f"{message.author.mention}", embed=embed)
      await message.delete()
      return

The first part is working without problems, but at the "else" part, does the bot nothing. He still posts the message with the "invalid" ping in the chat and just ignores it. What can I do to fix that?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...