Search code examples
pythonpython-3.xdiscorddiscord.py

The bot does not join the voice channel


The bot refuses to connect to the voice channel, while there is no error in the console or anywhere, it seems to me that it just hangs on voice_clients = await msg.author.voic.channel.connect() please help to solve this.

import discord
import os
import asyncio
import youtube_dl
from discord import *

intents = discord.Intents.default()
intents.message_content = True
intents.voice_states = True

client = discord.Client(intents=intents)

key = "MTA3ODY3OTcxOTIxMTQ0MjI5Nw.Gqtubx.SYqVZ7dwl8U3MpIK5mSIfXOSIi2qychJ0iddko"

voice_clients = {}

yt_dl_opts = {'format': 'bestaudio/best'}
ytdl = youtube_dl.YoutubeDL(yt_dl_opts)

ffmpeg_options = {'options': "-vn"}  

@client.event
async def on_ready():
    print(f"GOTOVO KAPITAN{client.user}")


@client.event
async def on_massage(msg):
    if msg.content.startswith("?play"):
        try:
            voice_clients = await msg.author.voic.channel.connect() 
            voice_clients[voice_clients.guild.id] = voice_clients

            url = msg.content.split()[1]

            loop = asyncio.get_event_loop()
            data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=False) )

            song = data['url']
            player = discord.FFmpegPCMAudio(song, **ffmpeg_options, executable="C:\\ffmpeg\\bin\\ffmpeg.exe")

            voice_clients[msg.guild.id].play(player)

        except Exception as err:
            print(err)
        
    if msg.content.startswith("?pause"):
        try:
            voice_clients[msg.guild.id].pause()
        except Exception as err:
            print(err)

    if msg.content.startswith("?resume"):
        try:
            voice_clients[msg.guild.id].resume()
        except Exception as err:
            print(err)

    if msg.content.startswich("?stop"):
        try:
            voice_clients[msg.guild.id].stop()
            await voice_clients[msg.guild.id].disconnect()
        except Exception as err:
            print(err)

client.run(key)

I looked at quite a lot of similar situations on many forums, but I still did not understand why this happens and how to solve it, on the video from which I read this code, everything works out and I rewrote it several times. (I'm new to Python, so sorry if this is too easy a mistake)


Solution

  • It looks like you misspelled on_massage and I believe msg.author.voic.channel should be msg.author.voice.channel.