-
Notifications
You must be signed in to change notification settings - Fork 1
Add /recommend command for profile-based recommendations #19
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |||||
| """ | ||||||
|
|
||||||
| import random | ||||||
| from itertools import islice | ||||||
|
|
||||||
| import discord | ||||||
| from discord import app_commands | ||||||
|
|
@@ -63,3 +64,184 @@ async def christmas(interact: discord.Interaction): | |||||
| color=scratch_orange, | ||||||
| ) | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| @bot.tree.command( | ||||||
| name="recommend", | ||||||
| description="Get personalized recommendations for projects, studios, or users based on a profile!", | ||||||
| ) | ||||||
| @app_commands.choices( | ||||||
| recommendation_type=[ | ||||||
| app_commands.Choice(name="Projects", value="projects"), | ||||||
| app_commands.Choice(name="Users", value="users"), | ||||||
| app_commands.Choice(name="Studios", value="studios"), | ||||||
| ] | ||||||
| ) | ||||||
| async def recommend( | ||||||
| interact: discord.Interaction, username: str, recommendation_type: str | ||||||
| ): | ||||||
| await interact.response.defer() | ||||||
|
|
||||||
| try: | ||||||
| user = scratch.get_user(username) | ||||||
| except scratch.utils.exceptions.UserNotFound: | ||||||
| await interact.followup.send( | ||||||
| embed=discord.Embed( | ||||||
| title="Error:", | ||||||
| description="This user doesn't exist! <:giga404:1330551323610976339>", | ||||||
| color=discord.Color.red(), | ||||||
| ) | ||||||
| ) | ||||||
| return | ||||||
|
|
||||||
| msg = discord.Embed(color=scratch_orange) | ||||||
|
|
||||||
| if recommendation_type == "projects": | ||||||
| # Get user's loved and favorited projects to find similar ones | ||||||
kRxZykRxZy marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| loved_projects = list(islice(user.loved_projects(limit=10), 10)) | ||||||
|
|
||||||
| if not loved_projects: | ||||||
| msg.title = f"No recommendations found for {username}" | ||||||
| msg.description = "This user hasn't loved any projects yet!" | ||||||
| await interact.followup.send(embed=msg) | ||||||
| return | ||||||
|
|
||||||
| # Extract common tags/themes from loved projects | ||||||
kRxZykRxZy marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| recommendations = [] | ||||||
| seen_ids = set() | ||||||
|
|
||||||
| # For each loved project, get its author's other popular projects | ||||||
| for project in loved_projects[:3]: # Limit to avoid too many API calls | ||||||
| try: | ||||||
| author = project.author() | ||||||
| author_projects = list(author.projects(limit=5)) | ||||||
| for proj in author_projects: | ||||||
| if proj.id not in seen_ids and proj.id != project.id: | ||||||
| recommendations.append(proj) | ||||||
| seen_ids.add(proj.id) | ||||||
| if len(recommendations) >= 5: | ||||||
| break | ||||||
| except Exception: | ||||||
| continue | ||||||
kRxZykRxZy marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| if len(recommendations) >= 5: | ||||||
| break | ||||||
|
|
||||||
| if recommendations: | ||||||
| msg.title = f"📚 Project recommendations for {username}" | ||||||
| description = "Based on projects you loved, you might enjoy:\n" | ||||||
| for proj in recommendations[:5]: | ||||||
| description += ( | ||||||
| f"\n**[{proj.title}](<https://scratch.mit.edu/projects/{proj.id}>)**\n" | ||||||
| f"-# by [{proj.author_name}](https://scratch.mit.edu/users/{proj.author_name}) " | ||||||
| f"• ❤️ {proj.loves} • ⭐ {proj.favorites}\n" | ||||||
| ) | ||||||
| msg.description = description | ||||||
kRxZykRxZy marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| else: | ||||||
| msg.title = f"No recommendations found for {username}" | ||||||
| msg.description = "Could not find similar projects at this time." | ||||||
|
|
||||||
| elif recommendation_type == "users": | ||||||
| # Get users that the target user follows | ||||||
| following = list(islice(user.following_names(limit=20), 20)) | ||||||
|
|
||||||
| if not following: | ||||||
| msg.title = f"No recommendations found for {username}" | ||||||
| msg.description = "This user isn't following anyone yet!" | ||||||
| await interact.followup.send(embed=msg) | ||||||
| return | ||||||
|
|
||||||
| # Find users followed by the people this user follows | ||||||
| recommendations = [] | ||||||
| seen_users = set(following + [username]) | ||||||
|
|
||||||
| for followed_username in following[:5]: # Limit API calls | ||||||
| try: | ||||||
| followed_user = scratch.get_user(followed_username) | ||||||
| their_following = list(followed_user.following_names(limit=10)) | ||||||
|
|
||||||
| for potential_rec in their_following: | ||||||
| if potential_rec not in seen_users: | ||||||
| try: | ||||||
| rec_user = scratch.get_user(potential_rec) | ||||||
| recommendations.append(rec_user) | ||||||
| seen_users.add(potential_rec) | ||||||
| if len(recommendations) >= 5: | ||||||
| break | ||||||
| except Exception: | ||||||
| continue | ||||||
kRxZykRxZy marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| except Exception: | ||||||
| continue | ||||||
kRxZykRxZy marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| if len(recommendations) >= 5: | ||||||
| break | ||||||
|
|
||||||
| if recommendations: | ||||||
| msg.title = f"👥 User recommendations for {username}" | ||||||
| description = "Based on who you follow, you might like:\n" | ||||||
| for rec_user in recommendations[:5]: | ||||||
| description += ( | ||||||
| f"\n**[{rec_user.username}](https://scratch.mit.edu/users/{rec_user.username})**\n" | ||||||
| f"-# {rec_user.follower_count()} followers • {rec_user.following_count()} following\n" | ||||||
kRxZykRxZy marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| ) | ||||||
| msg.description = description | ||||||
| else: | ||||||
| msg.title = f"No recommendations found for {username}" | ||||||
| msg.description = "Could not find similar users at this time." | ||||||
|
|
||||||
| elif recommendation_type == "studios": | ||||||
| # Get studios the user is curating | ||||||
| curating = list(islice(user.studios_curating(limit=20), 20)) | ||||||
|
|
||||||
| if not curating: | ||||||
| msg.title = f"No recommendations found for {username}" | ||||||
| msg.description = "This user isn't curating any studios yet!" | ||||||
| await interact.followup.send(embed=msg) | ||||||
| return | ||||||
|
|
||||||
| # Get related studios from the ones they're already in | ||||||
| recommendations = [] | ||||||
| seen_ids = set([studio.id for studio in curating]) | ||||||
|
||||||
| seen_ids = set([studio.id for studio in curating]) | |
| seen_ids = set(studio.id for studio in curating) |
kRxZykRxZy marked this conversation as resolved.
Show resolved
Hide resolved
kRxZykRxZy marked this conversation as resolved.
Show resolved
Hide resolved
kRxZykRxZy marked this conversation as resolved.
Show resolved
Hide resolved
kRxZykRxZy marked this conversation as resolved.
Show resolved
Hide resolved
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.