Skip to content

Commit

Permalink
make presence pick randomly from games.json instead of last played
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroquinc committed Sep 26, 2024
1 parent 63d8e67 commit 59f51dc
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/presence.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import discord
import json
import random
from services.api import UserProfile, GameDetails
from utils.custom_logger import logger

async def process_presence(bot, user, api_username, api_key):
"""Process the presence of a user in Discord by setting their rich presence based on the last game they played.
"""Process the presence of a user in Discord by randomly setting their rich presence from games.json or adding new games.
Args:
bot: The Discord bot instance.
Expand All @@ -22,6 +23,7 @@ async def process_presence(bot, user, api_username, api_key):
await process_presence(bot_instance, user_instance, 'api_username', 'api_key')
"""
try:
# Fetch user profile and last game played
user_profile = UserProfile(user, api_username, api_key)
profile = user_profile.get_profile()
last_game_id = profile.last_game_id
Expand All @@ -33,22 +35,33 @@ def get_or_fetch_game(game_id):
except FileNotFoundError:
games = {}

# If the game is not in games.json, fetch and add it
if str(game_id) not in games:
logger.info(f"Fetching game ID {game_id} from API.")
game_details = GameDetails(game_id, api_username, api_key)
game = game_details.get_game()
games[str(game_id)] = {"title": game.title, "platform": game.remap_console_name()}

# Save the new game to games.json
with open('games.json', 'w') as f:
json.dump(games, f, indent=4)
else:
logger.info(f"Game ID {game_id} found in JSON.")
return games[str(game_id)]

return games

game_data = get_or_fetch_game(last_game_id)
# Fetch or add the last played game to games.json
games = get_or_fetch_game(last_game_id)

# Pick a random game from the updated games.json
random_game_id = random.choice(list(games.keys()))
game_data = games[random_game_id]
game_title = game_data["title"]
game_platform = game_data["platform"]

# Set rich presence to a randomly selected game
await bot.change_presence(activity=discord.Game(name=f"{game_title} ({game_platform})"))
logger.info(f"Setting rich presence for {user} to {game_title} ({game_platform})")

except Exception as e:
logger.error(f'Error processing user {user}: {e}')

0 comments on commit 59f51dc

Please sign in to comment.