'How to make a music bot join the voice call

So i have been seeing many youtube vids and got a working code but I cannot find the error in my code

import discord
from discord import Embed
from itertools import cycle
import os
import asyncio
import random
import json
import youtube_dl
import praw
from datetime import datetime as dt
from typing import Optional
from discord.ext import tasks,commands
from discord.ext.commands import Bot
from discord.utils import get
from keep_alive import keep_alive

def getprefix(bot,msg):
  return['d! ','d!','doge ','doge']


bot = commands.Bot(case_insensitive=True,command_prefix=getprefix)
bot.remove_command("help")

status = cycle(['Prefix=d!','Helping server','Prefix=doge'])

@bot.event
async def on_ready():
  print('on')
  change_status.start()

@tasks.loop(seconds=10)
async def change_status():
  await bot.change_presence(status=discord.Status.idle,activity=discord.Streaming(name=(next(status)), url='https://www.youtube.com/watch?v=iik25wqIuFo'))

@bot.command(aliases=['j','enter','connect'])
async def join(ctx):
    if ctx.author.voice is None:
      em=discord.Embed(title="ERROR",description=f"{ctx.author.mention} You are not in a voice channel!")
      await ctx.send(embed=em)
    voice_channel=ctx.author.voice.channel
    if ctx.voice_client is None:
      await voice_channel.connect()
      em=discord.Embed(description=f"Connected")
      await ctx.send(embed=em)
    else:
      await ctx.voice_client.move_to(voice_channel)
      em=discord.Embed(description=f"Reconnected")
      await ctx.send(embed=em)

@bot.command(aliases=['l','exit','leave'])
async def disconnect(ctx):
    await ctx.voice_client.disconnect()

@bot.command(aliases=['stop','halt','pau'])
async def pause(ctx):
    await ctx.voice_client.pause()
    em=discord.Embed(description=f"Paused! {ctx.author.name}")
    await ctx.send(embed=em)

@bot.command(aliases=['continue','res'])
async def resume(ctx):
    await ctx.voice_client.resume()
    em=discord.Embed(description=f"Resumed! {ctx.author.name}")
    await ctx.send(embed=em)

@bot.command(aliases=['run','display'])
async def play(ctx,url):
  ctx.voice_client.stop()
  FFMPEG_OPTIONS={'before_options':'-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5','options':'-vn'}
  YDL_OPTIONS={'format':'bestaudio'}
  vc=ctx.voice_client

  with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
    info=ydl.extract_info(url,download=False)
    url2=info['formats'][0]['url']
    source=await discord.FFmpegOpusAudio.from_probe(url2,**FFMPEG_OPTIONS)
    vc.play(source)





So if I am not in a voice channel the bot shows the error but when I am in the voice channel it doesn't join nor does it send any message. So I cannot figure out my mistake and I really don't know if the rest will work as the first step itself has gone wrong. Any help would be valuable



Solution 1:[1]

Try to replace

if ctx.voice_client is None:

with

voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
if voice == None:
    await voice_channel.connect()

Alternatively:

 async def join(self, ctx):
        member = utils.find(lambda m: m.id == ctx.author.id, ctx.guild.members)
        if member is not None and member.voice is not None:
            vc = member.voice.channel
            await vc.connect()
            print('Joined a Channel')

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