Monday 16 November 2020

commands of the music bot discord.py

When I try to run the leave and play command I get this error. I've been looking for some questions for a long time but I haven't found this error anywhere how can I fix it? Error play:

Ignoring exception in on_voice_state_update
Traceback (most recent call last):
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "C:/Users/PC GIUSEPPE/PycharmProjects/LMIIBot Development/LMIIBot Development.py", line 32, in on_voice_state_update
    if client.user in prev.channel.members and len([m for m in prev.channel.members if not m.bot]) == 0:
AttributeError: 'NoneType' object has no attribute 'members'
Task exception was never retrieved
future: <Task finished name='Task-35' coro=<VoiceClient._create_socket() done, defined at C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\voice_client.py:172> exception=gaierror(11001, 'getaddrinfo failed')>
Traceback (most recent call last):
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\voice_client.py", line 191, in _create_socket
    self.endpoint_ip = socket.gethostbyname(self.endpoint)
socket.gaierror: [Errno 11001] getaddrinfo failed

Errors on_voice_stat_update

Ignoring exception in on_voice_state_update
Traceback (most recent call last):
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "C:/Users/PC GIUSEPPE/PycharmProjects/LMIIBot Development/LMIIBot Development.py", line 21, in on_voice_state_update
    if client.user in prev.channel.members and len([m for m in prev.channel.members if not m.bot]) == 0:
AttributeError: 'NoneType' object has no attribute 'members'
Task exception was never retrieved
future: <Task finished name='Task-19' coro=<VoiceClient._create_socket() done, defined at C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\voice_client.py:172> exception=gaierror(11001, 'getaddrinfo failed')>
Traceback (most recent call last):
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\voice_client.py", line 191, in _create_socket
    self.endpoint_ip = socket.gethostbyname(self.endpoint)
socket.gaierror: [Errno 11001] getaddrinfo failed
Ignoring exception in on_voice_state_update
Traceback (most recent call last):
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "C:/Users/PC GIUSEPPE/PycharmProjects/LMIIBot Development/LMIIBot Development.py", line 23, in on_voice_state_update
    await channel.disconnect()
AttributeError: 'NoneType' object has no attribute 'disconnect'

Error leave command

Task exception was never retrieved
future: <Task finished name='Task-16' coro=<VoiceClient._create_socket() done, defined at C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\voice_client.py:172> exception=gaierror(11001, 'getaddrinfo failed')>
Traceback (most recent call last):
  File "C:\Users\PC GIUSEPPE\PycharmProjects\LMIIBot Development\venv\lib\site-packages\discord\voice_client.py", line 191, in _create_socket
    self.endpoint_ip = socket.gethostbyname(self.endpoint)
socket.gaierror: [Errno 11001] getaddrinfo failed

These mistakes are new to me and therefore I have a lot of difficulty understanding them ...

Code:

import discord
import youtube_dl
import random
import asyncio
from discord import client
from discord.ext import commands
from discord.utils import get
from random import choice

token = 'token'
client = commands.Bot(command_prefix='°')

players = {}

@client.event
async def on_ready():
    print('Bot online')

@client.event
async def on_voice_state_update(member, prev, cur):
    if client.user in prev.channel.members and len([m for m in prev.channel.members if not m.bot]) == 0:
        channel = discord.utils.get(client.voice_clients, channel=prev.channel)
        await channel.disconnect()

@client.command()
async def join(ctx):
    v_channel = ctx.message.author.voice.channel
    await v_channel.connect()

@client.command()
async def leave(ctx):
    player = ctx.message.guild.voice_client
    await player.disconnect()

ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192',
    }],
}

def endSong(guild, path):
    os.remove(path)

@client.command(pass_context=True)
async def play(ctx, url):
    if not ctx.message.author.voice:
        await ctx.send('You are not connected to a voice channel') #message when you are not connected to any voice channel
        return

    else:
        channel = ctx.message.author.voice.channel

    voice_client = await channel.connect()

    guild = ctx.message.guild

    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        file = ydl.extract_info(url, download=True)
        path = str(file['title']) + "-" + str(file['id'] + ".mp3")

    voice_client.play(discord.FFmpegPCMAudio(path), after=lambda x: endSong(guild, path))
    voice_client.source = discord.PCMVolumeTransformer(voice_client.source, 1)

    await ctx.send(f'**Music: **{url}') #sends info about song playing right now

client.run(token)


from commands of the music bot discord.py

No comments:

Post a Comment