Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add searchable info WIP #109

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions crimsobot/cogs/reactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async def fact_about(self, ctx: commands.Context, *, subject: CleanMentions) ->
You will need to know that fact's ID """

try:
fact_object = await FunFact.get_by_subject(subject.lower().strip(), ctx.guild.id)
fact_object = await FunFact.get_random_by_subject(subject.lower().strip(), ctx.guild.id)
except NoFactsExist:
embed = c.crimbed(
title='OMAN',
Expand Down Expand Up @@ -125,7 +125,7 @@ async def fact_inspect(self, ctx: commands.Context, fact_id: int) -> None:
guild = self.bot.get_guild(fact_object.guild_id)

embed = c.crimbed(
title=f'FACT INSPECT // ID: {fact_object.uid}' + ' (admin view)' if owner else '',
title=f'FACT INSPECT // ID: {fact_object.uid}' + (' (admin view)' if owner else ''),
descr=None,
footer='Users with the Manage Messages permission can remove facts using ">fact remove [ID]"',
thumb_name='nerd',
Expand All @@ -144,6 +144,9 @@ async def fact_inspect(self, ctx: commands.Context, fact_id: int) -> None:
),
]

if not owner:
field_list.pop(3) # no need to tell the plebs the server name

for field in field_list:
embed.add_field(name=field[0], value=field[1], inline=False)

Expand Down Expand Up @@ -175,6 +178,41 @@ async def fact_random(self, ctx: commands.Context) -> None:

await ctx.send(return_string)

@fact.command(name='subject', brief='Get info about a specific subject of facts!')
async def subject_info(self, ctx: commands.Context, *, subject: CleanMentions) -> None:
"""Look up more info about a subject.
"""

# clean input
user_input = subject.split(';', 1)
subject = user_input.pop(0).strip().lower()

try:
fact_object = await FunFact.get_all_by_subject(subject, ctx.guild.id) # TODO: this only grabs one fact
print(fact_object[0].__dict__)
except DoesNotExist:
embed = c.crimbed(title=None, descr=f'Subject {subject} does not exist (at least not in this server)!')
await ctx.send(embed=embed, delete_after=18)
return

embed = c.crimbed(
title=f'SUBJECT INSPECT // {subject}',
descr=None,
footer='Users with the Manage Messages permission can wipe a subject using ">fact wipe [subject]"',
thumb_name='nerd',
color_name='yellow',
)

field_list = [
('Subject', subject),
('Count', len(fact_object)),
]

for field in field_list:
embed.add_field(name=field[0], value=field[1], inline=False)

await ctx.send(embed=embed)

@fact.command(name='remove', aliases=['delete'], brief='Remove a fact by ID.')
@has_guild_permissions(manage_messages=True)
async def remove_fact(self, ctx: commands.Context, fact_id: int) -> None:
Expand Down
8 changes: 7 additions & 1 deletion crimsobot/models/fun_fact.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async def get_by_id(cls, fact_id: int, guild: int, owner: bool) -> 'FunFact':
return fact

@classmethod
async def get_by_subject(cls, subject: str, guild: int) -> 'FunFact':
async def get_random_by_subject(cls, subject: str, guild: int) -> 'FunFact':
all_facts = await FunFact.filter(subject=subject, guild_id=guild).prefetch_related('created_by')

try:
Expand All @@ -68,6 +68,12 @@ async def get_by_subject(cls, subject: str, guild: int) -> 'FunFact':

return fact

@classmethod
async def get_all_by_subject(cls, subject: str, guild: int) -> Any: # TODO: why Any and not List['FunFact']?
all_facts = await FunFact.filter(subject=subject, guild_id=guild).prefetch_related('created_by')

return all_facts

@classmethod
async def get_random(cls, guild: int) -> 'FunFact':
all_facts = await FunFact.filter(guild_id=guild).prefetch_related('created_by')
Expand Down