async def local(self, ctx): """List all non-global tags on this server""" all_tags = await self.bot.db.fetch( "SELECT * FROM guild_tags WHERE guild_id = $1 AND global = false " "ORDER BY uses desc", ctx.guild.id) if not all_tags: embed = self.bot.embeds.embed_builder( title="There are no local tags on this server.", description="", has_footer=False) return await ctx.send(embed=embed) pretty_tags = [] for record in all_tags: pretty_tags.append( f"`{config.BOT_PREFIX}{record['name']}` {record['title']}") pages = AlternativePages( ctx=ctx, entries=pretty_tags, show_entry_count=True, a_title=f"Local Tags in {ctx.guild.name}", a_icon=ctx.guild.icon_url_as(static_format='png'), show_index=False, show_amount_of_pages=True) await pages.paginate()
async def paginate_all_laws(self, ctx): async with ctx.typing(): query = """SELECT legislature_laws.law_id, legislature_bills.bill_name, legislature_bills.link FROM legislature_laws JOIN legislature_bills ON legislature_laws.bill_id = legislature_bills.id ORDER BY legislature_laws.law_id; """ all_laws = await self.bot.db.fetch(query) pretty_laws = [] for record in all_laws: pretty_laws.append( f"Law #{record['law_id']} - [{record['bill_name']}]({record['link']})" ) if not pretty_laws: embed = self.bot.embeds.embed_builder( title="There are no laws yet.") return await ctx.send(embed=embed) pages = AlternativePages( ctx=ctx, entries=pretty_laws, show_entry_count=False, title= f"{self.bot.mk.NATION_EMOJI} All Laws in {self.bot.mk.NATION_FULL_NAME}", per_page=14, show_index=False, show_amount_of_pages=True) await pages.paginate()
async def _from(self, ctx, *, member: typing.Union[discord.Member, CaseInsensitiveMember, discord.User] = None): """List the tags that someone made""" member = member or ctx.author all_tags = await self.bot.db.fetch( "SELECT * FROM guild_tags WHERE author = $1 AND guild_id = $2" " ORDER BY uses desc", member.id, ctx.guild.id) if not all_tags: embed = self.bot.embeds.embed_builder( title=f"{member} hasn't made any tags on this server yet.", description="", has_footer=False) return await ctx.send(embed=embed) pretty_tags = [] for record in all_tags: pretty_tags.append( f"`{config.BOT_PREFIX}{record['name']}` {record['title']}") pages = AlternativePages( ctx=ctx, entries=pretty_tags, show_entry_count=True, a_title=f"Tags from {member.display_name}", show_index=False, show_amount_of_pages=True, a_icon=member.avatar_url_as(static_format="png")) await pages.paginate()
async def _from(self, ctx, *, member_or_party: typing.Union[discord.Member, CaseInsensitiveMember, discord.User, PoliticalParty] = None): """List the laws a specific person or Political Party authored""" member = member_or_party or ctx.author if isinstance(member, PoliticalParty): name = member.role.name members = [m.id for m in member.role.members] else: name = member.display_name members = [member.id] query = """SELECT legislature_laws.law_id, legislature_bills.bill_name, legislature_bills.link FROM legislature_laws JOIN legislature_bills ON legislature_laws.bill_id = legislature_bills.id WHERE legislature_bills.submitter = ANY($1::bigint[]) ORDER BY legislature_laws.law_id; """ laws_from_person = await self.bot.db.fetch(query, members) if not laws_from_person: if isinstance(member, PoliticalParty): title = f"No member of {name} has made a law yet." else: title = f"{name} hasn't made any laws yet." embed = self.bot.embeds.embed_builder(title=title) return await ctx.send(embed=embed) pretty_laws = [] for record in laws_from_person: pretty_laws.append( f"Law #{record['law_id']} - [{record['bill_name']}]({record['link']})" ) if isinstance(member, PoliticalParty): a_title = f"Laws from members of {name}" a_icon = await member.get_logo() or EmptyEmbed else: a_title = f"Laws from {name}" a_icon = member.avatar_url_as(static_format='png') pages = AlternativePages(ctx=ctx, entries=pretty_laws, show_entry_count=False, a_title=a_title, show_index=False, show_amount_of_pages=True, a_icon=a_icon) await pages.paginate()
async def bills(self, ctx): """See all open bills from the Legislature to vote on""" pretty_bills = await self.get_pretty_vetos() if pretty_bills is None: embed = self.bot.embeds.embed_builder(title="There are no new bills to vote on.") return await ctx.send(embed=embed) pages = AlternativePages(ctx=ctx, entries=pretty_bills, show_entry_count=False, title=f"{self.bot.mk.NATION_EMOJI} Open Bills to Vote On", show_index=False, show_amount_of_pages=True) await pages.paginate()
async def search(self, ctx, *query: str): """Search for laws by their name or description""" name = ' '.join(query) if len(name) < 3: return await ctx.send( ":x: The query to search for must be at least 3 characters.") async with ctx.typing(): # First, search by name similarity async with self.bot.db.acquire() as con: results = await self.bot.laws.search_law_by_name( name, connection=con) # Set word similarity threshold for search by tag await self.bot.laws.update_pg_trgm_similarity_threshold( 0.4, connection=con) # Then, search by tag similarity for substring in query: if len(substring) < 3 or substring in self.illegal_tags: continue result = await self.bot.laws.search_law_by_tag( substring, connection=con) if result: results.update(result) if not results: results = ['Nothing found.'] pages = AlternativePages( ctx=ctx, entries=list(results), show_entry_count=False, title=f"{self.bot.mk.NATION_EMOJI} Laws matching '{name}'", show_index=False, show_amount_of_pages=True) await pages.paginate()
async def search(self, ctx, *, query: str): """Search for a global or local tag on this server""" db_query = """SELECT tag_id FROM guild_tags_alias WHERE (global = true AND alias LIKE '%' || $1 || '%') OR (alias LIKE '%' || $1 || '%' AND guild_id = $2) ORDER BY similarity(alias, $1) DESC LIMIT 20 """ tags = await self.bot.db.fetch(db_query, query.lower(), ctx.guild.id) pretty_names = dict( ) # Abuse dict as ordered set since you can't use DISTINCT in above SQL query if not tags: pretty_names['Nothing found.'] = None else: for record in tags: details = await self.bot.db.fetchrow( "SELECT name, title from guild_tags WHERE id = $1", record['tag_id']) if details: pretty_names[ f"`{config.BOT_PREFIX}{details['name']}` {details['title']}"] = None pages = AlternativePages( ctx=ctx, entries=list(pretty_names), show_entry_count=False, a_title=f"Tags matching '{query}'", a_icon=ctx.guild.icon_url_as(static_format='png'), show_index=False, show_amount_of_pages=True) await pages.paginate()
async def lyrics(self, ctx, *, query: str = None): """Find lyrics for a song **Usage:** `-lyrics` to get the lyrics to the song you're currently listening to on Spotify `-lyrics <query>` to search for lyrics that match your query """ if query is None: now_playing = self.get_spotify_connection(ctx.author) if now_playing is None: return await ctx.send( ":x: You either have to give me something to search for or listen to a song" " on Spotify!") query = f"{now_playing.title} {' '.join(now_playing.artists)}" if len(query) < 3: return await ctx.send( ":x: The query has to be more than 3 characters!") async with ctx.typing(): async with self.bot.session.get( f"https://some-random-api.ml/lyrics?title={query}" ) as response: if response.status == 200: lyrics = await response.json() else: return await ctx.send( f":x: Couldn't find anything that matches `{query}`.") try: lyrics['lyrics'] = lyrics['lyrics'].replace("[", "**[").replace( "]", "]**") if len(lyrics['lyrics']) <= 2048: embed = self.bot.embeds.embed_builder( title=f"{lyrics['title']} by {lyrics['author']}", description=lyrics['lyrics'], colour=0x2F3136) embed.url = lyrics['links']['genius'] embed.set_thumbnail(url=lyrics['thumbnail']['genius']) return await ctx.send(embed=embed) pages = AlternativePages( ctx=ctx, entries=lyrics['lyrics'].splitlines(), show_entry_count=False, title=f"{lyrics['title']} by {lyrics['author']}", show_index=False, title_url=lyrics['links']['genius'], thumbnail=lyrics['thumbnail']['genius'], per_page=20, colour=0x2F3136, show_amount_of_pages=True) except KeyError: return await ctx.send( f":x: Couldn't find anything that matches `{query}`.") await pages.paginate()