I'm currently trying to code a discord bot in python and am trying to make it join a channel, play a sound and then leave.
I came up with this code :
@bot.command()
async def sound(ctx):
channel = ctx.author.voice.channel
await join(ctx)
voice = get(bot.voice_clients, guild=ctx.guild)
source = FFmpegPCMAudio('sound.mp3')
await voice.play(source)
await channel.disconnect()
However, when I try it, after playing the sound, it doesn't disconnect and there is in my shell an error :
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: object NoneType can't be used in 'await' expression
I saw on forums that it could be a asyncio problem but I don't know how to resolve it.
Could someone help me please ?
Edit : my imports on my program :
import discord
import asyncio
from discord.ext import commands
from discord.utils import get
from discord import FFmpegPCMAudio
Edit v2 : the join() command :
@bot.command(pass_context=True)
async def join(ctx):
channel = ctx.author.voice.channel
voice = get(bot.voice_clients, guild=ctx.guild)
if not channel:
await ctx.send("You are not in a vocal channel.")
return
if voice and voice.is_connected():
await voice.move_to(channel)
elif voice == channel:
return
else:
await channel.connect()
await join(ctx)
but not whatjoin
does since it's not included. This is just a guess, but tryawait voice.disconnet()
– Benjin