問題描述
我正在向我的 Discord 機器人添加個人資料卡,但我遇到了一個問題.當有人鍵入 !profile @user
我不確定如何正確解析 @user 以便機器人知道要查找哪個個人資料卡.
I am adding profile cards onto my Discord bot, but I've come across one issue. When someone types !profile @user
I am not sure how to properly parse for @user so the bot knows which profile card to lookup.
我首先解析 message.content,然后刪除消息內(nèi)容的前 9 個字符(始終是 !profile
),但消息內(nèi)容的其余部分返回看起來 < 的 user_id;@289583108183948460>
而不是用戶的歧視.我曾嘗試使用 re.sub 刪除特殊字符(@、> 和 <),如下所示:
I first parse message.content and then remove the 9 first chars of the message content (which is always !profile
) but the rest of the message content returns the user_id which looks <@289583108183948460>
instead of the user's discrim. I have tried using re.sub to remove the special characters (@, >, and <) like this:
a = str(message.content[9:])
removeSpecialChars = re.sub("[!@#$%^&*()[]{};:,./<>?|`~-=_+]", " ", a)
print(removeSpecialChars)
但是當我只想要數(shù)字時,奇怪的字符仍然存在,因此我可以輕松地在數(shù)據(jù)庫中搜索它.我確信有更好的方法可以做到這一點,但我想不通.
But the weird characters are still there when I only want the number so I can search it in the database easily. I'm sure there's a way better way to do this though but I can't figure it out.
推薦答案
discord.py 的消息對象包含 Message.mentions
屬性,因此您可以遍歷 Member
.以下是 async 和 的文檔列表 rel="nofollow noreferrer">重寫.
discord.py's message objects include a Message.mentions
attribute so you can iterate over a list of Member
. Here are the doc listings for async and rewrite.
有了這個,你可以簡單地迭代提及:
With this you can simply iterate over the mentions as so:
for member in ctx.message.mentions:
# do stuff with member
你真正想要的
discord.py 允許您從帶有 discord.Member 對象"nofollow noreferrer">類型提示.只需將以下內(nèi)容添加到命令中
what you actually want
discord.py allows you to grab discord.Member
objects from messages with type hinting. simply add the following to the command
@bot.command()
async def profile(ctx, member: discord.Member=None):
member = member or ctx.message.author
這篇關于如何正確解析我的 Discord 機器人中的標記用戶?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!