【发布时间】:2022-07-18 12:36:46
【问题描述】:
我正在关注this 教程来创建一个带有仪表板的不和谐机器人 但是当我运行我的 bot.py 文件时,我遇到了一个错误。我做错了什么?
bot.py:
import discord
from discord.ext import commands, ipc
class Bot(commands.Bot):
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
self.ipc = ipc.Server(self,secret_key = "test")
async def on_ready(self):
print("Bot is ready.")
async def on_ipc_ready(self):
print("Ipc server is ready.")
async def on_ipc_error(self, endpoint, error):
print(endpoint, "raised", error)
bot_client = Bot(command_prefix = "!", intents = discord.Intents.default())
@bot_client.ipc.route()
async def get_guild_count(data):
return len(my_bot.guilds) # returns the len of the guilds to the client
@bot_client.ipc.route()
async def get_guild_ids(data):
final = []
for guild in my_bot.guilds:
final.append(guild.id)
return final # returns the guild ids to the client
@bot_client.command()
async def hi(ctx):
await ctx.send("Hi")
bot_client.ipc.start()
bot_client.run("TOKEN")
这是我在运行bot.py时遇到的错误:
File "D:/PyCharm Projects/AiChat/bot.py", line 44, in <module>
bot_client.ipc.start()
File "D:\PyCharm Projects\AiChat\venv\lib\site-packages\discord\ext\ipc\server.py", line 253, in start
self.bot.dispatch("ipc_ready")
File "D:\PyCharm Projects\AiChat\venv\lib\site-packages\discord\bot.py", line 1281, in dispatch
super().dispatch(event_name, *args, **kwargs) # type: ignore
File "D:\PyCharm Projects\AiChat\venv\lib\site-packages\discord\client.py", line 440, in dispatch
self._schedule_event(coro, method, *args, **kwargs)
File "D:\PyCharm Projects\AiChat\venv\lib\site-packages\discord\client.py", line 400, in _schedule_event
return asyncio.create_task(wrapped, name=f"pycord: {event_name}")
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\asyncio\tasks.py", line 381, in create_task
loop = events.get_running_loop()
RuntimeError: no running event loop
sys:1: RuntimeWarning: coroutine 'Client._run_event' was never awaited
Process finished with exit code 1
【问题讨论】:
-
你用的是什么python版本?
-
@ŁukaszKwieciński 我正在使用 python 3.8
-
根据错误,这在我看来就像在某处缺少
await -
为什么特别需要使用ipc?您可以在机器人中创建一个简单的 http api,然后从您的仪表板网页中调用它。
标签: python discord discord.py ipc pycord