清除特定频道on_load上的所有消息

时间:2019-02-18 06:34:57

标签: python python-3.x discord.py

在与我的机器人玩游戏时,我只能在我能访问的“机器人测试”频道中进行所有操作,我试图弄清楚如何使该机器人加载以清除或删除该机器人中当前存在的所有消息。某个频道

我试图使bot.say(!clear)成为其自己的命令以清除频道,但无法识别其输出,我目前必须手动执行我设置的清除命令以供其他人使用

@bot.event 
async def on_ready():
    #bot announces to me when its online and posts in bottesting channel
    await bot.send_message(discord.Object(id=bottesting), 'Im Back ' + myid)
    print('<',datetime.datetime.now(),'>',NaMe," Launched")

This is what my current on_ready looks like and as of now it just posts that it is online and tags me in the post and the channel as well as outputs to cmd with print.

1 个答案:

答案 0 :(得分:2)

这是将clear变成可以在两个地方调用的单独协程的一种方法:

@bot.command(pass_context=True) 
@has_permissions(administrator=True, manage_messages=True, manage_roles=True) 
async def clear(ctx, amount: int=1): 
    await _clear(ctx.message.channel, amount)  # Technically you could apply the decorators directly 
                                               # to the _clear callback, but that's a little complicated

async def _clear(channel, amount=1):
    messages = [] 
    async for message in bot.logs_from(channel, limit=amount+1):     
        messages.append(message) 
    await bot.delete_messages(messages)

@bot.event
async def on_ready():
    channel = bot.get_channel("123")
    await _clear(channel, 100)