blob: 5e262a5b5824e3fe0b2ea918ec1c593d2a0edcd4 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
import json
import logging
from pathlib import Path
from random import shuffle
import discord
from discord.ext import commands
log = logging.getLogger(__name__)
game_recs = []
# Populate the list `game_recs` with resource files
for rec_path in Path("bot/resources/evergreen/game_recs").glob("*.json"):
with rec_path.open(encoding='utf8') 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: commands.Bot) -> None:
self.bot = bot
self.index = 0
@commands.command(name="recommendgame", aliases=['gamerec'])
async def recommend_game(self, ctx: commands.Context) -> None:
"""Sends an Embed of a random game recommendation."""
if self.index >= len(game_recs):
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
embed = discord.Embed(color=discord.Colour.blue())
if author is not None:
embed.set_author(name=author.name, icon_url=author.avatar_url)
embed.set_image(url=game['image'])
embed.add_field(name='Recommendation: ' + game['title'] + '\n' + game['link'], value=game['description'])
await ctx.send(embed=embed)
def setup(bot: commands.Bot) -> None:
"""Loads the RecommendGame cog."""
bot.add_cog(RecommendGame(bot))
|