#Getting User Data with Pycord, Even If He Is Not A Member Of The Server Where The Bot Is Located

1 messages · Page 1 of 1 (latest)

frozen elm
#

I'm trying to find usernames via ids for a bot system. In other words, there are id's in the database and the system to access user names with these ids. For the get_user command, that user needs to be on a server with the bot, so it won't work. I'm not sure if the fetch_user command does this, but when I try I get an error like this:
discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: TypeError: Client.fetch_user() missing 1 required positional argument: 'user_id'

Finally, is there an API that I can use for this, like top.gg's but not requesting active bots? (Pycord compatible) Or something like the pycord version of the get_user feature of the discord GameSDK?

quasi girder
#

You are correct get_user will not work in all cases. But you should still try so that you can avoid so many API calls. Then fallback onto fetch_user. Could you show your code for this so I can debug the error

#

And can you also explain the API thing you want a bit more I do not fully understand

frozen elm
frozen elm
#
def get_blacklist_member_data(bot):
    search = member_blacklist.find({})
    member_ids = []
    for data in search:
        current_member_id = data["member_id"]
        member_ids.append(current_member_id)

    member_names = []
    x = 0
    while x<len(member_ids):
        current_member_name = commands.Bot.fetch_user(int(member_ids[x])) 
        member_names.append(current_member_name)
        x=x+1

    return member_ids, member_names
#

This is the function I used fetch_user

quasi girder
#

The main problem i see is that you are not awaiting it

frozen elm
#

so I must use an async function?

quasi girder
#

yes

#

I think you could do a asyncio.create_task(get_blacklist_member_data, bot) or something

frozen elm
#

I used this but it isn't worked

quasi girder
#

Was there a error?

frozen elm
#

oh

#

sorry

#

I didn't looked error 😅

#

Yes, it is a different problem

#

thanks

frozen elm
#

discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: TypeError: 'coroutine' object is not subscriptable

#

this error

frozen elm
#

I started function normally

quasi girder
#

Now you need to await get_blacklist_member_data(self.bot) Then you can subscript it.

#

Is this bit of code something that is needed 1 time or every restart or in a command

frozen elm
#

?

#

It is a part of a slash command

#

In a cog file

quasi girder
#

Can you show the full command

#

With the decorator

frozen elm
quasi girder
#

@discord.command or similar

frozen elm
#

Ehh, I can't understand. Sorry, I am not too experianced about coding bots

quasi girder
#

Change This

member_names = await get_blacklist_member_data(self.bot)[1]

To This

member_names = await get_blacklist_member_data(self.bot)
member_names[1]

And the other one too

frozen elm
#

okay

quasi girder
#

How I remember this is you need to wait for the function to return so you cannot access its return value right away

frozen elm
quasi girder
#

that should work

frozen elm
# quasi girder that should work

I guess it worked but this error started again:
discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: TypeError: Client.fetch_user() got some positional-only arguments passed as keyword arguments: 'user_id'

quasi girder
#

Try replacing commands.Bot with just bot (The varible that gets passed into the function)

frozen elm
#

discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: TypeError: Client.fetch_user() got some positional-only arguments passed as keyword arguments: 'user_id'

#

this again

#

current_member_name = await commands.Bot.fetch_user(user_id=int(member_ids[x]))

#

I added "user_id="

#

?

quasi girder
#

remove user_id= that is what the error is mad about
(Also sorry was helping someone else)

frozen elm
#

File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\commands\core.py", line 124, in wrapped
ret = await coro(arg)
^^^^^^^^^^^^^^^
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\commands\core.py", line 978, in _invoke
await self.callback(self.cog, ctx, **kwargs)
File "C:\Users\user\Desktop\AI-GPT\cogs\blacklist.py", line 144, in show_blacklist
await ctx.respond("List:")
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\commands\context.py", line 282, in respond
return await self.interaction.response.send_message(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\interactions.py", line 825, in send_message
await self._locked_response(
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\interactions.py", line 1090, in locked_response
await coro
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\webhook\async
.py", line 219, in request
raise NotFound(response, data)
discord.errors.NotFound: 404 Not Found (error code: 10062): Unknown interaction

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\bot.py", line 1114, in invoke_application_command
await ctx.command.invoke(ctx)
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\commands\core.py", line 375, in invoke
await injected(ctx)
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\commands\core.py", line 132, in wrapped
raise ApplicationCommandInvokeError(exc) from exc

#

discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: NotFound: 404 Not Found (error code: 10062): Unknown interaction

#

this error

quasi girder
#

Discord only allows 3 seconds for yoou to respond to a command. You can extend this to 15 min by using await ctx.defer() at the beginning of your command.

#

the bot cant find anything to respond to because discord deleted it after 3 seconds

#

You will have to change ctx.respond to ctx.followup.send_message as well

frozen elm
quasi girder
#

the very first line of the command callback

frozen elm
quasi girder
#
@commands.slash_command()
async def show_blacklist(self, ctx):
    await ctx.defer()
    rest of command code
frozen elm
#

Ehh, bot is thinking since 1 minute

quasi girder
#

Yeah, if you have a lot of members in the database it might take a long time

frozen elm
#

there is 20-25

quasi girder
#

hmmm. something is stuck then. I would add print statements to see where it is taking long

frozen elm
#

yes, it is still thinking

#

what should I do?

quasi girder
#

I would stop and add a bunch of print statements so you can see where it is stuck

frozen elm
#

hmmm

#

okay

#

error loaded now

#

I guess I maked a writing mistake

frozen elm
#

discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: AttributeError: 'Webhook' object has no attribute 'send_message'

quasi girder
#

it might not let me check

frozen elm
#

it is send

#

it is working now

quasi girder
#

👍

frozen elm
#

there is 20-25 id in database

#

but only 2 of them giving a username

#

from bot

#

only first 2 id working

#

then bot done working and give result

#

no error

quasi girder
#

I have to go. Can you post the updated code here and I can look at it later

frozen elm
#

sure

frozen elm
#

problem solved

#

it is a very simple problem

#

it isn't worked because member_ids have 2 list in it.

#

but

#

It is still slow

#

Youn know a way for making it faster?

quasi girder
# frozen elm Youn know a way for making it faster?

tl;dr Replace fetch_user with get_or_fetch_user.

What this does is it makes the bot look through its own small storage system to see if the it already knows that user. It is much faster then asking Discord but there is limited space so sometimes it will not find anyone. That is ok because built in to the get_or_fetch_user the same fetch_user function you have been using that is treated as a backup, but is generally slower.

Another thing that could be done is only get the first 10 items from the blacklist. And then get the next 10 when the user goes to the next page in the paginator.

My guess is that your (database?) member_blacklist call is very slow. I could look at that is you send it.

frozen elm
quasi girder
#

IT depends on how you access it

frozen elm
#

on py file

frozen elm
#

somewhere fetch_user is rereading the same ids.

#

Traceback (most recent call last):
File "C:\Users\user\Desktop\AI-GPT\main.py", line 32, in <module>
bot.run("TOKEN")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\client.py", line 704, in run
future = asyncio.ensure_future(runner(), loop=loop)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\asyncio\tasks.py", line 649, in ensure_future
return _ensure_future(coro_or_future, loop=loop)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\asyncio\tasks.py", line 670, in _ensure_future
return loop.create_task(coro_or_future)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 434, in create_task

#

@quasi girder

#

there is an error

#

now

#

normally this error is means shutdown of bot with ctrl+c I guess

frozen elm
#

okay problem is solved now

#

it is fast and page list problem is solved

#

thank you