Auto-provision voice channels

This commit is contained in:
whitney 2024-05-16 22:02:08 -07:00
parent 915fac2eba
commit 68d516834c
5 changed files with 48 additions and 1 deletions

View File

@ -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

View File

@ -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)

View File

@ -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

0
pwsBot/voice/__init__.py Normal file
View File

View File

@ -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))