ECONNREFUSED on all requests from discord bot repls

Question:
As the title states i have a discord bot and it was working fine before but is now not starting due to some error (did not change anything).

Repl link:
https://replit.com/@acerifleyt/StellFico-Bot

oh wait my ctrl key is broken i thought i pasted the error here, give me one moment

Welcome to the forums @acerifleyt !

Can you please provide the description of the error that you are getting when you try to run your code?

1 Like
 * Serving Flask app ''
2023-05-25 19:20:48 INFO     discord.client logging in using static token
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:8080
 * Running on http://172.31.196.20:8080
Press CTRL+C to quit
Traceback (most recent call last):
  File "/home/runner/StellFico-Bot/venv/lib/python3.10/site-packages/aiohttp/connector.py", line 980, in _wrap_create_connection
    return await self._loop.create_connection(*args, **kwargs)  # type: ignore[return-value]  # noqa
  File "/nix/store/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/asyncio/base_events.py", line 1067, in create_connection
    raise exceptions[0]
  File "/nix/store/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/asyncio/base_events.py", line 1052, in create_connection
    sock = await self._connect_sock(
  File "/nix/store/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/asyncio/base_events.py", line 963, in _connect_sock
    await self.sock_connect(sock, address)
  File "/nix/store/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/asyncio/selector_events.py", line 500, in sock_connect
    return await fut
  File "/nix/store/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/asyncio/selector_events.py", line 535, in _sock_connect_cb
    raise OSError(err, f'Connect call failed {address}')
ConnectionRefusedError: [Errno 111] Connect call failed ('162.159.137.232', 443)

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

Traceback (most recent call last):
  File "main.py", line 105, in <module>
    bot.run(os.getenv("BOT_TOKEN"))
  File "/home/runner/StellFico-Bot/venv/lib/python3.10/site-packages/discord/client.py", line 860, in run
    asyncio.run(runner())
  File "/nix/store/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/nix/store/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/home/runner/StellFico-Bot/venv/lib/python3.10/site-packages/discord/client.py", line 849, in runner
    await self.start(token, reconnect=reconnect)
  File "/home/runner/StellFico-Bot/venv/lib/python3.10/site-packages/discord/client.py", line 777, in start
    await self.login(token)
  File "/home/runner/StellFico-Bot/venv/lib/python3.10/site-packages/discord/client.py", line 612, in login
    data = await self.http.static_login(token)
  File "/home/runner/StellFico-Bot/venv/lib/python3.10/site-packages/discord/http.py", line 801, in static_login
    data = await self.request(Route('GET', '/users/@me'))
  File "/home/runner/StellFico-Bot/venv/lib/python3.10/site-packages/discord/http.py", line 624, in request
    async with self.__session.request(method, url, **kwargs) as response:
  File "/home/runner/StellFico-Bot/venv/lib/python3.10/site-packages/aiohttp/client.py", line 1141, in __aenter__
    self._resp = await self._coro
  File "/home/runner/StellFico-Bot/venv/lib/python3.10/site-packages/aiohttp/client.py", line 536, in _request
    conn = await self._connector.connect(
  File "/home/runner/StellFico-Bot/venv/lib/python3.10/site-packages/aiohttp/connector.py", line 540, in connect
    proto = await self._create_connection(req, traces, timeout)
  File "/home/runner/StellFico-Bot/venv/lib/python3.10/site-packages/aiohttp/connector.py", line 901, in _create_connection
    _, proto = await self._create_direct_connection(req, traces, timeout)
  File "/home/runner/StellFico-Bot/venv/lib/python3.10/site-packages/aiohttp/connector.py", line 1206, in _create_direct_connection
    raise last_exc
  File "/home/runner/StellFico-Bot/venv/lib/python3.10/site-packages/aiohttp/connector.py", line 1175, in _create_direct_connection
    transp, proto = await self._wrap_create_connection(
  File "/home/runner/StellFico-Bot/venv/lib/python3.10/site-packages/aiohttp/connector.py", line 988, in _wrap_create_connection
    raise client_error(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host discord.com:443 ssl:default [Connect call failed ('162.159.137.232', 443)]
1 Like

Seems like your bot is not connecting to the Discord API server (the error is ConnectionRefusedError)

This is mainly because the Discord could not validate your Token. You should check if the token has changed or if there is an issue with how it is being read from the environment variables.

It could be rate limit from Discord (If your bot makes too many requests in a short time, Discord might temporarily block it) but it’s most likely the token.

2 Likes

not the token, just reset it and still does not work, maybe rate limited then?

Try to import the logs of the connection. Discord.py has a built-in logging utility that can be used to provide more detailed information about what your bot is doing and if it encounters any errors or warnings.

import logging

logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)

You could also add print statements before the bot tries to connect to Discord to help you see if the bot token is being read correctly.

3 Likes

You can check their documentation to see how logging would fit more for you.
https://discordpy.readthedocs.io/en/stable/logging.html

1 Like

Discord.log says nothings wrong, thanks for trying tho.

For some reason Discord is refusing your connection. It might be rate limit so you could check with them (there’s information here Discord Developer Portal)

Or it might be that Discord is not reading (or reading wrong) your Token too, so try to print the token before connecting.

I have the same problem. Worked fine for weeks and a few hours ago it stopped working. When I run it, I get this error:

node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

[Error: connect ECONNREFUSED 162.159.138.232:443] {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '16.15.13.23',
  port: 443
}

Node.js v18.12.1

I get the same ECONNREFUSED error when trying to connect to anything other than Discordbot in the same repository.
I also called “npm search” from the Shell tab and the same thing happened here.

$ npm search express
npm ERR! code ECONNREFUSED
npm ERR! syscall connect
…
npm ERR!     at ClientRequest.<anonymous>…

Thank you for your response.
I tried to get more detailed information, but the situation was resolved now.
Both Discord Bots and npm connections are working fine.

I also remembered one more thing I had tried.
I tried @replit/database at the same time and that didn’t work either with the same error.
Sorry about this one, but I didn’t log it so I can’t provide more information.

1 Like

Nevermind. I did not do anything, not changed anything but it works now. I am happy it works now, even though I have no idea how this got fixed. >.<

I find this weird, I’m running a Minecraft/Discord bot that connects to both Discord and Minecraft as a relay chat and im not getting this error at all its connecting directly to discord and Minecraft

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

looks like there’s a second wave

I haven’t made new changes in a few days so I’m not sure why it’s randomly erroring out or what the issue is.

triggerUncaughtException(err, true /* fromPromise */);
          ^

[Error: connect ECONNREFUSED 162.159.136.232:443] {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '162.159.136.232',
  port: 443
}

to confirm that this issue is still occurring: do you also get errors when you send requests to anywhere else? For example, if you run in Shell:

curl --no-progress-meter -o/dev/null https://example.com

it shouldn’t show any output

1 Like

Thanks for the fast reply! Yes that gives me an error as well:

curl: (7) Failed to connect to example.com port 443 after 2 ms: Connection refused

2 Likes