1
|
|
|
"""Cog responsible for searching for schools and schools in state""" |
2
|
|
|
from discord.ext import commands |
3
|
|
|
from bot import utils |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class SearchCog(commands.Cog, name="Search"): |
7
|
|
|
""" |
8
|
|
|
Search Cog |
9
|
|
|
|
10
|
|
|
Cog that deals with searching for schools by name and by state. |
11
|
|
|
|
12
|
|
|
**Commands:** |
13
|
|
|
- `validate-school`: Allows users to see if the school they select is valid. |
14
|
|
|
|
15
|
|
|
- `search-school`: Allows users to search all valid schools. |
16
|
|
|
|
17
|
|
|
- `search-state`: Allows users to see all valid schools per state. |
18
|
|
|
|
19
|
|
|
""" |
20
|
|
|
|
21
|
|
|
def __init__(self, bot: commands.Bot): |
22
|
|
|
self.bot = bot |
23
|
|
|
|
24
|
|
|
@commands.command( |
25
|
|
|
name="check-school", |
26
|
|
|
help="Checks to see if <school> exists in the csv.\n" "Only returns True or False", |
27
|
|
|
) |
28
|
|
|
async def validate_school(self, ctx: commands.Context, *, school: str) -> None: |
29
|
|
|
""" |
30
|
|
|
Validate school |
31
|
|
|
|
32
|
|
|
Validates school name. Only returns true or false. |
33
|
|
|
|
34
|
|
|
:param ctx: Command context |
35
|
|
|
:type ctx: discord.ext.commands.Context |
36
|
|
|
:param school: Name of school the member wants to validate. |
37
|
|
|
:type school: str |
38
|
|
|
:return: None |
39
|
|
|
""" |
40
|
|
|
await utils.make_embed( |
41
|
|
|
ctx, title=str(await utils.school_check(self.bot.school_list, school)) |
42
|
|
|
) |
43
|
|
|
|
44
|
|
|
@commands.command( |
45
|
|
|
name="search-school", |
46
|
|
|
aliases=["search-schools"], |
47
|
|
|
help="Search all schools for <school>.\n" |
48
|
|
|
"College, University, Community are blocked as they return a lot of results.\n" |
49
|
|
|
"The full list is at https://github.com/Competitive-Cyber-Clubs/School-List/blob/master/school_list.csv", # noqa: E501 pylint: disable=line-too-long |
50
|
|
|
) |
51
|
|
|
async def search_school(self, ctx: commands.Context, *, school: str) -> None: |
52
|
|
|
""" |
53
|
|
|
Search school |
54
|
|
|
|
55
|
|
|
Searches for a school based on the school arguments. It search the school.csv in utils |
56
|
|
|
as a list using the `in` statement. |
57
|
|
|
|
58
|
|
|
:param ctx: Command context |
59
|
|
|
:type ctx: discord.ext.commands.Context |
60
|
|
|
:param school: Part of the name the member wants to search for. |
61
|
|
|
:type school: str |
62
|
|
|
:return: None |
63
|
|
|
""" |
64
|
|
|
blocked_words = [ |
65
|
|
|
"college", |
66
|
|
|
"university", |
67
|
|
|
"community", |
68
|
|
|
"arts", |
69
|
|
|
"technology", |
70
|
|
|
"institute", |
71
|
|
|
] |
72
|
|
|
if school.lower() in blocked_words: |
73
|
|
|
await utils.error_message( |
74
|
|
|
ctx, |
75
|
|
|
f"Please refine your search as '{school}' returns a lot of results.", |
76
|
|
|
title="Search Error", |
77
|
|
|
) |
78
|
|
|
return |
79
|
|
|
results = await utils.school_search(self.bot.school_list, school) |
80
|
|
|
created_roles = await utils.fetch("schools", "school") |
81
|
|
|
if not results: |
82
|
|
|
await utils.error_message(ctx, "No results found") |
83
|
|
|
else: |
84
|
|
|
for place, results_name in enumerate(results): |
85
|
|
|
results[place] = "{} :: Role created: {}".format( |
86
|
|
|
results_name, |
87
|
|
|
await utils.TF_emoji(results_name in created_roles), |
88
|
|
|
) |
89
|
|
|
await utils.list_message(ctx, results, "Search Results:\n") |
90
|
|
|
|
91
|
|
|
@commands.command(name="search-state") |
92
|
|
|
async def search_state(self, ctx: commands.Context, *, state: str): |
93
|
|
|
""" |
94
|
|
|
Search state |
95
|
|
|
|
96
|
|
|
Returns all schools in a state. |
97
|
|
|
|
98
|
|
|
:param ctx: Command context |
99
|
|
|
:type ctx: discord.ext.commands.Context |
100
|
|
|
:param state: Name of the state that the member wants to get schools from. |
101
|
|
|
:type state: str |
102
|
|
|
:return: None |
103
|
|
|
""" |
104
|
|
|
state = state.strip("\"'") |
105
|
|
|
schools = await utils.state_list(self.bot.school_list, state) |
106
|
|
|
if not any(schools): |
107
|
|
|
await utils.error_message(ctx, message="No results found.") |
108
|
|
|
return |
109
|
|
|
await utils.list_message(ctx, schools, f"Schools in State '{state.title()}'") |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
def setup(bot): |
113
|
|
|
"""Needed for extension loading""" |
114
|
|
|
bot.add_cog(SearchCog(bot)) |
115
|
|
|
|