Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

403 Forbidden in fetch_user() #744

Open
4 tasks done
EchoQuill opened this issue Oct 11, 2024 · 12 comments · May be fixed by #767
Open
4 tasks done

403 Forbidden in fetch_user() #744

EchoQuill opened this issue Oct 11, 2024 · 12 comments · May be fixed by #767
Labels
bug Something isn't working

Comments

@EchoQuill
Copy link

Summary

fetching dms failed.

Reproduction Steps

Running the code below, gave 403 error.

Code

async def on_ready(self):
        self.on_ready_done = False
        self.cmds = 1
        self.dm,self.cm = None,None
        if self.session is None:
            self.session = aiohttp.ClientSession()
        await asyncio.sleep(0.1)
        if webhookEnabled:
            await self.webhookSender("logging in", desc=f"logged in as {self.user.name}", colors=0x8334eb, img_url=self.user.avatar)
        self.cmds_cooldown = 0
        printBox(f'-Loaded {self.user.name}[*].'.center(console_width - 2 ),'bold purple on black' )
        listUserIds.append(self.user.id)
        await asyncio.sleep(0.12)
        try:
            self.cm = self.get_channel(self.channel_id)
        except Exception as e:
            print(e)
        try:
            #self.dm = await self.fetch_user(408785106942164992) #owo bot's id
            self.dm = await (await self.fetch_user(408785106942164992)).create_dm() #fixes dm closed issue
            print(self.dm)
        except Exception as e:
            print(e)

        if self.dm == None:
            print("channel disabled")

Expected Results

it is suppossed to fetch user with id "408785106942164992" then create a dm.

Actual Results

2024-10-11 10:51:06 DEBUG    discord.gateway Gateway event: {'t': 'THREAD_LIST_SYNC', 's': 5, 'op': 0, 'd': {'threads': [], 'most_recent_messages': [], 'guild_id': '1293590819198079071'}}.
2024-10-11 10:51:06 DEBUG    discord.gateway Parsing event THREAD_LIST_SYNC.
2024-10-11 10:51:06 DEBUG    discord.gateway Gateway event: {'t': 'THREAD_LIST_SYNC', 's': 6, 'op': 0, 'd': {'threads': [], 'most_recent_messages': [], 'guild_id': '1189557307596603442'}}.
2024-10-11 10:51:06 DEBUG    discord.gateway Parsing event THREAD_LIST_SYNC.
2024-10-11 10:51:06 DEBUG    discord.http GET https://discord.com/api/v9/users/408785106942164992 with None has returned 403.
403 Forbidden (error code: 40001): Unauthorised
channel disabled
2024-10-11 10:51:06 ERROR    discord.client Ignoring exception in on_ready
Traceback (most recent call last):
  File "/home/dimlight/Desktop/owo/uwu/myenv/lib/python3.12/site-packages/discord/client.py", line 637, in _run_event
    await coro(*args, **kwargs)
  File "/home/dimlight/Desktop/owo/lwl/uwu.py", line 1585, in on_ready
    self.list_channel = [self.channel_id, self.dm.id]
                                          ^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'id'
2024-10-11 10:51:46 DEBUG    discord.gateway Keeping websocket alive.
2024-10-11 10:51:46 DEBUG    discord.gateway Gateway event: {'t': None, 's': None, 'op': 11, 'd': None}.

System Information

- Python v3.12.3-final
- discord.py-self v2.1.0-alpha
    - discord.py-self metadata: v2.1.0a4857+g0471428f
- aiohttp v3.10.9
- system info: Linux 6.8.0-45-generic #45-Ubuntu SMP PREEMPT_DYNAMIC Fri Aug 30 12:02:04 UTC 2024

Checklist

  • I have confirmed I am using unmodified discord.py-self and not the upstream discord.py.
  • I have searched the open issues for duplicates.
  • I have shared the entire traceback.
  • I am using a user token (and it isn't visible in the code).

Additional Information

This code was working fine yesterday and other users seems to be also having this issue today. Iam new to posting issues, so do be understanding of any mistakes in this and feel free to ask if i need to provide any additional information
(and yes the bot's dm we are trying to fetch and create dm is accessable, i even dmed the bot manually and tried running the code to see if that fixed the issue.)

Note: the issue here is with fetching the channel, not creating the dm
i have tried replacing
self.dm = await (await self.fetch_user(408785106942164992)).create_dm()
with
self.dm = await self.fetch_user(408785106942164992)
still same result...
Thanks!

@EchoQuill EchoQuill added the unconfirmed bug Unconfirmed bug label Oct 11, 2024
@EchoQuill
Copy link
Author

is this perhaps related to #500 ..?

EchoQuill added a commit to EchoQuill/owo-dusk that referenced this issue Oct 11, 2024
@blusewill
Copy link

blusewill commented Oct 11, 2024

I have also run into this issue. I can run it on my main PC but I can't do it on the server I rented. So weird.

Log :

2024-10-12 02:05:00,805 - INFO - Today's Quotes: 遇到難題就慢慢來,解決它的過程也是成長的一部分汪 (๑´ㅂ`๑) --機器狼GPT
2024-10-12 02:05:00,805 - INFO - Today's Server Quotes: 托托哥哥最信任的就是你們的力量,一起迎接勝利汪 (^▽^) --機器狼GPT
2024-10-12 02:05:00,831 - INFO - New day detected. Updating count to 9.
2024-10-12 02:05:00,831 - INFO - Count updated: 9 days.
2024-10-12 02:05:00,936 - ERROR - Ignoring exception in on_ready
Traceback (most recent call last):
  File "/home/container/.local/lib/python3.12/site-packages/discord/client.py", line 507, in _run_event
    await coro(*args, **kwargs)
  File "/home/container/run.py", line 96, in on_ready
    user = await client.fetch_user(user_id)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/container/.local/lib/python3.12/site-packages/discord/client.py", line 2161, in fetch_user
    data = await self.http.get_user(user_id)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/container/.local/lib/python3.12/site-packages/discord/http.py", line 830, in request
    raise Forbidden(response, data)
discord.errors.Forbidden: 403 Forbidden (error code: 40001): Unauthorized

Code :

@client.event
async def on_ready():
    logging.info(f'Logged in as {client.user}')

    # Load and log quotes
    quotes = open("quotes.txt").read().splitlines()
    random_quotes = random.choice(quotes)
    logging.info(f"Today's Quotes: {random_quotes}")

    quotes_server = open("quotes_server.txt").read().splitlines()
    random_quotes_server = random.choice(quotes_server)
    logging.info(f"Today's Server Quotes: {random_quotes_server}")

    taipei_tz = pytz.timezone('Asia/Taipei')
    current_date = datetime.datetime.now(taipei_tz).date()

    # Check if today is not Saturday or Sunday
    if current_date.weekday() < 7:  # Monday is 0, Sunday is 6
        file_path = 'day_count.txt'
        if os.path.exists(file_path):
            with open(file_path, 'r') as file:
                content = file.read().split(',')
                last_date = datetime.datetime.strptime(content[0], '%Y-%m-%d').date()
            if current_date == last_date:
                logging.info("Same day detected, shutting down the server.")
                shutdown_server()

        # Update the day count
        day_count = update_count()

    # Send messages to user and server
    user = await client.fetch_user(user_id)
    server_channel = await client.fetch_channel(channel_id)
    
    logging.info(f"Sending private message to user {user_id}.")
    await user.send(f"\u6258\u6258\u4e0a\u73ed\u4e86\uff01\u4eca\u5929\u662f\u4f60\u7b2c {day_count} \u5929\u5de5\u4f5c\u4e86\u3002")
    time.sleep(2)
    await user.send(f"{random_quotes}")
    time.sleep(2)
    await user.send(f"`\u6258\u6258\u4e0a\u73ed\u547c\u53eb\u5668 2024/10/12`")
    
    logging.info(f"Sending message to server channel {channel_id}.")
    await server_channel.send(f"\u5927\u5bb6\u65e9\u4e0a\u597d\uff01\u6e96\u5099\u597d\u8981\u4e0a\u73ed\u4e86\u55ce\uff1f")
    time.sleep(2)
    await server_channel.send(f"\u4f86\u8b93\u4eca\u5929\u7684\u91d1\u53e5\u6fc0\u52f5\u5927\u5bb6\u5427\uff01")
    time.sleep(2)
    await server_channel.send(f"{random_quotes_server}")
    time.sleep(2)
    await server_channel.send(f"`\u6258\u6258\u4e0a\u73ed\u547c\u53eb\u5668 2024/10/12`")
    
    logging.info("Shutting down server after message delivery.")
    shutdown_server()

client.run(token)

@notawfdx
Copy link

i have the same error
i have tried fetching multiple different users
i have tried fetching from multiple different accounts
nothing is working so far
i didnt have this problem yesterday but today my code is no longer working

@dolfies
Copy link
Owner

dolfies commented Oct 12, 2024

Smells like more TLS fingerprinting. Can you check with the refactor/tls branch?

@EchoQuill
Copy link
Author

Smells like more TLS fingerprinting. Can you check with the refactor/tls branch?

I cant seem to run it sadly. I am not smart enough to figure out what I am doing wrong here.
( installed with pip install git+https://github.com/dolfies/discord.py-self.git@refactor/tls )
sorry!

2024-10-13 09:24:15 INFO     discord.http Found user agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36", build number 335184.
Exception in thread Thread-1 (run_bot):
Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1073, in _bootstrap_inner
    self.run()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1010, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Admin\Desktop\New folder\owo-dusk\uwu.py", line 2746, in run_bot
    client.run(token, log_level=logging.DEBUG)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\client.py", line 1051, in run
    asyncio.run(runner())
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\client.py", line 1040, in runner
    await self.start(token, reconnect=reconnect)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\client.py", line 970, in start
    await self.login(token)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\client.py", line 812, in login
    data = await state.http.static_login(token.strip())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\http.py", line 1078, in static_login
    data = await self.get_me()
           ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\http.py", line 709, in request
    await self.startup()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\http.py", line 644, in startup
    if not requests.BrowserType.has(impersonate):
           ^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: type object 'BrowserType' has no attribute 'has'```

@fortnite-flopper
Copy link

Smells like more TLS fingerprinting. Can you check with the refactor/tls branch?

This fixes it for me.

I cant seem to run it sadly. I am not smart enough to figure out what I am doing wrong here. ( installed with pip install git+https://github.com/dolfies/discord.py-self.git@refactor/tls ) sorry!

Doesn't work with curl_cffi>=0.7.2. You can downgrade with pip install curl_cffi==0.6.0b7 (oldest version mentioned in requirements.txt)

@dolfies
Copy link
Owner

dolfies commented Oct 19, 2024

Sounds good, I'll make that branch a priority when I have time

@dolfies dolfies added bug Something isn't working and removed unconfirmed bug Unconfirmed bug labels Oct 19, 2024
@EchoQuill
Copy link
Author

Doesn't work with curl_cffi>=0.7.2. You can downgrade with pip install curl_cffi==0.6.0b7 (oldest version mentioned in requirements.txt)

Thanks, it worked!

Smells like more TLS fingerprinting. Can you check with the refactor/tls branch?

Fixes it for me as well.

@EchoQuill
Copy link
Author

now get_channel() also gives the same error sadly.

@dolfies
Copy link
Owner

dolfies commented Nov 1, 2024

That doesn't make sense, that just looks up from cache

@EchoQuill
Copy link
Author

That doesn't make sense, that just looks up from cache

sorry just checked! my bad, while trying to merge two folder, i accidentally switched up file containing the channel id with one with old channel id. (since my code attempted to fetch dms as well with try-except to handle that error, i thought it was get_channel() that was giving the error(channel fetched was none, causing the try-except to not work as intended)).

sorry again! Is working just fine.

@hotapmfhypo
Copy link

hotapmfhypo commented Nov 17, 2024

pip uninstall discord.py-self then pip install git+https://github.com/dolfies/discord.py-self.git@refactor/tls worked to solve the 403 error for me on fetch_user()

@dolfies dolfies linked a pull request Dec 2, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants