async def wiperesources(cmd, pld): """ :param cmd: The command object referenced in the command. :type cmd: sigma.core.mechanics.command.SigmaCommand :param pld: The payload with execution data and details. :type pld: sigma.core.mechanics.payload.CommandPayload """ try: target_id = abs(int(pld.args[0])) if pld.args else None except ValueError: target_id = None if target_id: target = await cmd.bot.get_user(target_id) target_name = target.name if target else target_id colls = await cmd.db[cmd.db.db_nam].list_collection_names() reses = list( sorted([ coll[:-8].lower() for coll in colls if coll.endswith('Resource') ])) for res in reses: new_res = SigmaResource({}) await cmd.db.update_resource(target_id, res, new_res) response = discord.Embed( color=0xFFCC4D, title=f'🔥 Ok, I\'ve wiped {target_name}\'s resources.') else: response = error('Nothing inputted.') await pld.msg.channel.send(embed=response)
async def get_resource(self, user_id, resource_name): """ Returns a resource class for the given user id and resource name. :type user_id: int :type resource_name: str :rtype: sigma.core.mechanics.resources.SigmaResource """ data = await self[self.db_nam ][f'{resource_name.title()}Resource'].find_one( {'user_id': user_id}) or {} resource = SigmaResource(data) return resource
async def precache_resources(self): """ Caches all user resources for all guilds on startup to reduce database load during regular functionality. """ self.bot.log.info('Pre-Caching all resource data...') res_cache_counter = 0 all_colls = await self[self.db_nam].list_collection_names() for coll in all_colls: if coll.endswith('Resource'): res_nam = coll[:8].lower() docs = await self[self.db_nam][coll].find({}).to_list(None) for doc in docs: uid = doc.get('user_id') cache_key = f'res_{res_nam}_{uid}' resource = SigmaResource(doc) await self.cache.set_cache(cache_key, resource) res_cache_counter += 1 self.bot.log.info( f'Finished pre-caching {res_cache_counter} resource entries.')
async def awardpumpkinpatch(cmd, pld): """ :param cmd: The command object referenced in the command. :type cmd: sigma.core.mechanics.command.SigmaCommand :param pld: The payload with execution data and details. :type pld: sigma.core.mechanics.payload.CommandPayload """ await pld.msg.add_reaction('🎃') guild_sums = {} guild_counts = {} guild_participants = {} all_weights = await cmd.db[cmd.db.db_nam ].WeightResource.find({}).to_list(None) for weight in all_weights: resource = SigmaResource(weight) if not await cmd.db.is_sabotaged(resource.raw.get('user_id')): for guild_key in resource.origins.guilds.keys(): guild_part = guild_participants.get(guild_key, []) guild_part.append({ 'id': resource.raw.get('user_id'), 'val': resource.origins.guilds.get(guild_key) }) guild_total = guild_sums.get(guild_key, 0) guild_count = guild_counts.get(guild_key, 0) guild_total += resource.origins.guilds.get(guild_key) guild_count += 1 guild_sums.update({guild_key: guild_total}) guild_counts.update({guild_key: guild_count}) guild_participants.update({guild_key: guild_part}) guild_sum_list = [] for gsk in guild_sums.keys(): guild_sum_list.append({ 'id': int(gsk), 'val': guild_sums.get(gsk), 'cnt': guild_counts.get(gsk), 'avg': guild_sums.get(gsk) / guild_counts.get(gsk) }) guild_sum_list = list( sorted(guild_sum_list, key=lambda x: x.get('avg'), reverse=True)) award_count = 0 award_value = 0 for gsli_x, gsli in enumerate(guild_sum_list): guild_multi = 20 - gsli_x if gsli_x < 20 else 0.25 guild_val = gsli.get('val') guild_count = gsli.get('cnt') user_list = guild_participants.get(str(gsli.get('id'))) for user_doc in user_list: award_count += 1 user_id = user_doc.get('id') user_val = user_doc.get('val') award = int(100000 * guild_multi * (user_val / guild_val) * (guild_count / 1.666)) award_value += award await cmd.db.add_resource(int(user_id), 'currency', award, cmd.name, pld.msg, ranked=False) response = ok( f'Awarded {award_count} users a total of {award_value} {cmd.bot.cfg.pref.currency}.' ) await pld.msg.channel.send(embed=response)
async def get_resource(self, user_id: int, resource_name: str): resources = await self.get_profile(user_id, 'resources') or {} resource_data = resources.get(resource_name, {}) return SigmaResource(resource_data)
async def update_resource(self, resource: SigmaResource, user_id: int, name: str): resources = await self.get_profile(user_id, 'resources') or {} resources.update({name: resource.dictify()}) await self.set_profile(user_id, 'resources', resources)