首页 > 解决方案 > 如何让不和谐机器人复制频道中的最新消息?

问题描述

所以我想创建一个机器人来复制每个人所说的一切。我试图制作一个 .txt 文件,其中包含某人所说的所有内容,但它很快就会变得混乱,并且无论如何都不会发送消息。有什么帮助吗?

代码:

@bot.command()
async def copy(ctx):
    with open("file.txt", "w") as f:
        async for message in ctx.history(limit=1000):
            f.write(message.content + "\n")

标签: pythondiscorddiscord.pybots

解决方案


要在发送后立即再次发送每条消息,请添加:

@bot.event
async def on_message(message):
    await message.channel.send(message.content)
    await client.process_commands(message)

为了避免机器人也增加它自己的消息,检查是否message.author是机器人:

@bot.event
async def on_message(message):
    if not message.author.bot:
        ctx = await bot.get_context(message)
        await ctx.send(message.content)
        await client.process_commands(message)

如果需要,您可以删除命令副本。

参考:


推荐阅读