I am trying to play an mp3 file when a specific user is moved to the afk voice channel.
In discord.py rewrite:
async def on_voice_state_update(member, before, after):
user = bot.get_user("my user id")
if after.afk and user == True:
channel = bot.get_channel("the id of the channel I want the bot to connect to")
voice = await channel.connect()
voice.play(discord.FFmpegPCMAudio('directory of mp3 file'))
await asyncio.sleep(7)
await voice.disconnect()
The Problem
It all works until I try to specify the user in the if statement:
user == True
However, once I include this requirement it stops working.
What I've tried
I have tried to give the user object an attribute, such as user.connect == True and user.joined == True etc... but no luck.
The goal
In it's perfected form, I would be able to specify not only what user that is required to join the afk channel for the event to work, but also get the channel id of another user so that the bot can connect to that channel:
channel = bot.get_channel("the id the channel that a specified user is in")
EDIT:
Based on Diggy's answer I have changed the if statement to look like this:
async def on_voice_state_update(member, prev, cur):
if member.id == 'my user id' and cur.afk:
channel = bot.get_channel('the channel id for the bot to connect to')
voice = await channel.connect()
voice.play(discord.FFmpegPCMAudio('dir of mp3 file'))
await asyncio.sleep(7)
await voice.disconnect()
else:
pass
However it also plays the audio once I leave the channel.
I tried to specify that it would not play once I leave the channel by using if member.id == 'my user id' and cur.afk and not prev.afk but it still plays when I leave.
Experimental Solution:
Okay so I thought of making it so the bot only joins the previous channel and so once I leave the afk channel it plays it in that which isn't a problem. This has allowed it to work and play in the channel previous to the afk one which is great but it's not perfect. It shouldn't need to join the afk channel:
async def on_voice_state_update(member, prev, cur):
if member.id == 'my user id' and cur.afk:
prevchannel = prev.channel.id
channel = bot.get_channel(prevchannel)
voice = await channel.connect()
voice.play(discord.FFmpegPCMAudio('dir to mp3 file'))
await asyncio.sleep('duration of mp3 file')
await voice.disconnect()
else:
pass
I thought that an elif might work such as:
elif prev.afk is not None and cur.afk is None:
pass
but it pretty much just ignores it.
-The prev and cur args sometimes don't work too so I have to switch back to before and after