【问题标题】:pycord RuntimeError while trying to start ipc Discord Bot尝试启动 ipc Discord Bot 时出现 pycord RuntimeError
【发布时间】: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
  • github.com/Ext-Creators/discord-ext-ipc 该项目不再维护。
  • 根据错误,这在我看来就像在某处缺少await
  • 为什么特别需要使用ipc?您可以在机器人中创建一个简单的 http api,然后从您的仪表板网页中调用它。

标签: python discord discord.py ipc pycord


【解决方案1】:

所以看起来你有bot_client.ipc.start()

有比这更简单的方法,不需要ipccommands.Botevent 听众有更好的方法:

import discord
from discord import commands
bot_client = commands.Bot(command_prefix="!", intents=discord.Intents.All())

guildCount = 0
guildIds = []
@bot_client.event
async def on_ready():
    guildCount = len(bot_client.guilds)
    for guild in bot_client.guilds:
        guildIds.append(guild.id)

您不需要为此使用ipc.start()。事实上,你根本不需要ipc

还有更多的事件监听器,你可以看看in the Discord API reference. :)

希望这对你有用。

【讨论】:

  • Intents.All() 可能是不必要的,但我喜欢启用它们,以便我的机器人获得正常运行所需的权限而不会出错。
  • 除了拼写错误 :) 没有任何问题,但是您还需要在开发人员门户中定义额外的意图。如果您不想使用它,您可能不应该添加意图,因为隐私。
【解决方案2】:

我看了你提供的 youtube 视频,它似乎已经过时了(1 岁很多,尝试使用 wocs 代替,即使它可能更难)。我从未见过使用 ipc 并且您可以调用自己的函数,而不管 pycord 是异步的。只有当你需要在你自己的函数中等待一个函数时,才在你的函数之前使用 async 关键字并等待它本身。

【讨论】:

    【解决方案3】:

    所以我找到了解决方案,我也想帮助遇到这个问题的每个人。 :)

    只需删除

    async def on_ipc_ready(self):
            print("Ipc server is ready.")
    

    部分,它应该可以工作。

    有关更多信息,您可以read this issue,这就是我从中获得解决方案的地方,它对我有用。希望我能帮助你们! >:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-29
      • 2018-05-14
      • 1970-01-01
      • 2018-05-23
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 2021-11-04
      相关资源
      最近更新 更多