擅长:python、mysql、java
<p>您可以移除<code>amount</code>的默认值,并捕获当用户写入错误内容时抛出的ValueError</p>
<pre><code>@client.command(pass_context=True)
async def purge(ctx, amount):
try:
if ctx.message.author.server_permissions.manage_messages:
channel = ctx.message.channel
messages = []
async for message in client.logs_from(channel, limit=int(amount) + 1):
messages.append(message)
try:
await client.delete_messages(messages)
await client.say('Purged {} message(s)!'.format(len(messages)-1))
except Exception as inst:
await client.say("One or more messages older than 14 days\n{}".format(inst))
else:
embed = discord.Embed(
title = ':x: Insufficient Permissions',
description = 'You need to have the Manage Messages premission to execute this command.',
colour = 0xff0000
)
await client.say(embed=embed)
except ValueError as va:
await client.say(va)
</code></pre>
<p>我还添加了try/except,当至少有一条消息已经存在14天,因此无法删除时</p>