Python Bot删除消息

时间:2018-08-07 14:13:09

标签: python-3.x discord.py

如何使漫游器删除xx消息。示例!clear xx!clear xx @user

If i use `!clear xx` it should delete xx message.
If i use `!clear xx @user` it should delete xx message of User tagged.

1 个答案:

答案 0 :(得分:1)

this answer作为起点:

from discord.ext.commands import has_permissions
from datetime import datetime

@bot.command(pass_context=True)
@has_permissions(administrator=True)  # Like the properties of a Permissions object
async def censor(ctx, limit: int, target: discord.User = None):
    check = (lambda message: message.author.id == target.id) if target else None
    try:
        await bot.purge_from(ctx.message.channel, 
                             check=check, 
                             limit=limit)
    except discord.Forbidden:
        await bot.say("I don't have permission to do that")

@censor.error
async def censor_error(error, ctx):
    if isinstance(error, commands.CheckFailure):
        await bot.send_message(ctx.message.channel, "You don't have permissions")

请注意,purge_from需要一个机器人帐户。