'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 1:[1]
So it looks like you have bot_client.ipc.start()
.
There's an easier way than that, without needing ipc
. commands.Bot
has a better way to do that with event
listeners:
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)
You don't need to use ipc.start()
for this one. In fact, you don't need ipc
at all for this one.
There are many more event listeners, you can check them out in the Discord API reference. :)
Hope this works for you.
Solution 2:[2]
I took a look at the youtube video you provided and it seems to be outdated (1year old is a lot, try using the wocs instead even tho its arguably harder). I've never seen ipc in use and you can call your own functions regardless of pycord being asychronous. Only if you need to await a function in your own function, use the async keyword before your function and await it itself.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Chill and Char |
Solution 2 | NoBlockhit |