diff options
author | 2019-08-15 19:07:29 -0400 | |
---|---|---|
committer | 2019-08-15 19:07:29 -0400 | |
commit | d39f4b59474b2c67489c0e33a74a764894bd8a4c (patch) | |
tree | e141dc1ba100702642d7ad2e0271922ba9eb01eb | |
parent | edited json files, added new resource, small tweaks to Embed (diff) |
moved populating game_recs to module level, added an index to iterate game_recs, edit docstring, added aliases
-rw-r--r-- | bot/seasons/evergreen/recommend_game.py | 36 |
1 files changed, 19 insertions, 17 deletions
diff --git a/bot/seasons/evergreen/recommend_game.py b/bot/seasons/evergreen/recommend_game.py index 45864336..1c81ea39 100644 --- a/bot/seasons/evergreen/recommend_game.py +++ b/bot/seasons/evergreen/recommend_game.py @@ -1,6 +1,5 @@ import json import logging -import os from pathlib import Path from random import shuffle @@ -8,25 +7,36 @@ import discord from discord.ext import commands log = logging.getLogger(__name__) -DIR = "bot/resources/evergreen/game_recs" +DIR = Path("bot/resources/evergreen/game_recs") game_recs = [] +# Populate the list `game_recs` with resource files +for file_url in DIR.glob("*.json"): + with Path(file_url).open(encoding='utf-8') as file: + data = json.load(file) + game_recs.append(data) +shuffle(game_recs) + class RecommendGame(commands.Cog): """Commands related to recommending games.""" def __init__(self, bot): self.bot = bot - self.populate_recs() + self.index = 0 - @commands.command(name="recommend_game") + @commands.command(name="recommend_game", aliases=['gameRec', 'recommendGame']) async def recommend_game(self, ctx): """Sends an Embed of a random game recommendation.""" - if not game_recs: - self.populate_recs() - game = game_recs.pop() + if self.index < len(game_recs): + game = game_recs[self.index] + self.index += 1 else: - game = game_recs.pop() + self.index = 0 + shuffle(game_recs) + game = game_recs[self.index] + self.index += 1 + author = self.bot.get_user(int(game['author'])) # Creating and formatting Embed @@ -37,16 +47,8 @@ class RecommendGame(commands.Cog): await ctx.send(embed=embed) - def populate_recs(self): - """Populates the list `game_recs` from resources.""" - for file_url in os.listdir(DIR): - with Path(DIR, file_url).open(encoding='utf-8') as file: - data = json.load(file) - game_recs.append(data) - shuffle(game_recs) - def setup(bot): """Loads the RecommendGame cog.""" bot.add_cog(RecommendGame(bot)) - log.info("Recommend_Game cog loaded") + log.info("RecommendGame cog loaded") |