Skip to content

Instantly share code, notes, and snippets.

@alissone
Created August 22, 2020 12:20
Show Gist options
  • Select an option

  • Save alissone/f241bbacfa629c3faebb36df7860b87a to your computer and use it in GitHub Desktop.

Select an option

Save alissone/f241bbacfa629c3faebb36df7860b87a to your computer and use it in GitHub Desktop.
Get last N messages from Discord using discord.py
import discord
import time
import asyncio
last_messages = 5
token = "YOUR TOKEN HERE"
client = discord.Client()
@client.event
async def on_message(message):
channels = ["general"]
if str(message.channel) in channels:
if message.content == "!history":
messages = await message.channel.history(limit=last_messages).flatten()
messages.reverse()
await message.channel.send(f"Last {last_messages} messages:")
await message.channel.send([message.content for message in messages])
client.run(token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment