@client.command()
async def clear(ctx, amount=2):
await ctx.channel.purge(limit=amount)
await ctx.send(f'Note:I have cleared the previous two messages\nDeleted messages:{(ctx)}')
我正在尝试使该机器人显示它已删除的消息,但我不知道该怎么做。
答案 0 :(得分:1)
为此,请channel.history。这样做比较好,因为在一次删除channel.purge的同时,channel.history在删除之前先查看每条消息,这意味着您可以获取内容。
@client.command()
async def clear(ctx, amount:int=2):
messagesSaved = [] # Used to save the messages that are being deleted to be shown later after deleting everything.
async for msg in ctx.channel.history(limit=amount, before=ctx.message): # Before makes it that it deletes everything before the command therfore not deleting the command itself.
await msg.delete() # Delets the messages
messagesSaved.append(msg.content) # Put the message in the list
await ctx.send(f'Note: I have cleared the previous {amount} messages.\nDeleted messages:\n'+'\n'.join(str(message) for message in messagesSaved))
将消息保存在列表中而不是在删除后说是一件好事,因此我们可以立即发送所有消息,而不是在删除后立即删除并发送消息,因为这可能会引起很多通知,尤其是在删除大量消息时。 / p>