Discordpy member history() function returning nothing

Viewed 788

I've been trying to get my own message history using the member.history() method. But all i get is an empty asyncIterator that doesn't have anything in it.

import discord
from discord.ext import commands

client = commands.Bot(command_prefix = ".",intents=discord.Intents.all())

@client.event
async def on_ready():
    print('Bot is ready')

@client.command()
async def test(ctx):
    me = ctx.guild.get_member_named("Slade")
    print(me)
    async for message in me.history(limit=5):
        print(message)

client.run("token would go here")

The only thing the code above prints is the ready message and my own discord name and tag. What am i doing wrong?

2 Answers

Figured out the problem. For some reason both member.history() and user.history() function return private DM's with the bot not the guild message history.

Do the following

messages = await channel.history(limit=5).flatten()

this will return a list of messages.

So your function will be:

@client.command()
async def test(ctx):
    me = ctx.guild.get_member_named("Slade")
    print(me)
    messages = []
    for channel in ctx.guild.channels:
        message = await channel.history(limit=5).flatten()
        messages.append(message)
    for message in messages:
        print(message[0].content)
    ```

This returns for you 5 messages of every channel in your server.
Related