implement voice channel connect logic

This commit is contained in:
deflax 2022-03-27 22:38:03 -04:00
parent c963e6f256
commit 626ab32a73

46
main.py
View file

@ -22,19 +22,21 @@ bot = commands.Bot(command_prefix='!', description=description)
# Initialize some global variables # Initialize some global variables
voice_client = None voice_client = None
text_channel = None text_channel = None
isPlaying = False isConnected = False
@bot.event @bot.event
async def on_ready(): async def on_ready():
print('Logged in as ' + bot.user.name + ' #' + str(bot.user.id)) print('[INFO] Logged in as ' + bot.user.name + ' #' + str(bot.user.id))
# get channels # get channels
#voice_channel = bot.get_channel(voice_channel_id) voice_channel = await bot.fetch_channel(voice_channel_id)
#text_channel = bot.get_channel(text_channel_id) debug_channel = await bot.fetch_channel(text_channel_id)
#if not voice_channel: if not voice_channel:
# print("No voice channel " + voice_channel_id + " found!") print("[WARN] No voice channel " + voice_channel_id + " found!")
#if not text_channel: if not debug_channel:
# print("No text channel " + text_channel_id + " found!") print("[WARN] No text channel " + text_channel_id + " found!")
await debug_channel.send('] ready.')
@bot.event @bot.event
async def on_message(message): async def on_message(message):
@ -63,28 +65,26 @@ async def on_voice_state_update(member, before, after):
:param after: The member as they are after the change. :param after: The member as they are after the change.
:return: :return:
""" """
#print('before: ' + str(before)) global isConnected
#print('after: ' + str(after))
#return
if member.bot: voice_channel = await bot.fetch_channel(voice_channel_id)
print("self event detection") member_ids = len(voice_channel.voice_states.keys())
return
debug_channel = bot.get_channel(text_channel_id) debug_channel = await bot.fetch_channel(text_channel_id)
voice_channel = bot.get_channel(voice_channel_id)
member_ids = voice_channel.voice_states.keys()
await debug_channel.send('voice activity') if member_ids > 0:
return await debug_channel.send('] voice activity. voice #' + voice_channel_id + ' member count: ' + str(member_ids))
if str(after.channel.id) == voice_channel_id: if member_ids > 1 and not isConnected:
print("Connecting to voice channel " + voice_channel_id) await debug_channel.send('] connecting to #' + voice_channel_id)
voiceChannel = await voice_channel.connect() voiceChannel = await voice_channel.connect()
isConnected = True
print("Disconnecting from voice channel " + voice_channel_id) if member_ids == 1 and isConnected:
voiceChannel = await after.channel.connect() await debug_channel.send('] disconnecting from #' + voice_channel_id)
voiceChannel = await voice_channel.connect()
await voiceChannel.disconnect() await voiceChannel.disconnect()
isConnected = False
# Start the bot with multiprocess compatiblity # Start the bot with multiprocess compatiblity
if __name__ == "__main__": if __name__ == "__main__":