【问题标题】:clear all messages in certain channel on_load清除特定频道 on_load 中的所有消息
【发布时间】:2019-02-19 05:45:29
【问题描述】:

当我在玩我的机器人时,我在只有我可以访问的“bottesting”频道中做所有事情,我试图弄清楚如何让机器人加载以清除或删除当前在该频道中的所有消息某个频道

我尝试让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.

【问题讨论】:

  • 也许最好将您的 clear 命令的逻辑分解到另一个协程中,然后从 on_ready 和 clear 调用该协程。
  • 还在学习python,所以要读一下协程,别以为我还没用过,如果是的话那不是故意的哈哈
  • 协程是async def 函数。我的意思是做一些你可以从两个不同的地方调用的东西,从每个地方传递相关信息(要清除的通道)
  • 啊,好吧,我明白了,这是有道理的,只是在某个地方可以制作和存储可以调用的东西?如果我可以让机器人调用自己 id 或者甚至找出一个不可调用的自动命令 on_ready
  • 其实我可以看看你的clear命令吗?根据其设置方式,这可能相当容易。

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


【解决方案1】:

这是您可以将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)

【讨论】:

  • 做到了!非常感谢人!那是一个星期的头痛,哈哈
猜你喜欢
  • 2021-04-21
  • 2020-11-22
  • 2019-06-13
  • 2015-12-25
  • 2022-01-12
  • 2020-10-01
  • 1970-01-01
  • 2021-07-06
  • 1970-01-01
相关资源
最近更新 更多