首页 > 解决方案 > Python Bot报错原因及解决方法

问题描述

我在共享主机中运行一个 python 机器人,它运行良好,但每天几个小时后,它会崩溃 1 或 2 次。那么是什么导致了这个错误以及如何解决这个问题。

RuntimeError: can't start new thread
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7f80a690b6d8>
Task exception was never retrieved
future: <Task finished coro=<WebSocketCommonProtocol.run() done, defined at /home/demotry/.local/lib/python3.6/site-packages/websockets/protocol.py:428> exception=ConnectionResetError(104, 'Connection reset by peer')>
Traceback (most recent call last):
  File "/home/demotry/.local/lib/python3.6/site-packages/websockets/protocol.py", line 434, in run
    msg = yield from self.read_message()
  File "/home/demotry/.local/lib/python3.6/site-packages/websockets/protocol.py", line 456, in read_message
    frame = yield from self.read_data_frame(max_size=self.max_size)
  File "/home/demotry/.local/lib/python3.6/site-packages/websockets/protocol.py", line 511, in read_data_frame
    frame = yield from self.read_frame(max_size)
  File "/home/demotry/.local/lib/python3.6/site-packages/websockets/protocol.py", line 546, in read_frame
    self.reader.readexactly, is_masked, max_size=max_size)
  File "/home/demotry/.local/lib/python3.6/site-packages/websockets/framing.py", line 86, in read_frame
    data = yield from reader(2)
  File "/home/demotry/.local/lib/python3.6/asyncio/streams.py", line 674, in readexactly
    yield from self._wait_for_data('readexactly')
  File "/home/demotry/.local/lib/python3.6/asyncio/streams.py", line 464, in _wait_for_data
    yield from self._waiter
  File "/home/demotry/.local/lib/python3.6/asyncio/selector_events.py", line 723, in _read_ready
    data = self._sock.recv(self.max_size)
ConnectionResetError: [Errno 104] Connection reset by peer
Task was destroyed but it is pending!
task: <Task pending coro=<WebSocketCommonProtocol.run() running at /home/demotry/.local/lib/python3.6/site-packages/websockets/protocol.py:434> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f80a6970f48>()]>>

我添加了我的 bot.py 脚本代码,如下所示,其中一些命令只有一个 bot.py,没有其他文件。一切正常,但一天崩溃 2 或 3 次。

token = "This is my Token" # This is what the bot uses to log into Discord.
prefix = "?" # This will be used at the start of commands.

import discord
from discord.ext import commands
from discord.ext.commands import Bot

bot = commands.Bot(command_prefix=prefix)
bot.remove_command("help")

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print(discord.__version__)
    print('------')

@bot.command(pass_context=True)
async def ping(ctx):
    msg = 'Pong {0.author.mention}'.format(ctx.message)
    await bot.say(msg)

@bot.command(pass_context=True)
async def hello(ctx):
    msg = 'hello... {0.author.mention}'.format(ctx.message)
    await bot.say(msg)

bot.run(token)

标签: pythonpython-3.xdiscorddiscord.py

解决方案


推荐阅读