Discord.py - 重新启动命令错误 - 运行时错误:事件循环已关闭

时间:2021-01-22 23:33:26

标签: python python-3.x discord.py

我目前正在开发一个不和谐的机器人。我正在处理重启命令。

我检查了不和谐以查看它是否有效,但出现此错误:

Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x00000245FCC9C280>
Traceback (most recent call last):
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 746, in call_soon
    self._check_closed()
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x00000245FD9BB100>
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x00000245FCC9C280>
Traceback (most recent call last):
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 116, in __del__
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python39\lib\asyncio\proactor_events.py", line 108, in close
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 746, in call_soon
  File "C:\Users\Jerry\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 510, in _check_closed
RuntimeError: Event loop is closed

我的代码

import discord
import asyncio
from discord.ext import commands

client = commands.Bot(command_prefix='>')
client.remove_command('help')

@client.event
async def on_ready():
    print('Ready')

@client.event
async def on_connect():

    Online = discord.Embed(title='Online', color=discord.Color.green())
    Online.set_author(name="Bot Status")

@client.event
async def on_disconnect():

    Offline = discord.Embed(title='Offline', color=discord.Color.red())
    Offline.set_author(name="Bot Status")

@client.command()
async def clear(ctx, amount=9999):
    await ctx.send("Fetching messages...", delete_after=2.0)
    await asyncio.sleep(2.0)
    await ctx.send(content="Deleting messages...", delete_after=2.0)
    await asyncio.sleep(2)
    await ctx.channel.purge(limit=amount + 1) 
    await ctx.send(f"Done! {amount} message(s) deleted.", delete_after=10.0) 
    return

@client.command()
async def restart(ctx):
    await ctx.send('Restarting...', delete_after=4.0)
    await ctx.message.add_reaction('?')
    await ctx.Bot.logout()
    await client.login("Token", bot=True)
    await ctx.send('Done!', delete_after=5.0)

client.run('Token', bot=True, reconnect=True)

我改变了它,但每次都是一样的。那么有人可以帮我弄清楚吗?感谢您的所有回答!

我使用 Python 3.9.1 VS代码

1 个答案:

答案 0 :(得分:0)

您可以使用 discord.ext.commands.Bot.logoutdiscord.ext.commands.Bot.login

from discord.ext import commands

@client.command()
@commands.is_owner()
async def restart(ctx):
    await ctx.bot.logout()
    await client.login("your_token", bot=True)