how to connect a discord bot through proxy

2019-07-29 12:34发布

问题:

I am trying to run a discord bot using discord.py and through a proxy. The discordpy doc on this is pretty scarce on the subject and not up to date with aiohttp implementation.

discordpy doc basically says to use a ProxyConnector and pass it as an argument when the client is created. But in aiohttp, this way is deprecated and client.ClientSession().get is recommended instead. Problem is, client.ClientSession().get asks me to provide a URL.

I also tried with ProxyConnector anyway, but it doesn't work when I finally run the bot (can't connect to the discord API). I'm not sure what's wrong with it, as the proxy itself works fine with any other HTTPS services.


Code with recommended way

conn = client.ClientSession().get(proxy='<proxy_url>', proxy_auth=BasicAuth(<proxy_auth>))

self.client = discord.Client(connector=conn)

Code with deprecated way

conn = ProxyConnector(proxy='<proxy_url>', proxy_auth=BasicAuth(<proxy_auth>))

self.client = discord.Client(connector=conn)

Traceback

Traceback (most recent call last):
  File "C:/Users/airiau/PycharmProjects/pronostics/main.py", line 50, in <module>
    main()
  File "C:/Users/airiau/PycharmProjects/pronostics/main.py", line 46, in main
    bot.run(config['token'])
  File "C:\Users\airiau\PycharmProjects\pronostics\sample\DiscordBot.py", line 352, in run
    self.client.run(self.token)
  File "C:\Users\airiau\venv-3.6\lib\site-packages\discord\client.py", line 519, in run
    self.loop.run_until_complete(self.start(*args, **kwargs))
  File "C:\Program Files (x86)\Python36-32\lib\asyncio\base_events.py", line 468, in run_until_complete
    return future.result()
  File "C:\Users\airiau\venv-3.6\lib\site-packages\discord\client.py", line 491, in start
    yield from self.connect()
  File "C:\Users\airiau\venv-3.6\lib\site-packages\discord\client.py", line 444, in connect
    self.ws = yield from DiscordWebSocket.from_client(self)
  File "C:\Users\airiau\venv-3.6\lib\site-packages\discord\gateway.py", line 207, in from_client
    timeout=60, loop=client.loop)
  File "C:\Program Files (x86)\Python36-32\lib\asyncio\tasks.py", line 358, in wait_for
    return fut.result()
  File "C:\Users\airiau\venv-3.6\lib\site-packages\discord\gateway.py", line 65, in _ensure_coroutine_connect
    ws = yield from websockets.connect(gateway, loop=loop, klass=klass)
  File "C:\Users\airiau\venv-3.6\lib\site-packages\websockets\py35\client.py", line 19, in __await__
    return (yield from self.client)
  File "C:\Users\airiau\venv-3.6\lib\site-packages\websockets\client.py", line 210, in connect
    factory, wsuri.host, wsuri.port, **kwds)
  File "C:\Program Files (x86)\Python36-32\lib\asyncio\base_events.py", line 787, in create_connection
    ', '.join(str(exc) for exc in exceptions)))
OSError: Multiple exceptions: [Errno 10060] Connect call failed ('104.16.59.37', 443), [Errno 10060] Connect call failed ('104.16.60.37', 443)

回答1:

From continuing research, I found this link with this answer:

It appears that WebSockets used by discord.py do not support HTTP proxies. This would just magically work with HTTPS, but since the proxy is HTTP it doesn't. That means that, short of rewriting discord.py with HTTP proxy support (by using websocket-client, for example, which supports HTTP proxies), we may be out of luck.

It looks like it might not be possible to do it altogether.