How do I get message contents from channel.history using a Discord bot?

Question:

I have code that (wants) prints out message history, however I get a barely useful slur of info, IDs and more.

<Message id=x channel=<TextChannel id=x name='general' position=5 nsfw=False news=False category_id=x> type=<MessageType.default: 0> author=<Member id=x name='Cheesebot' discriminator='1916' bot=True nick=None guild=<Guild id=x name='NoLife Minecraft' shard_id=None chunked=False member_count=18>> flags=<MessageFlags value=0>>
<Message id=x channel=<TextChannel id=x name='general' position=5 nsfw=False news=False category_id=x> type=<MessageType.default: 0> author=<User id=x name='' discriminator='3912' bot=False> flags=<MessageFlags value=0>>
<Message id=x channel=<TextChannel id=x name='general' position=5 nsfw=False news=False category_id=x> type=<MessageType.default: 0> author=<Member id=x name='Cheesebot' discriminator='1916' bot=True nick=None guild=<Guild id=x name='NoLife Minecraft' shard_id=None chunked=False member_count=18>> flags=<MessageFlags value=0>>
<Message id=x channel=<TextChannel id=x name='general' position=5 nsfw=False news=False category_id=x> type=<MessageType.default: 0> author=<User id=x name='' discriminator='3912' bot=False> flags=<MessageFlags value=0>>
<Message id=x channel=<TextChannel id=x name='general' position=5 nsfw=False news=False category_id=x> type=<MessageType.default: 0> author=<User id=x name='' discriminator='3912' bot=False> flags=<MessageFlags value=0>>
<Message id=x channel=<TextChannel id=x name='general' position=5 nsfw=False news=False category_id=x> type=<MessageType.default: 0> author=<User id=x name='' discriminator='3912' bot=False> flags=<MessageFlags value=0>>
<Message id=x channel=<TextChannel id=x name='general' position=5 nsfw=False news=False category_id=x> type=<MessageType.default: 0> author=<Member id=x name='Cheesebot' discriminator='1916' bot=True nick=None guild=<Guild id=x name='NoLife Minecraft' shard_id=None chunked=False member_count=18>> flags=<MessageFlags value=0>>
<Message id=x channel=<TextChannel id=x name='general' position=5 nsfw=False news=False category_id=x> type=<MessageType.default: 0> author=<Member id=x name='Cheesebot' discriminator='1916' bot=True nick=None guild=<Guild id=x name='NoLife Minecraft' shard_id=None chunked=False member_count=18>> flags=<MessageFlags value=0>>
<Message id=x channel=<TextChannel id=x name='general' position=5 nsfw=False news=False category_id=x> type=<MessageType.default: 0> author=<User id=x name='' discriminator='3912' bot=False> flags=<MessageFlags value=0>>
<Message id=x channel=<TextChannel id=x name='general' position=5 nsfw=False news=False category_id=x> type=<MessageType.default: 0> author=<Member id=x name='Cheesebot' discriminator='1916' bot=True nick=None guild=<Guild id=x name='NoLife Minecraft' shard_id=None chunked=False member_count=18>> flags=<MessageFlags value=0>>

The code I have right now is this:

  async def messagesl(sendc):
    messages = await sendc.history(limit=10).flatten()
    return messages

What I need is a way to get the message contents, and other info like user, etc.

sensitive info is an x (or for names, nothing.)

Asked By: Cheesebellies

||

Answers:

messages is a list of discord.Message objects. You’ll want to loop through the list and get the Message.content of each of them.


References

Answered By: dank tagg

Simply loop through the messages and access the attributes you need

messages = await sendc.history(limit=10).flatten()
contents = []

for message in messages:
    contents.append(message.content)

If you want a one-liner

contents = [message.content for message in messages]
Answered By: Łukasz Kwieciński

As of today, the correct way to get chat history in Discord.py is by using a list comprehension with an async for loop. This method allows you to retrieve a specific number of messages from a channel’s history.

To do this, you can use the following code:

messages = [message async for message in channel.history(limit=123)]

It’s important to note that this method uses an async for loop, so you will need to make sure your code is within an async function.

You could then simply print the contents:

for msg in messages:
    print(msg.content)
Answered By: Leopold W.