import random from discord.ext import commands class InternCog(commands.Cog): def __init__(self, bot=None): self.bot = bot # `data` expected to be a list of dicts with 'guild_id' and 'mentor_id' self.data = [] self.connected_guild_ids = set() self.in_voice_channel = False async def on_voice_state_update(self, member, before, after): # determine guild context guild = None if after and getattr(after, 'channel', None): guild = after.channel.guild elif before and getattr(before, 'channel', None): guild = before.channel.guild if not guild: return # find matching mentor entry for this guild entry = None for e in getattr(self, 'data', []): if e.get('guild_id') == getattr(guild, 'id', None) and e.get('mentor_id') == getattr(member, 'id', None): entry = e break if not entry: return # find existing voice client for this guild (if any) voice_client = None for vc in getattr(self.bot, 'voice_clients', []): if getattr(vc, 'guild', None) and getattr(vc.guild, 'id', None) == getattr(guild, 'id', None): voice_client = vc break # mentor joined a channel (initial join) if (not before or not getattr(before, 'channel', None)) and after and getattr(after, 'channel', None): # if bot is not connected, connect with 25% chance if not voice_client: if random.random() < 0.25: await after.channel.connect() return # mentor moved channels while bot is connected -> move bot if voice_client and after and getattr(after, 'channel', None): await voice_client.move_to(after.channel) return # mentor left channel -> disconnect bot if connected if voice_client and (not after or not getattr(after, 'channel', None)): await voice_client.disconnect(force=True) return