async def applied(self, ctx): redis_cache = self.bot.ext_conns["redis_cache"] manager = MessageManager(ctx) admin_channel = self.bot.get_channel( self.bot.guild_map[ctx.guild.id].admin_channel ) cursor = b"0" while cursor: cursor, keys = await redis_cache.scan( cursor, match=f"{ctx.guild.id}-clan-application-*" ) if len(keys) > 0: for key in keys: embed_packed = await redis_cache.get(key) member_db_id = key.decode("utf-8").split("-")[-1] application_db = await ClanMemberApplication.filter( member_id=member_db_id ) previous_message_id = application_db.message_id previous_message = await admin_channel.fetch_message( previous_message_id ) await previous_message.delete() new_message = await manager.send_embed(deserializer(embed_packed)) application_db.message_id = new_message.id await application_db.save() else: await manager.send_and_clean("No applications found.")
async def applied(self, ctx): redis_cache = self.bot.ext_conns['redis_cache'] manager = MessageManager(ctx) admin_channel = self.bot.get_channel( self.bot.guild_map[ctx.guild.id].admin_channel) cursor = b'0' while cursor: cursor, keys = await redis_cache.scan( cursor, match=f'{ctx.guild.id}-clan-application-*') if len(keys) > 0: for key in keys: embed_packed = await redis_cache.get(key) member_db_id = key.decode('utf-8').split('-')[-1] query = ClanMemberApplication.select().join(Member).where( Member.id == member_db_id) application_db = await self.bot.ext_conns['database'].get(query ) previous_message_id = application_db.message_id previous_message = await admin_channel.fetch_message( previous_message_id) await previous_message.delete() new_message = await manager.send_embed( deserializer(embed_packed)) application_db.message_id = new_message.id await self.bot.ext_conns['database'].update(application_db) else: await manager.send_and_clean("No applications found.")
async def get_cached_members(ctx, guild_id, guild_name): cache_key = f'{guild_id}-members' clan_members = await ctx['redis_cache'].get(cache_key) if not clan_members: clan_members = await set_cached_members(ctx, guild_id, guild_name) clan_members = deserializer(clan_members) member_dbs = [dict_to_model(ClanMember, member) for member in clan_members] return member_dbs
async def roster(self, ctx, *args): """Show roster for all connected clans""" manager = MessageManager(ctx) clan_dbs = await self.bot.database.get_clans_by_guild(ctx.guild.id) if not clan_dbs: return await manager.send_and_clean("No connected clans found") members = [] members_redis = await self.bot.ext_conns['redis_cache'].lrange( f"{ctx.guild.id}-clan-roster", 0, -1) if members_redis and '-nocache' not in args: await self.bot.ext_conns['redis_cache'].expire( f"{ctx.guild.id}-clan-roster", constants.TIME_HOUR_SECONDS) for member in members_redis: members.append(deserializer(member)) else: members_db = await self.bot.database.get_clan_members( [clan_db.clan_id for clan_db in clan_dbs], sorted_by='username') for member in members_db: await self.bot.ext_conns['redis_cache'].rpush( f"{ctx.guild.id}-clan-roster", serializer(member)) await self.bot.ext_conns['redis_cache'].expire( f"{ctx.guild.id}-clan-roster", constants.TIME_HOUR_SECONDS) members = members_db entries = [] for member in members: timezone = "Not Set" if member.timezone: tz = datetime.now(pytz.timezone(member.timezone)) timezone = f"{tz.strftime('UTC%z')} ({tz.tzname()})" member_info = ( member.username, f"Clan: {member.clanmember.clan.name} [{member.clanmember.clan.callsign}]\n" f"Join Date: {date_as_string(member.clanmember.join_date)}\n" f"Timezone: {timezone}") entries.append(member_info) p = FieldPages(ctx, entries=entries, per_page=5, title="Roster for All Connected Clans", color=constants.BLUE) await p.paginate()
async def create_redis_jobs_pool(): return await arq.create_pool( config.arq_redis, job_serializer=lambda b: serializer(b), job_deserializer=lambda b: deserializer(b), )
def job_deserializer(b): return deserializer(b)
async def info(self, ctx, *args): """Show information for all connected clans""" redis_cache = self.bot.ext_conns["redis_cache"] manager = MessageManager(ctx) clan_dbs = await self.bot.database.get_clans_by_guild(ctx.guild.id) if not clan_dbs: return await manager.send_and_clean( "No connected clans found", mention=False ) embeds = [] clan_redis_key = f"{ctx.guild.id}-clan-info" clan_info_redis = await redis_cache.get(clan_redis_key) if clan_info_redis and "-nocache" not in args: log.debug(f"{clan_redis_key} {clan_info_redis}") await redis_cache.expire(clan_redis_key, constants.TIME_HOUR_SECONDS) embeds = [ discord.Embed.from_dict(embed) for embed in deserializer(clan_info_redis) ] else: for clan_db in clan_dbs: group = await execute_pydest( self.bot.destiny.api.get_group, clan_db.clan_id, return_type=DestinyGroupResponse, ) if not group.response: log.error( f"Could not get details for clan {clan_db.name} ({clan_db.clan_id}) - " f"{group.error_status} {group.error_description}" ) return await manager.send_and_clean( f"Clan {clan_db.name} not found", mention=False ) else: group = group.response embed = discord.Embed( colour=constants.BLUE, title=group.detail.motto, description=group.detail.about, ) embed.set_author( name=f"{group.detail.name} [{group.detail.clan_info.clan_callsign}]", url=f"https://www.bungie.net/en/ClanV2?groupid={clan_db.clan_id}", ) embed.add_field( name="Members", value=group.detail.member_count, inline=True ) embed.add_field( name="Founder", value=group.founder.bungie_net_user_info.display_name, inline=True, ) embed.add_field( name="Founded", value=date_as_string(group.detail.creation_date), inline=True, ) embeds.append(embed) await redis_cache.set( clan_redis_key, serializer([embed.to_dict() for embed in embeds]), expire=constants.TIME_HOUR_SECONDS, ) if len(embeds) > 1: paginator = EmbedPages(ctx, embeds) await paginator.paginate() else: await manager.send_embed(embeds[0])