如何在 discord py 中创建仅供管理员使用的命令?
async def 청소(ctx, amout:int):
await ctx.channel.purge(limit=amout)
print('한 유저가 청소를 했습니다.')
答案 0 :(得分: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('한 유저가 청소를 했습니다.')
答案 1 :(得分:0)
这应该会有所帮助:
@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('**' + str(amount) + '** message(s) deleted.')
else:
await ctx.send('Sorry, You are not an administrator.')