Skip to content

refactor(tempvc): use list comprehensions, add TEMPVC_BASE_NAME #951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/settings.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ USER_IDS:
TEMPVC_CATEGORY_ID: 123456789012345679
# Set this to the channel ID where you want the temporary voice channels to be created.
TEMPVC_CHANNEL_ID: 123456789012345679
TEMPVC_BASE_NAME: "/tmp/"

# This will automatically give people with a status regex a role.
STATUS_ROLES:
Expand Down
37 changes: 19 additions & 18 deletions tux/cogs/services/temp_vc.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import asyncio

import discord
from discord.ext import commands

Expand All @@ -8,7 +10,7 @@
class TempVc(commands.Cog):
def __init__(self, bot: Tux) -> None:
self.bot = bot
self.base_vc_name: str = "/tmp/"
self.base_vc_name: str = CONFIG.TEMPVC_BASE_NAME or "/tmp/"

@commands.Cog.listener()
async def on_voice_state_update(
Expand All @@ -34,15 +36,16 @@ async def on_voice_state_update(
# Ensure CONFIGants are set correctly
temp_channel_id = int(CONFIG.TEMPVC_CHANNEL_ID or "0")
temp_category_id = int(CONFIG.TEMPVC_CATEGORY_ID or "0")
if temp_channel_id == 0 or temp_category_id == 0:

if 0 in {temp_category_id, temp_channel_id}:
return

# When user joins the temporary voice channel
if after.channel and after.channel.id == temp_channel_id:
await self._handle_user_join(member, after.channel)

# When user leaves any voice channel
elif before.channel:
if before.channel:
await self._handle_user_leave(before.channel, after.channel, temp_channel_id, temp_category_id)

async def _handle_user_join(
Expand All @@ -61,11 +64,12 @@ async def _handle_user_join(
The channel that the member joined.
"""

for voice_channel in channel.guild.voice_channels:
# Check if the channel is a temporary channel and if it is the user's channel
if voice_channel.name == self.base_vc_name + member.name:
await member.move_to(voice_channel)
return
tasks = [
member.move_to(voice_channel)
for voice_channel in channel.guild.voice_channels
if voice_channel.name == self.base_vc_name + member.name
]
asyncio.gather(*tasks)

# Create a new channel for the user if it doesn't exist
new_channel = await channel.clone(name=self.base_vc_name + member.name)
Expand Down Expand Up @@ -107,19 +111,16 @@ async def _handle_user_leave(
return

# Delete the channel if it is empty
if len(before_channel.members) == 0:
if before_channel.members == []:
await before_channel.delete()

# Search and delete all empty temporary channels
for channel in category.voice_channels:
if (
not channel.name.startswith(self.base_vc_name)
or len(channel.members) != 0
or channel.id == temp_channel_id
):
continue

await channel.delete()
tasks = [
channel.delete()
for channel in category.voice_channels
if channel.name.startswith(self.base_vc_name) and channel.members == [] and channel.id == temp_channel_id
]
await asyncio.gather(*tasks)


async def setup(bot: Tux) -> None:
Expand Down
1 change: 1 addition & 0 deletions tux/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def BOT_TOKEN(self) -> str: # noqa: N802
# Temp VC
TEMPVC_CATEGORY_ID: Final[str | None] = config["TEMPVC_CATEGORY_ID"]
TEMPVC_CHANNEL_ID: Final[str | None] = config["TEMPVC_CHANNEL_ID"]
TEMPVC_BASE_NAME: Final[str | None] = config["TEMPVC_BASE_NAME"]

# GIF ratelimiter
RECENT_GIF_AGE: Final[int] = config["GIF_LIMITER"]["RECENT_GIF_AGE"]
Expand Down
Loading