Discord.py - 重新启动命令错误 - RuntimeError:事件循环已关闭
Posted
技术标签:
【中文标题】Discord.py - 重新启动命令错误 - RuntimeError:事件循环已关闭【英文标题】:Discord.py - Restart Command Error - RuntimeError: Event loop is closed 【发布时间】:2021-04-27 10:49:07 【问题描述】:我目前正在开发一个不和谐的机器人。我正在处理重启命令。
我检查了 discord 是否有效,但出现此错误:
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 代码
【问题讨论】:
你会想要regenerate your bot's token。它已被编辑掉,但仍将保留在编辑修订版中。 忘记隐藏令牌。谢谢!刚刚重新生成了它 不应该是ctx.bot.login(...)
吗?
不,它不起作用
我编辑了代码,所以很清楚
【参考方案1】:
您可以使用discord.ext.commands.Bot.logout
和discord.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)
【讨论】:
不,它仍然是同样的东西,但它有点工作以上是关于Discord.py - 重新启动命令错误 - RuntimeError:事件循环已关闭的主要内容,如果未能解决你的问题,请参考以下文章