【问题标题】:Discord.py issues with copying bulk messages复制批量消息时出现 Discord.py 问题
【发布时间】:2021-03-22 19:24:05
【问题描述】:

就上下文而言,我与我自己和我的朋友一起为服务器创建了一个不和谐机器人。

我希望这个机器人做的部分事情是能够复制频道中的所有消息以进行备份,因为某些频道包含我们不想丢失的东西。

我已经得到它来复制消息,但它在连续复制许多消息时遇到问题。

代码如下:

@client.command()
async def MessageCopy(ctx, comeFrom, goTo):
    attachments = []
    aafile = None
    fromChannel = client.get_channel(int(comeFrom))
    toChannel = client.get_channel(int(goTo))
    async for message in fromChannel.history(oldest_first=True, limit=None):
        if not message.attachments == None:
            for file in message.attachments:
                aafile = await file.to_file()
                attachments.append(aafile)
        try:
            await toChannel.send(content=message.clean_content, file=aafile)
        except:
            continue
        attachments = []
        aafile = None

客户端定义为

commands.Bot(command_prefix="$", case_insensitive=True)

机器人每次都被同一条消息捕获,消息没有什么特别之处,它不会抛出错误,它只是安静地停止复制。如果这很重要,那就是它无法复制的频道中的第 9639 条消息。

编辑:我通过它运行了另一个频道,它再次安静地停止,这次是第 9758 条消息。

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    我找到了修复/解决方法。

    原来是这样的

    aafile = file.to_file()
    

    这导致它停止复制,该代码会导致错误并且它会安静地停止。

    所以我将围绕它的代码更改为:

    for file in message.attachments:
        try:
            aafile = file.to_file()
            attachments.append(aafile)
        except:
            continue
    

    【讨论】:

      猜你喜欢
      • 2021-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-21
      • 1970-01-01
      • 2021-03-17
      • 1970-01-01
      • 2017-10-01
      相关资源
      最近更新 更多