Python 我想让我的机器人告诉你,当你不提某人时,你必须提及某人

Python 我想让我的机器人告诉你,当你不提某人时,你必须提及某人,python,discord.py,Python,Discord.py,我有很多有趣的命令,比如“拥抱,结婚等等。” 他们正在与 .拥抱用户 但是如果你不提及用户,我希望我的python机器人告诉我“你需要提及某人”。然而,每当我测试机器人时,什么都不会发生,用户也不知道他们需要提到一个用户 我的代码: async def hug(ctx,user:discord.Member): if not discord.Member: await ctx.send("you need to mention someone")

我有很多有趣的命令,比如“拥抱,结婚等等。”

他们正在与

.拥抱用户

但是如果你不提及用户,我希望我的python机器人告诉我“你需要提及某人”。然而,每当我测试机器人时,什么都不会发生,用户也不知道他们需要提到一个用户

我的代码:

async def hug(ctx,user:discord.Member):
    if not discord.Member:
        await ctx.send("you need to mention someone")
        return

    embed = discord.Embed(title=f"{ctx.author} hugs {user.name}", description="nice...")
    embed.set_image(url="url")

    await ctx.send(embed=embed)``
   


解决方案非常简单,message对象有一个
提及
属性,您可以检查消息提及的长度,如果它们不等于1,则应该存在错误

async def hug(ctx,用户:discord.Member):
如果len(ctx.message.notices)!=1:
等待ctx.send(“您必须提到一个用户!”)
返回
embed=discord.embed(title=f“{ctx.author}拥抱{user.name}”,description=“nice…”)
嵌入。设置_图像(url=“url”)
等待ctx.send(嵌入=嵌入)``

有关
消息的Discord API文档提到了属性

您检查的方式是错误的,您定义了一个名为
user
的属性,并为其指定了一个类型
Discord.Member
,因此需要检查是否传递了
user

async def hug(ctx, user:discord.Member):
    if not user:
        await ctx.send("you must mention a user!")
        return

    embed = discord.Embed(title=f"{ctx.author} hugs {user.name}", description="nice...")
    embed.set_image(url="url")

    await ctx.send(embed=embed)
如果您正在检查len(ctx.message.notices)!=1,即当您使用:
.hug xyz@billy
它也将通过if条件,但不起作用,相反,它将抛出一个错误。因为一个BadArgument-
xyz
被第二次传递。

实际的问题是什么?执行代码时会发生什么?看起来会有用的。添加所有相关信息有助于提供有用的答案—什么都不会发生