W
W
wasaabii2020-09-16 11:37:22
Python
wasaabii, 2020-09-16 11:37:22

Discord.py audio bot error. How to fix?

Code below, I hid the token

import discord
from discord.ext import commands
from discord.utils import get
import youtube_dl
import os

TOKEN = '****'
BOT_PREFIX = '!' #!join

bot = commands.Bot(command_prefix=BOT_PREFIX)

@bot.event
async def on_ready():
    print('Logged in as: ' + bot.user.name + '\n')

@bot.command(pass_context=True, aliases=['j', 'joi'])
async def join(ctx):
    global voice
    channel = ctx.message.author.voice.channel
    voice = get(bot.voice_clients, guild = ctx.guild)

    if voice and voice.is_connected():
        await voice.move_to(channel)
    else:
        voice = await channel.connect()

    await voice.disconnect()

    if voice and voice.is_connected():
        await voice.move_to(channel)
    else:
        voice = await channel.connect()
        print(f"The bot has connected to {channel}\n")

    await ctx.send(f'Joined {channel}')


@bot.command(pass_context=True, aliases=['l', 'lea'])
async def leave(ctx):
    channel = ctx.message.author.voice.channel
    voice = get(bot.voice_clients, guild = ctx.guild)

    if voice and voice.is_connected():
        await voice.disconnect()
        print(f'The bot has left {channel}')
        await ctx.send(f'Left {channel}')
    else:
        print('Bot was told to leave voice channel, but was not in one')
        await ctx.send("Don't think I am in a voice channel")

@bot.command(pass_context=True, aliases=['p', 'pla'])
async def play(ctx, url: str):
    song_there = os.path.isfile('song.mp3')
    try:
        if song_there:
            os.remove('song.mp3')
            print('Removed old song file')
    except PermissionError:
        print('Trying to delete song file, but it is being played')
        await ctx.send('ERROR: Music playing')
        return

    await ctx.send('Getting everything ready now')

    voice = get(bot.voice_clients, guild=ctx.guild)

    ydl_opts={
        'format': 'bestaudio/best',
        'postproccesors': [{
            'key': 'FFmpegExtractAudio',
            'prefferedcodec': 'mp3',
            'prefferedquality': '192',
        }],
    }

    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        print('Downloading audio now\n')
        ydl.download([url])

    for file in os.listdir('./'):
        if file.endswith('.mp3'):
            name = file
            print(f'Renamed File: {file}\n')
            os.rename(file,'song.mp3')

    voice.play(discord.FFmpegPCMAudio('song.mp3'), after=lambda e: print(f'{name} has finished playing'))
    voice.source = discord.PCMVolumeTransformer(voice.source)
    voice.source.volume = 0.07

    nname = name.rsplit('-', 2)
    await ctx.send(f'Playing: {nname}')
    print('playing\n')

bot.run(TOKEN)

Gives an error message:
File "C:\Users\Obama\Desktop\server2\music2", line 86, in play
    voice.play(discord.FFmpegPCMAudio('song.mp3'), after=lambda e: print(f'{name} has finished playing'))
AttributeError: 'NoneType' object has no attribute 'play'

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question