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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
import json
import logging
import random
from pathlib import Path
from typing import Optional
import discord
from discord.ext import commands
from fuzzywuzzy import fuzz
from bot import bot
from bot import constants
log = logging.getLogger(__name__)
PRIDE_LEADERS_RESOURCE = Path("bot/resources/pride/prideleader.json")
MINIMUM_FUZZ_RATIO = 40
class PrideLeader(commands.Cog):
"""Gives information about Pride Leaders."""
def __init__(self, bot: bot.Bot):
self.bot = bot
with PRIDE_LEADERS_RESOURCE.open(encoding="utf8") as data:
self.pride = json.load(data)
def invalid_embed_generate(self, pride_leader: str) -> discord.Embed:
"""
Generates Invalid Embed.
The invalid embed contains a list of closely matched names of the invalid pride
leader the user gave. If no closely matched names are found it would list all
the available pride leader names.
Wikipedia is a useful place to learn about pride leaders and we don't have all
the pride leaders, so the bot would add a field containing the wikipedia
command to execute.
"""
embed = discord.Embed(
color=constants.Colours.soft_red
)
valid_names = []
pride_leader = pride_leader.title()
for name in self.pride:
if fuzz.ratio(pride_leader, name) >= MINIMUM_FUZZ_RATIO:
valid_names.append(name)
if not valid_names:
valid_names = ", ".join(self.pride)
error_msg = "Sorry your input didn't match any stored names, here is a list of available names:"
else:
valid_names = "\n".join(valid_names)
error_msg = "Did you mean?"
embed.description = f"{error_msg}\n```{valid_names}```"
embed.add_field(
name="You can get information about the Pride Leader on the Wikipedia command",
value=f"Do `{constants.Client.prefix}wiki {pride_leader}`"
f" in <#{constants.Channels.community_bot_commands}>",
inline=False
)
return embed
def embed_builder(self, leader_name: str) -> discord.Embed:
"""Generate an Embed with information about a pride leader."""
embed = discord.Embed(
title=leader_name,
description=self.pride[leader_name]["About"],
color=constants.Colours.blue
)
embed.add_field(
name="Known for",
value=self.pride[leader_name]["Known for"],
inline=False
)
embed.add_field(
name="D.O.B and Birth place",
value=self.pride[leader_name]["Born"],
inline=False
)
embed.add_field(
name="Awards and honors",
value=self.pride[leader_name]["Awards"],
inline=False
)
embed.add_field(
name="For More Information",
value=f"Do `{constants.Client.prefix}wiki {leader_name}`"
f" in <#{constants.Channels.community_bot_commands}>",
inline=False
)
embed.set_thumbnail(url=self.pride[leader_name]["url"])
return embed
@commands.command(aliases=("pl", "prideleader"))
async def pride_leader(self, ctx: commands.Context, *, pride_leader_name: Optional[str]) -> None:
"""
Information about a Pride Leader.
Returns information about the specified pride leader
and if there is no pride leader given, return a random pride leader.
"""
if not pride_leader_name:
leader = random.choice([name for name in self.pride])
else:
leader = self.pride.get(pride_leader_name.title())
if not leader:
log.trace(f"Got a Invalid pride leader: {pride_leader_name}")
embed = self.invalid_embed_generate(pride_leader_name)
await ctx.send(embed=embed)
return
embed = self.embed_builder(leader)
await ctx.send(embed=embed)
def setup(bot: bot.Bot) -> None:
"""Loads the Pride leader cog."""
bot.add_cog(PrideLeader(bot))
|