Jenga
Jenga

Reputation: 63

TypeError: 'method' object is not iterable

I am newer to asynchronous coding. I am trying to resolve the error: TypeError: 'method' object is not iterable. The code I am using is below.

async def on_message(message):
    for msg in message.channel.history:
            fetched_message = await message.channel.fetch_message(msg.id)
            reactions = fetched_message.reactions
            users = []
            for reaction in reactions:
                    users = await reaction.users().flatten() 
                    for u in users:
                            print(message.id, reaction, u, message.channel, 1)

This code block works, but I want to loop it like above.

    fetched_message = await message.channel.fetch_message(1111111)
    reactions = fetched_message.reactions
    users = []
    for reaction in reactions:
            users = await reaction.users().flatten() 
            for u in users:
                    print(message.id, reaction, u, message.channel, 1)

EDIT:

This is the loop having an error for msg in message.channel.history: TypeError: 'method' object is not iterable

EDIT EDIT: This ended up being the solution after troubleshooting

    async def on_message(message):
            async for msg in message.channel.history(limit=None):  
                    users = [] 
                    for reaction in msg.reactions:
                        users = await reaction.users().flatten() 
                        for u in users:

Upvotes: 0

Views: 1164

Answers (1)

stijndcl
stijndcl

Reputation: 5647

The comments from another answer (which has now been removed) suggest that you're using a deprecated version of discord.py. You should update to 2.X, as 1.7 is no longer supported as of this month.

Your original error is caused by you not calling the method. channel.history is just a method reference, you have to actually call it.

In Discord.py 2.0, all async iterators (including Messageable.history()) were changed. .flatten() is no more.

Migration guide (explaining how to convert every old method into the new version): https://discordpy.readthedocs.io/en/stable/migrating.html#moving-away-from-custom-asynciterator

Docs for history (with two examples): https://discordpy.readthedocs.io/en/stable/api.html#discord.abc.Messageable.history

Note: the same thing goes for your reaction.users().flatten() a few lines later!!

Upvotes: 1

Related Questions