Discord.py send friend requests

Question:

Wondering how to send friend-requests with id.txt as infile.
My code looks like this but does not work

@client.event
async def on_ready():
    with open("id.txt") as infile:
        for line in infile:
            id = int(line)
            user = await client.get_user_info(id)
                try:
                    time.sleep(.305)
                    await user.send.friend_request
                except (discord.Forbidden, discord.HTTPException):
                    continue
Asked By: Una Tripolla

||

Answers:

The correct coroutine is User.send_friend_request, which you have to call.

from asyncio import sleep

@client.event
async def on_ready():
    with open("id.txt") as infile:
        for line in infile:
            id = int(line)
            user = await client.get_user_info(id)
            try:
                await sleep(.305)
                await user.send_friend_request()
            except (discord.Forbidden, discord.HTTPException):
                continue

You should also use asyncio.sleep to avoid unnecessary blocking.

Answered By: Patrick Haugh
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.