Skip to content
This repository was archived by the owner on Oct 5, 2025. It is now read-only.

Commit 5b6f8d1

Browse files
authored
Added a Tag Feature to Group Queries by Related Topics (#18)
* Adding tags to various queries * Implemented a simple study guide generator * Documented my code slightly * Reformatted the utils file slightly * Cleaned up code * Added tags to every query
1 parent b754e97 commit 5b6f8d1

File tree

3 files changed

+228
-81
lines changed

3 files changed

+228
-81
lines changed

pymon.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from discord import Message
55
from discord.ext import commands
66
from discord_slash import SlashCommand
7-
from discord_slash.utils.manage_commands import create_option
7+
from discord_slash.utils.manage_commands import create_choice, create_option
88

99
import pymon_utils as utils
1010

@@ -27,7 +27,8 @@
2727
# Discord bot code
2828
async def _react_on_mention(message: Message):
2929
if client.user.mentioned_in(message) and not message.mention_everyone:
30-
indices = utils.search(keyword_mapping, utils.generate_keywords(message.content))
30+
indices = utils.search(
31+
keyword_mapping, utils.generate_keywords(message.content))
3132
if indices:
3233
reply = list()
3334
reply.extend([
@@ -110,6 +111,45 @@ async def _get(ctx, index: int):
110111
await ctx.send(embed=embed)
111112

112113

114+
@slash.slash(
115+
name="study",
116+
description="Provides a study guide from a set of predetermined tags.",
117+
options=[
118+
create_option(
119+
name="tag",
120+
description="Select a topic for your study guide.",
121+
required=True,
122+
option_type=3,
123+
choices=[
124+
create_choice(
125+
value=tag,
126+
name=f"{tag} ({len(utils.get_queries_from_tag(queries, tag))})",
127+
)
128+
for tag in sorted(utils.generate_tags_set(queries))
129+
]
130+
)
131+
]
132+
)
133+
async def _study(ctx, tag: str):
134+
"""
135+
Prints out a list of questions relevant to the tag.
136+
137+
:param ctx: the context to send messages to
138+
:return: None
139+
"""
140+
matches = utils.get_queries_from_tag(queries, tag)
141+
embed = discord.Embed(
142+
title=f"Pymon v{__version__}: Study Guide for {tag}",
143+
color=discord.Color.red(),
144+
description="\n".join(
145+
f"• ID-{match[0]}: {utils.create_md_link(match[1].get('resource'), match[1].get('query'))}"
146+
for match in matches
147+
)
148+
)
149+
150+
await ctx.send(embed=embed)
151+
152+
113153
@slash.slash(
114154
name="refresh",
115155
description="Refreshes the bot's knowledge base.",

pymon_utils.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,34 @@ def refresh_knowledge() -> tuple[list, dict]:
128128
keyword_mapping = generate_keyword_mapping(queries)
129129
generate_similar_queries(queries, keyword_mapping)
130130
return queries, keyword_mapping
131+
132+
133+
def generate_tags_set(queries: list) -> set:
134+
"""
135+
A handy method for processing the set of tags contained in Pymon's
136+
brain.
137+
138+
:param queries: the list of queries from Pymon's brain
139+
:return: a set of tags represented in Pymon's brain
140+
"""
141+
tags = set()
142+
for query in queries:
143+
query_tags = query.get("tags", [])
144+
tags |= set(query_tags)
145+
return tags
146+
147+
148+
def get_queries_from_tag(queries: list, tag: str) -> list[tuple[int, dict]]:
149+
"""
150+
Given a tag, this function searches Pymon's brain for all the
151+
matching queries.
152+
153+
:param queries: Pymon's brain as a list of queries
154+
:param tag: a tag to lookup
155+
:return: a list of tuples in the form (query ID, query)
156+
"""
157+
matches = list()
158+
for i, query in enumerate(queries):
159+
if tag in query.get("tags", []):
160+
matches.append((i, query))
161+
return matches

0 commit comments

Comments
 (0)