Home > Software engineering >  Use pycord to make a music bot
Use pycord to make a music bot

Time:02-04

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("           
  • Related