anyad lába
anyad lába

Reputation: 5

discord.py 1.7.3 - Send messages to a specific user id ( on_ready )

So I'm having the issue where I send a dm message (message var) to each user id on a list (memberlist var), and when I try to send it, I get an error saying: AttributeError: 'NoneType' object has no attribute 'send'

Code:

client = discord.Client()
    @client.event
    async def on_ready():
        print(f"\n{Color.GREEN} > {Color.WHITE} Logged in as: {client.user.name}")
        # send message to each member in memberlist
        for member in memberslist:
            print(member)       
            str(member)     
            user = client.get_user(member)
            await user.send(message)
            print(f"\n{Color.GREEN} > {Color.WHITE} Message sent to {client.get_user(member).name}")
        print(f"\n{Color.GREEN} > {Color.WHITE} Done!\n")

Upvotes: 0

Views: 723

Answers (2)

heroescreed
heroescreed

Reputation: 26

It would look like this, I have tested the code and it works. Do note that the bot needs to share a server with a user to be able to message them (plus the users dms need to be accepting messages from server members).

@client.event
async def on_ready():
  print('bot is ready')
  user = await client.fetch_user(userid)
  await user.send(message)

Upvotes: 0

Binary
Binary

Reputation: 11

The get_user method only looks through the members cache and, at the time of your get_user calls, your bot's members cache is empty thus returning Nones and leading you to this error.

What you can do instead is manually fetch the user using the fetch_user coroutine.

Upvotes: 1

Related Questions