Forums

Discord Bot problems

Hey I'm trying to host here 24h/24h a python bot for the discussion service Discord. It works on My PC. I've seen a topic in the firum talking about that so I added all the stuff about proxies. Now I still have this errors :

    Traceback (most recent call last):
  File "Bot.py", line 4, in <module>
    proxyc = aiohttp.ProxyConnector("http://proxy.server:3128")
NameError: name 'aiohttp' is not defined
17:44 ~ $ python3 Bot.py
Traceback (most recent call last):
  File "/usr/lib/python3.5/asyncio/base_events.py", line 682, in create_connection
    yield from self.sock_connect(sock, address)
  File "/usr/lib/python3.5/asyncio/futures.py", line 361, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.5/asyncio/tasks.py", line 296, in _wakeup
    future.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
    raise self._exception
  File "/usr/lib/python3.5/asyncio/selector_events.py", line 439, in _sock_connect_cb
    raise OSError(err, 'Connect call failed %s' % (address,))
ConnectionRefusedError: [Errno 111] Connect call failed ('104.16.60.37', 443)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "Bot.py", line 78, in <module>
    client.run('MjE4OTU4OTMxMTk4NzM4NDQy.CqKxDg.raJ-dZ9A6KI-Z2kday33Y1RTYWE')
  File "/home/Pyrrha/.local/lib/python3.5/site-packages/discord/client.py", line 487, in run
    self.loop.run_until_complete(self.start(*args, **kwargs))
  File "/usr/lib/python3.5/asyncio/base_events.py", line 387, in run_until_complete
    return future.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 274, in result
    raise self._exception
  File "/usr/lib/python3.5/asyncio/tasks.py", line 241, in _step
    result = coro.throw(exc)
  File "/home/Pyrrha/.local/lib/python3.5/site-packages/discord/client.py", line 459, in start
    yield from self.connect()
  File "/home/Pyrrha/.local/lib/python3.5/site-packages/discord/client.py", line 412, in connect
    self.ws = yield from DiscordWebSocket.from_client(self)
  File "/home/Pyrrha/.local/lib/python3.5/site-packages/discord/gateway.py", line 174, in from_client
    ws = yield from websockets.connect(gateway, loop=client.loop, klass=cls)
  File "/home/Pyrrha/.local/lib/python3.5/site-packages/websockets/py35/client.py", line 19, in __await__
    return (yield from self.client)
  File "/home/Pyrrha/.local/lib/python3.5/site-packages/websockets/client.py", line 150, in connect
    factory, wsuri.host, wsuri.port, **kwds)
  File "/usr/lib/python3.5/asyncio/base_events.py", line 704, in create_connection
    ', '.join(str(exc) for exc in exceptions)))
OSError: Multiple exceptions: [Errno 111] Connect call failed ('104.16.59.37', 443), [Errno 111] Connect call failed ('104.16.60.37', 443)
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7f098867f748>

If there is no way to fix that : Is there another service which can make it work? Thx

[edited by admin: formatting]

It is because external internet access for free users are restricted to a whitelist.

We are happy to add any public api's to the whitelist if you provide us with the api's documentation showing the official api endpoint.

Otherwise, you will have to upgrade your account.

https://discordpy.readthedocs.io/en/latest/ Here's the api doc ;)

discordapp.com is already on the whitelist, but only the http API will work on PythonAnywhere, not the websockets API.

Is github okay? https://github.com/hammerandchisel/discord-api-docs

You need to use discord over their http api and not web sockets if you are a free user. The whitelist stuff for free users only pertain to http/https.

pls how do i use it over their http api and not web sockets? i do not understand pls

pls how do i use it over their http api and not web sockets? i do not understand pls

I have the same problem, and the same question :

pls how do i use it over their http api and not web sockets? i do not understand pls

I'm sorry, none of us that works here have ever used the discord api, and their docs don't make it clear whether the HTTP API is complete, or can be used instead of the websockets API. If anyone else does manage to get something working, hopefully they'll post something here?

PS - paying users have unrestricted internet, so would be able to use the normal/websockets/gateway API

I think we can only use the web socket, because the api use gateway (https://discordapp.com/developers/docs/topics/gateway)

i think i have seen some free account users have been able to setup a discord bot that hits specifically http/https api endpoints, but we don't have any examples of that readily available. you may want to investigate yourself. alternatively I also think that many of our users have upgraded anyways to avoid the whole only http and no websocket problem.

Hello, Sawn, discord.py requires a WebSocket to connect to the Discord API server. Like what Conrad have stated, it is not possible at this time to do so. However, if you are willing to try something new, I recommend Disco. Disco is made by a Discord Staff (b1nzy) and has support to work without needing an active WebSocket connection. This library works in both Python 2 and Python 3! Here is a little test where I create a client object and played with sending a message to the guild channel.

endendragon@ubuntuVM:~$ python3.5
Python 3.5.2 (default, Jul 17 2016, 00:00:00) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from disco.api.client import APIClient
>>> client = APIClient("BOT_TOKEN")
>>> client
<disco.api.client.APIClient object at 0x7f9b1f6ec208>
>>> client.channels_get(140252024666062848)
<Channel 140252024666062848 (#general)>
>>> client.channels_messages_create(140252024666062848, "No Websockets required!")
<disco.types.message.Message object at 0x7f9b20c42788>
>>>

But keep in mind that it is pure HTTPS REST API and no connections to WebSockets are made. Therefore, your bot will stay perpetually offline and not able to receive any events that are normally sent over WebSockets (Message Create, Member Join, etc). But don't fret, you can always work around it by having a loop that checks for new messages over the REST and handles it accordingly.

If you are going on this path, please remember to respect Discord's API rate limits rules and such. Failure to do so and you will be reprimanded personally by a Discord staff member (happened to me, once).

Hope this helps!


Oh one more thing... Your original post contains your bot token. Please remove it from your post or someone can ruin your Discord guild with this. Why? This is essentially the password to your bot. You don't want to give away your passwords, don't you?

good catch! thanks for the helpful post EnderDragon!