Passed
Pull Request — master (#50)
by Cyb3r
01:15
created

bot.cogs.search   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A SearchCog.__init__() 0 2 1
A SearchCog.search_state() 0 18 2
A SearchCog.search_school() 0 45 4
A SearchCog.validate_school() 0 17 1

1 Function

Rating   Name   Duplication   Size   Complexity  
A setup() 0 3 1
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
        :param school: Name of school the member wants to validate.
36
        :type school: str
37
        :return: None
38
        """
39
        await utils.make_embed(
40
            ctx, title=str(await utils.school_check(self.bot.school_list, school))
41
        )
42
43
    @commands.command(
44
        name="search-school",
45
        aliases=["search-schools"],
46
        help="Search all schools for <school>.\n"
47
        "College, University, Community are blocked as they return a lot of results.\n"
48
        "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
49
    )
50
    async def search_school(self, ctx: commands.Context, *, school: str) -> None:
51
        """
52
        Search school
53
54
        Searches for a school based on the school arguments. It search the school.csv in utils
55
        as a list using the `in` statement.
56
57
        :param ctx: Command context
58
        :param school: Part of the name the member wants to search for.
59
        :type school: str
60
        :return: None
61
        """
62
        blocked_words = [
63
            "college",
64
            "university",
65
            "community",
66
            "arts",
67
            "technology",
68
            "institute",
69
        ]
70
        if school.lower() in blocked_words:
71
            await utils.error_message(
72
                ctx,
73
                f"Please refine your search as '{school}' returns a lot of results.",
74
                title="Search Error",
75
            )
76
            return
77
        results = await utils.school_search(self.bot.school_list, school)
78
        created_roles = await utils.fetch("schools", "school")
79
        if not results:
80
            await utils.error_message(ctx, "No results found")
81
        else:
82
            for place, results_name in enumerate(results):
83
                results[place] = "{} :: Role created: {}".format(
84
                    results_name,
85
                    await utils.TF_emoji(results_name in created_roles),
86
                )
87
            await utils.list_message(ctx, results, "Search Results:\n")
88
89
    @commands.command(name="search-state")
90
    async def search_state(self, ctx: commands.Context, *, state: str):
91
        """
92
        Search state
93
94
        Returns all schools in a state.
95
96
        :param ctx: Command context
97
        :param state: Name of the state that the member wants to get schools from.
98
        :type state: str
99
        :return: None
100
        """
101
        state = state.strip("\"'")
102
        schools = await utils.state_list(self.bot.school_list, state)
103
        if not any(schools):
104
            await utils.error_message(ctx, message="No results found.")
105
            return
106
        await utils.list_message(ctx, schools, f"Schools in State '{state.title()}'")
107
108
109
def setup(bot):
110
    """Needed for extension loading"""
111
    bot.add_cog(SearchCog(bot))
112