search.SearchCog.search_school()   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 46
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

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