def test_get(self): """ Test get() returns value if exists or default if not""" ttl_dict = TTLOrderedDict(1) ttl_dict['a'] = 1 self.assertEqual(ttl_dict.get('a'), 1) self.assertEqual(ttl_dict.get('b', "default"), "default") time.sleep(2) self.assertEqual(ttl_dict.get('a', "default"), "default")
class ReactionMenus: """ Class that manages reaction menus. Attributes --------------- bot: :class:`bot.TLDR` The discord bot. menus: :class:`TTLOrderedDict` Dict with TTL of five minutes """ def __init__(self, bot: bot.TLDR): self.bot = bot self.bot.add_listener(self._on_reaction_add, 'on_reaction_add') five_minutes = 5 * 60 self.menus = TTLOrderedDict(default_ttl=five_minutes) self.bot.logger.info('ReactionMenus module has been initiated') def add(self, menu: Union[ReactionMenu, BookMenu]): """ Adds menu to :attr:`menus`. Parameters ---------------- menu: Union[:class:`ReactionMenu`, :class:`BookMenu`] The menu that will be added to :attr:`menus` """ self.menus[menu.message.id] = menu async def _on_message_delete(self, message: discord.Message): """If the message gets deleted, delete the menu from the dict""" if message.id in self.menus: del self.menus[message.id] async def _on_reaction_add(self, reaction: discord.Reaction, user: discord.User): """Event listener that handles reactions.""" if user.bot: return menu = self.menus.get(reaction.message.id, None) if menu is None: return await menu.call_function(reaction, user)
class StalkCmds(commands.Cog): def __init__(self, bot): self.bot = bot self.stalkdict = TTLOrderedDict(default_ttl=60) @commands.command(aliases=['backgroundcheck', 'check', 'creep']) @commands.cooldown(60, 60) @permission_node(f'{__name__}.stalk') async def stalk(self, ctx, lookupName: str): """Fetch some incriminatory information on a player. Gets info such as UUID, past-names and avatar. """ log.info(f'Stalking \"{lookupName}\"...') if lookupName in self.stalkdict.keys(): mcU = self.stalkdict.get(lookupName) log.info(f'Retrieved data for \"{lookupName}\" from cache.') else: try: mcU = await MCUser.create(lookupName, self.bot.session) self.stalkdict[lookupName] = mcU except mojException as e: log.warning(e.message) reportCard = discord.Embed(title=e.message, type="rich", colour=discord.Colour.dark_red()) await ctx.send(embed=reportCard) return reportCard = discord.Embed(title="__Subject: " + mcU.name + "__", url='http://mcbouncer.com/u/' + mcU.uuid, type='rich', color=0x0080c0) reportCard.set_author( name="Classified Report", url=f'https://google.com/search?q=minecraft%20"{mcU.name}"', icon_url='https://crafatar.com/avatars/' + mcU.uuid) reportCard.set_thumbnail(url='https://crafatar.com/renders/head/' + mcU.uuid + '?overlay') reportCard.add_field(name="Current Name:", value="```\n" + mcU.name + "\n```") reportCard.add_field(name="UUID:", value="```\n" + mcU.uuid + "\n```") reportCard.add_field( name="Links!:", value=f"[MCBans](https://www.mcbans.com/player/{mcU.name}/)\n" f"[Statistic](https://minecraft-statistic.net/en/player/{mcU.name}.html)\n" f"[MCBouncer](http://mcbouncer.com/u/{mcU.uuid})\n" f'[Google](https://google.com/search?q=minecraft%20"{mcU.name}")') if mcU.demo: reportCard.add_field(name="__**DEMO ACCOUNT**__", value="Watch out for this!") if mcU.legacy: reportCard.add_field(name="*Legacy*", value="This guy is old-school!") if mcU.nameHistory is not None: pastNames = '\n'.join(mcU.nameHistory) reportCard.add_field(name="Past names:", value=f'```\n{pastNames}\n```') reportCard.set_footer(text="Report compiled by Agent Charfred") log.info('Sent Reportcard.') await ctx.send(embed=reportCard)