我在Python中制作一个Discord bot,我想添加一个功能,当我使用命令_throw
和ping用户时,bot将根据用户ping回复(它最好是通常的文本消息,而不是)嵌入)。目前,我有这个代码:
if message.content == "_throw":
user = message.mentions[0]
await message.channel.send("You threw a hamster to " + f"{user}" + "!")
但是我的bot根本没有回复它(PyCharm没有看到任何错误)。
这是我的bot脚本:@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('_hi'):
await message.channel.send(f'Hello, {message.author.mention}!')
if message.content == "_throw":
user = message.mentions[0]
await message.channel.send("You threw hamster to " + f"{user}" + "!")
if message.content.startswith("_userinfo"):
user = message.mentions[0]
emb14 = discord.Embed(
title=f"@{user} info:",
colour=discord.Colour.dark_blue()
)
emb14.set_image(url=user.avatar_url)
emb14.add_field(name=f"Name", value=f"{user}", inline=True)
emb14.add_field(name=f"Discord Joined date", value=f"{user.created_at}", inline=False)
emb14.add_field(name=f"Server Joined date", value=f"{user.joined_at}", inline=False)
emb14.add_field(name="Profile Picture", value=":arrow_down: :arrow_down: ", inline=False)
await message.channel.send(embed=emb14)
client.run('TOKEN')
任何想法?
我理解您希望您的bot响应_throw @user
。在这种情况下,当您检查message.content == "_throw"
时,它过滤的消息恰好是_throw
而不是@user
部分。
要接受user - mention参数,最简单的方法是使用regex匹配:
import re
# First checks if there are any mentions in the message
# Then uses regex "^_throws+<@!?[0-9]{17,22}>$" to match the message content
if message.mentions and re.match(r"^_throws+<@!?[0-9]{17,22}>$", message.content):
user = message.mentions[0]
await message.channel.send(f"You threw a hamster to {user.mention)!")
这将允许bot回复_throw @user
并回复You threw a hamster to @user!