【问题标题】:Prevent message author from mentioning themselves with command Discord.py防止消息作者使用命令 Discord.py 提及自己
【发布时间】:2019-02-04 23:38:40
【问题描述】:

试图阻止用户在使用 rob 命令时提及自己。我试过if message.author == user.mention,但它不起作用。

   if message.content.startswith('!rob'):
        try:
            if message.author == user.mention:
                 await client.send_message(message.channel, "{} you cant rob yourself! ????".format(message.author.mention))
        except:
            pass
        else:
             if get_dollars(message.author) < 0:
                await client.send_message(message.channel, "{} you can't even afford a gun!".format(message.author.mention))
        finally:
            for user in message.mentions:
                if (get_dollars(user)) < 25:
                    await client.send_message(message.channel, "{} is too broke to rob ????".format(user.mention))
                elif steal == 1:
                    print("{} catches {} slippin and sticks them up for ${} ????????".format(message.author.mention, user.mention, stealdollars))
                    await client.send_message(message.channel, "{} catches {} slippin and sticks them up for ${} ????????".format(message.author.mention, user.mention, stealdollars))
                    remove_dollars(user, stealdollars)
                    add_dollars(message.author, stealdollars)
                elif steal == 2:
                    print("{} gets arrested trying to rob {} and has to post $250 bail ????????".format(message.author.mention, user.mention))
                    await client.send_message(message.channel, "{} gets arrested trying to rob {} and has to post $250 bail ????????".format(message.author.mention, user.mention))
                    remove_dollars(message.author, 250)
                elif steal >= 3:
                    print("{} kicks {}'s door down but doesn't find any cash ????".format(message.author.mention, user.mention))
                    await client.send_message(message.channel, "{} kicks {}'s door down but doesn't find any cash ????".format(message.author.mention, user.mention))

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案1】:

    message.author 是 discord.Member,user.mention 不是。

    也许可以尝试以下方式:

    if message.author.id == user.id: # User IDs don't change.
        await client.send_message(ctx.message.channel, "You cannot rob yourself")
    

    希望对你有帮助

    【讨论】:

    • 感谢您的建议!我试过了,但它允许它通过并且没有将消息发送到我想要的频道。我也试过message.author.id == user.mention.id,它也没有发送消息。
    • 对不起,我现在习惯重写,这是我的一个错误。忘记添加频道参数 lad.
    • 我还是不习惯重写,我需要尽快切换。我用过这个,但似乎它正在跳过它。我的 try/else/finally 设置是否正确?我注意到当有人拥有
    【解决方案2】:

    你可以这样做

    if message.author in message.mentions:
    

    【讨论】:

    • 谢谢,这解决了问题!
    猜你喜欢
    • 2021-04-29
    • 2021-05-11
    • 1970-01-01
    • 2021-01-07
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 2021-06-02
    • 2018-08-27
    相关资源
    最近更新 更多