async def on_voice_state_join(self, member: discord.Member, after: discord.VoiceState): async with MaybeAcquire(pool=self.bot.pool) as connection: # Fetch DB entry record = await VoiceLogConfiguration.fetch_row( connection, guild_id=member.guild.id) if record is None: return channel = self.bot.get_channel(record["log_channel_id"]) if channel is None: return await channel.send(embed=discord.Embed( colour=discord.Colour.green(), description=f"{member.mention} joined **{after.channel.name}**.", timestamp=discord.utils.utcnow(), ).set_footer(text="Server log update", icon_url=member.avatar.url))
async def set_status(self): now = discord.utils.utcnow() status = get_status(now) if status is None: await self.bot.change_presence( status=discord.Status.online, activity=discord.Streaming(name="...", url="https://twitch.tv/monstercat"), ) status = "streaming" else: await self.bot.change_presence(status=status) status = status.name async with MaybeAcquire(pool=self.bot.pool) as connection: await StatusLog.insert(connection, user_id=self.bot.user.id, timestamp=now, status=status)
async def _find_guild(self, *, connection: Optional[asyncpg.Connection] = None) -> discord.Guild: async with MaybeAcquire(connection, pool=self.pool) as connection: free_spaces = {} for guild in CONFIG.EMOJI.GUILDS: total_free = guild.emoji_limit - sum(not e.animated for e in guild.emojis) if total_free > CONFIG.EMOJI.LEAVE_FREE: free_spaces[guild] = total_free if free_spaces: guilds = random.sample(list(free_spaces.keys()), counts=list(free_spaces.values()), k=1) return guilds[0] # Otherwise delete the oldest emoji record = await Emoji.fetch_row(connection, order_by=(Emoji.last_fetched, "ASC")) if record is None: raise RuntimeError("Somehow this happened.") await self.delete_emoji(record["emoji_id"], connection=connection) # return await self._find_guild(connection=connection) # could recurse return self.get_guild(record["guild_id"]) # type: ignore
def __init__(self, **kwargs) -> None: super().__init__(**kwargs) if self.bot.pool: self.db = MaybeAcquire(pool=self.bot.pool) else: self.db = NoDatabase()
async def add_users(self): await self.bot.wait_until_ready() async with MaybeAcquire(pool=self.bot.pool) as connection: for record in await VoiceWoesWhitelist.fetch(connection): self.bot.whitelisted_users.add(record["user_id"])