【问题标题】:How would I get my bot to return a message saying "Permission Denied, you do not have sufficient permissions."我如何让我的机器人返回一条消息“权限被拒绝,您没有足够的权限”。
【发布时间】:2019-06-21 21:08:48
【问题描述】:

在我的代码中,如果用户执行这些命令之一,我希望我的机器人在同一频道中发送消息“您没有足够的权限。”

我已经尝试过了,但它不起作用。

async def kick(ctx, member: discord.Member):
    if ctx.message.author.server_permissions.kick_members:
        await client.kick
        embed = discord.Embed(title="User Kicked!",
                              description="**{0}** kicked by **{1}**!".format(member, ctx.message.author),
                              color=discord.Colour.green())
        await client.say(embed=embed)
    else:
        embed = discord.Embed(title="Permission Denied.", description="You don't have permission to use this command.",
                              color=discord.Colour.red())
        await client.say(embed=embed)

我在上面添加了else:,而else 下方的任何内容都不起作用。

client = Bot(command_prefix=BOT_PREFIX)
commands = discord.ext.commands

@client.command(name='kick', pass_context=True)
@commands.has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member):
    if ctx.message.author.server_permissions.kick_members:
        await client.kick
        embed = discord.Embed(title="User Kicked!",
                              description="**{0}** kicked by **{1}**!".format(member, ctx.message.author),
                              color=discord.Colour.green())
        await client.say(embed=embed)```

I expect my bot (client) to say in the same channel the command was sent in to say "You do not have sufficient permissions."


【问题讨论】:

  • 如果检查失败,您需要使用错误处理程序来执行操作。看到这个答案:stackoverflow.com/a/51246799/6779307
  • 感谢@PatrickHaugh,救命稻草。像魅力一样工作。

标签: python discord.py discord.py-rewrite


【解决方案1】:

您可以使用permissions_for 来检查权限而不会引发异常。在你的情况下,它看起来像ctx.channel.permissions_for(ctx.author).kick_members

https://discordpy.readthedocs.io/en/rewrite/api.html?highlight=channel#discord.TextChannel.permissions_for

【讨论】:

    猜你喜欢
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多