【问题标题】:Discord py how to make administrators command? [duplicate]Discord py如何使管理员命令? [复制]
【发布时间】:2021-04-03 06:56:55
【问题描述】:

如何在 discord py 中创建仅供管理员使用的命令?

async def 청소(ctx, amout:int):
    await ctx.channel.purge(limit=amout)
    print('한 유저가 청소를 했습니다.')

【问题讨论】:

标签: discord discord.py


【解决方案1】:

使用@commands.has_permissions()。当某人无权运行命令时调用commands.errors.MissingPermissions。要弹出错误消息,请使用 on_command_error 事件,如下所示:

@bot.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.errors.MissingPermissions):
        await ctx.send('You are missing permissions to run this command!')

不管怎样,最终的代码是:

@commands.has_permissions(administrator=True)
@bot.command()
async def 청소(ctx, amount:int):
    await ctx.channel.purge(limit=amount)
    print('한 유저가 청소를 했습니다.')

【讨论】:

    【解决方案2】:

    这应该会有所帮助:

    @client.command()
    async def clear(ctx, amount = 100):  #100 will be the default amount purged
        if ctx.author.guild_permissions.administrator:
            await ctx.channel.purge(limit = amount + 1)
            await ctx.send(f'**{amount}** message(s) deleted.')
        else:
            await ctx.send('Sorry, You are not an administrator.')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-31
      • 2020-12-23
      • 2020-10-09
      • 2021-05-22
      • 2020-12-24
      • 2021-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多