From 68d516834ca9ff7b858c23ca7b2f9b137231590e Mon Sep 17 00:00:00 2001 From: whitney Date: Thu, 16 May 2024 22:02:08 -0700 Subject: [PATCH] Auto-provision voice channels --- pwsBot/Dockerfile | 1 + pwsBot/bot.py | 9 ++++++- pwsBot/resource/intents.py | 1 + pwsBot/voice/__init__.py | 0 pwsBot/voice/voice_event_handler.py | 38 +++++++++++++++++++++++++++++ 5 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 pwsBot/voice/__init__.py create mode 100644 pwsBot/voice/voice_event_handler.py diff --git a/pwsBot/Dockerfile b/pwsBot/Dockerfile index e996efa..f7ac30d 100644 --- a/pwsBot/Dockerfile +++ b/pwsBot/Dockerfile @@ -5,6 +5,7 @@ WORKDIR /app COPY bot.py requirements.txt ./ COPY resource/ ./resource COPY commands/ ./commands +COPY voice/ ./voice RUN pip install --no-cache-dir -r requirements.txt diff --git a/pwsBot/bot.py b/pwsBot/bot.py index 1b03956..680005e 100644 --- a/pwsBot/bot.py +++ b/pwsBot/bot.py @@ -2,10 +2,12 @@ import discord import logging import sys +from commands.message_handler import handle_message + from resource.credentials import get_token from resource.intents import get_intents -from commands.message_handler import handle_message +from voice.voice_event_handler import handle_voice_event intents = get_intents() client = discord.Client(intents=intents) @@ -22,4 +24,9 @@ async def on_ready(): async def on_message(message): await handle_message(client, message) +@client.event +async def on_voice_state_update(member, before, after): + await handle_voice_event(member, before, after) + client.run(client_token) + diff --git a/pwsBot/resource/intents.py b/pwsBot/resource/intents.py index 959aa98..729d824 100644 --- a/pwsBot/resource/intents.py +++ b/pwsBot/resource/intents.py @@ -6,6 +6,7 @@ def get_intents(): # Specifying a list of enabled (True) intents. intents.messages = True intents.message_content = True + intents.voice_states = True return intents diff --git a/pwsBot/voice/__init__.py b/pwsBot/voice/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pwsBot/voice/voice_event_handler.py b/pwsBot/voice/voice_event_handler.py new file mode 100644 index 0000000..8820ff8 --- /dev/null +++ b/pwsBot/voice/voice_event_handler.py @@ -0,0 +1,38 @@ +import logging +import sys + +VOICE_MANAGED_CHANNELS = ['bot-test-voice'] + +provisioned_channels = [] + +log_format = '%(asctime)s %(levelname)s\t%(filename)s %(message)s' +logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format=log_format) + +async def handle_voice_event(member, before, after): + if after.channel and before.channel != after.channel and after.channel.name in VOICE_MANAGED_CHANNELS: + # User has joined a managed voice channel that is not yet being tracked. + if before.channel: + logging.debug('Member ' + str(member.name) + ' moved from channel ' + str(before.channel.name) + ' to watched channel ' + str(after.channel.name) + '.') + else: + logging.debug('Member ' + str(member.name) + ' joined watched channel ' + str(after.channel.name) + '.') + + guild = member.guild + new_channel_name = f"{after.channel.name} ({member.name})" + new_channel = await guild.create_voice_channel(new_channel_name) + + provisioned_channels.append(new_channel_name) + logging.info('Created and tracked new voice channel: ' + str(new_channel_name)) + logging.debug('Tracked channels: ' + str(provisioned_channels)) + + await(member.move_to(new_channel)) + logging.info('Moved user ' + str(member.name) + ' from channel ' + str(after.channel.name) + ' to channel ' + str(new_channel.name) + '.') + elif before.channel and not before.channel.members and before.channel.name in provisioned_channels: + # User was the last one to leave a tracked channel. + logging.info('User ' + str(member.name) + ' was the last to leave tracked channel ' + str(before.channel.name) + '.') + + logging.info('Removing empty tracked voice channel: ' + str(before.channel.name) + '.') + await before.channel.delete() + + provisioned_channels.remove(before.channel.name) + logging.debug('Tracked channels: ' + str(provisioned_channels)) +