1
votes

I made a simple discord.py bot that reacts whenver it is PM'd. However, if someone messages it while my bot is offline, it will not react. How can i make it display all messages received while it was online on startup?

Current code:

import discord
from discord.ext import commands
import asyncio

client = commands.Bot(command_prefix='.')

@client.event
async def on_message(message):
if message.guild is None and message.author != client.user:

    send_this_console=f'{message.author}: \n{message.content}\n'
    print(send_this_console)
await client.process_commands(message)
1

1 Answers

0
votes

You'll need to use message history and track the last message sent. To get when a message was created:

lastMessageTimestamp = message.created_at

You'll need to store this in whatever way you want when your bot is offline (for example with pickle) and do this:

async for message in user.history(after = lastMessageTimestamp):
  print(message.content)