Devalart
Devalart

Reputation: 63

How to make a discord bot copy the latest message in a channel?

So I want to create a bot that copies everything that everybody says. I tried to make a .txt file that puts everything someone says, but it gets cluttered quickly and it doesn't send the messages anyway. Any help?

Code:

@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")

Upvotes: -1

Views: 2512

Answers (1)

LoahL
LoahL

Reputation: 2613

To send every message again immediately after it got sent, add:

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

To avoid the bot also multiplying it's own message, check if the message.author is the bot:

@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)

You can delete the command copy if you want to.

References:

Upvotes: 1

Related Questions