Search code examples
pythondiscorddiscord.pyipcpycord

pycord RuntimeError while trying to start ipc Discord Bot


I'm following this tutorial to create a discord bot with a dashboard but when I run my bot.py file I'm getting an error. What am I doing wrong?

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")

This is the error I get when running 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

Solution

  • So i found a solution and i wanted to help everyone who is stuck on this Problem too. :)

    Just simply remove the

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

    part and it should work.

    For more information you can read this issue, thats where i got the solution from and it worked for me. Hope i could help some of yall! >:)