My English is not good, I am still a newcomer, please forgive me if the question is not good. I'm using PyCord 2.3.2 to make a discord music robot. Currently, I only do the join command. Although I can join the channel, it throws an error.
Ignoring exception in command join:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 178, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\user\Desktop\BOT\Cogs\Music.py", line 52, in join
await channel.connect()
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\abc.py", line 1932, in connect
await voice.connect(timeout=timeout, reconnect=reconnect)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\voice_client.py", line 404, in connect
self.ws = await self.connect_websocket()
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\voice_client.py", line 375, in connect_websocket
await ws.poll_event()
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\gateway.py", line 944, in poll_event
await self.received_message(utils._from_json(msg.data))
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\gateway.py", line 861, in received_message
await self.initial_connection(data)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\gateway.py", line 898, in initial_connection
recv = await self.loop.sock_recv(state.socket, 70)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\asyncio\proactor_events.py", line 696, in sock_recv
return await self._proactor.recv(sock, n)
RuntimeError: Task <Task pending name='pycord: on_message' coro=<Client._run_event() running at C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py:377>> got Future <_OverlappedFuture pending overlapped=<pending, 0x183de095fc0>> attached to a different loop
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\bot.py", line 347, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 950, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\core.py", line 187, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: RuntimeError: Task <Task pending name='pycord: on_message' coro=<Client._run_event() running at C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\client.py:377>> got Future <_OverlappedFuture pending overlapped=<pending, 0x183de095fc0>> attached to a different loop
I tried to find the problem according to the error, but there is no good solution, hope to find a solution here.
this is my program Cogs:
import discord
from discord.ext import commands,tasks
import os
import youtube_dl
import asyncio
from definition.Classes import Cog_Extension
class Music(Cog_Extension):
@commands.command(name='join', help='Tells the bot to join the voice channel')
async def join(self,ctx):
if not ctx.message.author.voice:
await ctx.send("{} is not connected to a voice channel".format(ctx.message.author.name))
return
else:
channel = ctx.message.author.voice.channel
await channel.connect()
def setup(bot):
bot.add_cog(Music(bot))
Bot:
import discord
import asyncio
from discord.ext import commands
import os
import json
with open("config.json" , "r" ,encoding="utf8") as configFiles:
config = json.load(configFiles)
intents = discord.Intents.all()
bot = commands.Bot(command_prefix=config["command_prefix"], intents=intents)
@bot.event
async def on_ready():
os.system('clear')
print("-"*15)
print("🌐bot online")
print("-"*15)
print(bot.user.name)
print(bot.user.id)
print(bot.user)
print("-"*15)
Activity = discord.Activity(type=discord.ActivityType.watching,name="bot")
await bot.change_presence(activity=Activity)
await asyncio.sleep(3)
async def load():
for filename in os.listdir('./Cogs'):
if filename.endswith('.py'):
try:
bot.load_extension(f'Cogs.{filename[:-3]}')
print(f'✅ load {filename}')
except Exception as error:
print(f'❎ {filename} error {error}')
async def main():
await load()
await bot.start(config["Token"])
asyncio.run(main())
I think you could simplify your code a bit and that'll likely help:
def load():
# doesn't need to be async
for filename in os.listdir('./Cogs'):
if filename.endswith('.py'):
try:
bot.load_extension(f'Cogs.{filename[:-3]}')
print(f'✅ load {filename}')
except Exception as error:
print(f'❎ {filename} error {error}')
load()
bot.run(config["Token"])
Or, if you really want to use bot.start
then the docs recommend doing it this way:
def load():
# still doesn't need to be async
for filename in os.listdir('./Cogs'):
if filename.endswith('.py'):
try:
bot.load_extension(f'Cogs.{filename[:-3]}')
print(f'✅ load {filename}')
except Exception as error:
print(f'❎ {filename} error {error}')
load()
# for python3.11
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# loop = asyncio.get_event_loop()
try:
loop.run_until_complete(bot.start(config["Token"]))
except KeyboardInterrupt:
loop.run_until_complete(close())
# cancel all tasks lingering
finally:
loop.close()
I used the latter method, and copied your cog and my bot joined the VC I was in without any issues.