python - 无法连接到主机。 Heroku 和 Discord 机器人问题

标签 python python-3.x discord.py

我正在使用 wavelink 使用 python 制作一个不和谐的音乐机器人。当我第一次在我的电脑上启动这个应用程序时它工作正常,但昨天我想将它部署到 heroku 但它不工作,所以我试图再次运行它,只是在我的电脑上,使用相同的代码现在它根本不起作用。我认为代码与第一次完全相同,但每次运行机器人时都会出现:

WEBSOCKET | Connection Failure:: Cannot connect to host 127.0.0.1:2333 ssl:default [Komputer zdalny odrzucił połączenie sieciowe] Traceback (most recent call last): File "C:\Python\lib\site-packages\aiohttp\connector.py", line 936, in _wrap_create_connection return await self._loop.create_connection(*args, **kwargs) # type: ignore # noqa File "C:\Python\lib\asyncio\base_events.py", line 1021, in create_connection raise exceptions[0] File "C:\Python\lib\asyncio\base_events.py", line 1006, in create_connection sock = await self._connect_sock( File "C:\Python\lib\asyncio\base_events.py", line 920, in _connect_sock await self.sock_connect(sock, address) File "C:\Python\lib\asyncio\proactor_events.py", line 702, in sock_connect return await self._proactor.connect(sock, address) File "C:\Python\lib\asyncio\windows_events.py", line 808, in _poll value = callback(transferred, key, ov) File "C:\Python\lib\asyncio\windows_events.py", line 595, in finish_connect ov.getresult() ConnectionRefusedError: [WinError 1225] Komputer zdalny odrzucił połączenie sieciowe

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

Traceback (most recent call last): File "C:\Python\lib\site-packages\wavelink\websocket.py", line 76, in _connect self._websocket = await self._node.session.ws_connect(uri, headers=self.headers, heartbeat=self._node.heartbeat) File "C:\Python\lib\site-packages\aiohttp\client.py", line 721, in _ws_connect resp = await self.request(method, url, File "C:\Python\lib\site-packages\aiohttp\client.py", line 480, in _request conn = await self._connector.connect( File "C:\Python\lib\site-packages\aiohttp\connector.py", line 523, in connect proto = await self._create_connection(req, traces, timeout) File "C:\Python\lib\site-packages\aiohttp\connector.py", line 858, in _create_connection _, proto = await self._create_direct_connection( File "C:\Python\lib\site-packages\aiohttp\connector.py", line 1004, in _create_direct_connection raise last_exc File "C:\Python\lib\site-packages\aiohttp\connector.py", line 980, in _create_direct_connection transp, proto = await self._wrap_create_connection( File "C:\Python\lib\site-packages\aiohttp\connector.py", line 943, in _wrap_create_connection raise client_error(req.connection_key, exc) from exc aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host 127.0.0.1:2333 ssl:default [Komputer zdalny odrzucił połączenie sieciowe]

我认为问题在于这部分代码:

async def start_nodes(self):
        await self.bot.wait_until_ready()

        # Initiate our nodes. For this example we will use one server.
        # Region should be a discord.py guild.region e.g sydney or us_central (Though this is not technically required)
        await self.bot.wavelink.initiate_node(host='127.0.0.1',
                                              port=2333,
                                              rest_uri='http://127.0.0.1:2333',
                                              password='youshallnotpass',
                                              identifier='TEST',
                                              region='europe')

文件 application.yml 看起来像这样:

server: # REST and WS server
  port: 2333
  address: 0.0.0.0
lavalink:
  server:
    password: "youshallnotpass"
    sources:
      youtube: true
      bandcamp: true
      soundcloud: true
      twitch: true
      vimeo: true
      mixer: true
      http: true
      local: false
    bufferDurationMs: 400
    youtubePlaylistLoadLimit: 6 # Number of pages at 100 each
    playerUpdateInterval: 5 # How frequently to send player updates to clients, in seconds
    youtubeSearchEnabled: true
    soundcloudSearchEnabled: true
    gc-warnings: true
    #ratelimit:
      #ipBlocks: ["1.0.0.0/8", "..."] # list of ip blocks
      #excludedIps: ["...", "..."] # ips which should be explicit excluded from usage by lavalink
      #strategy: "RotateOnBan" # RotateOnBan | LoadBalance | NanoSwitch | RotatingNanoSwitch
      #searchTriggersFail: true # Whether a search 429 should trigger marking the ip as failing
      #retryLimit: -1 # -1 = use default lavaplayer value | 0 = infinity | >0 = retry will happen this numbers times

metrics:
  prometheus:
    enabled: false
    endpoint: /metrics

sentry:
  dsn: ""
  environment: ""
#  tags:
#    some_key: some_value
#    another_key: another_value

logging:
  file:
    max-history: 30
    max-size: 1GB
  path: ./logs/

  level:
    root: INFO
    lavalink: INFO

你有什么想法吗?

解决方案:好的,如果您遇到与我相同的问题并且您正在使用 Lavalink,则必须首先启动 Lavalink 服务器。在我的例子中,我必须在终端中输入“java -jar Lavalink.jar”来启动服务器。

最佳答案

我遇到了同样的问题,后来我想通了

你需要设置 Lavalink

  1. 下载OpenJDK 13.0.2然后提取
  2. 下载Lavalink
  3. 将第一步中的 Lavalink.jar 粘贴到 bin 中
mv ./Lavalink.jar ./open-jdk/bin
  1. 创建一个新文件 application.yml 并粘贴 this code
    - 确保将 address: 0.0.0.0 更改为 address: 127.0.0.1
server: # REST and WS server
  port: 2333
  address: 127.0.0.1
lavalink:
  server:
    password: "youshallnotpass"
    sources:
      youtube: true
      bandcamp: true
      soundcloud: true
      twitch: true
      vimeo: true
      http: true
      local: false
    bufferDurationMs: 400
    youtubePlaylistLoadLimit: 6 # Number of pages at 100 each
    playerUpdateInterval: 5 # How frequently to send player updates to clients, in seconds
    youtubeSearchEnabled: true
    soundcloudSearchEnabled: true
    gc-warnings: true
    #ratelimit:
      #ipBlocks: ["1.0.0.0/8", "..."] # list of ip blocks
      #excludedIps: ["...", "..."] # ips which should be explicit excluded from usage by lavalink
      #strategy: "RotateOnBan" # RotateOnBan | LoadBalance | NanoSwitch | RotatingNanoSwitch
      #searchTriggersFail: true # Whether a search 429 should trigger marking the ip as failing
      #retryLimit: -1 # -1 = use default lavaplayer value | 0 = infinity | >0 = retry will happen this numbers times

metrics:
  prometheus:
    enabled: false
    endpoint: /metrics

sentry:
  dsn: ""
  environment: ""
#  tags:
#    some_key: some_value
#    another_key: another_value

logging:
  file:
    max-history: 30
    max-size: 1GB
  path: ./logs/

  level:
    root: INFO
    lavalink: INFO
  1. 转到命令提示符并执行以下命令
pip install discord.py[voice] wavelink
  1. 打开解压 JDK 的文件夹,然后键入
java -jar Lavalink.jar

这将启动 lavalink 服务器,然后您就可以使用您的机器人了


如需更多帮助,请查看 this video

关于python - 无法连接到主机。 Heroku 和 Discord 机器人问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64941372/

相关文章:

python - 如何获取 Selenium 中悬停/弹出框的文本?

python - maya 的宏工具

python - 如何在 python3 中将组织好的文件放入字典中?

python - 齿轮已加载,但功能不起作用(discord.py)

python - psycopg2 : cursor already closed

python - 无法解析 h4 标签内的数据 : Python3

python - 调用 object.method() 和 Class.method(object) 时幕后发生了什么?

Python 太慢,无法在 for 循环中的字符串中查找文本

python - 从消息中获取图片

python - discord.py - 发送文件而不在我的计算机上创建文件?