提及用户的discord.py随机消息

时间:2020-07-10 22:36:14

标签: discord.py

py最近,我希望机器人发送随机消息提及用户,但这给了我这个错误:

discord.ext.commands.errors.CommandInvokeError:命令引发了异常:AttributeError:'NoneType'对象没有属性'mention'

代码如下:

@client.command()
async def randomroman(ctx, *,member: discord.Member=None):

    mention = member.mention
    variable=[
        f'{mention} ama tanto roman!',
        f'{mention} odia tanto roman!',
        f'{mention} ama roman!',
        f'{mention} odia roman!'
    ]
    await ctx.message.channel.send(random.choice(variable))

1 个答案:

答案 0 :(得分:0)

因此,您似乎已设置了默认值,因此在尝试发送消息之前,应检查是否已提及成员。这是您可以使用的两个不同的代码段。

@client.command()
async def randomroman(ctx, member: discord.Member=None):
    if not member:
        # We dont have anyone to mention so tell the user
        await ctx.send("You need to mention someone for me to mention!")
        return

    variable=[
        f'{member.mention} ama tanto roman!',
        f'{member.mention} odia tanto roman!',
        f'{member.mention} ama roman!',
        f'{member.mention} odia roman!'
    ]
    await ctx.send(random.choice(variable))

您也可以简单地使用ctx.send()

您可以做的另一件事是,如果他们不调用命令提及任何人,就提及作者,

@client.command()
async def randomroman(ctx, member: discord.Member=None):
    member = member or ctx.author

    variable=[
        f'{member.mention} ama tanto roman!',
        f'{member.mention} odia tanto roman!',
        f'{member.mention} ama roman!',
        f'{member.mention} odia roman!'
    ]
    await ctx.send(random.choice(variable))

在这种情况下,这两个都将起作用。 !randomroman!randomroman @user会提及一个用户。

希望这会有所帮助!