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

bot.cogs.search   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 48
dl 0
loc 111
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 17 2
A SearchCog.search_school() 0 44 4
A SearchCog.validate_school() 0 16 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
    """SearchCog
8
    ---
9
10
    Cog that deals with searching for schools by name and by state.
11
12
    Commands:
13
    ---
14
        `validate-school`: Allows users to see if the school they select is valid.
15
        `search-school`: Allows users to search all valid schools.
16
        `search-state`: Allows users to see all valid schools per state.
17
18
    Arguments:
19
    ---
20
        bot {discord.commands.Bot} -- The bot
21
    """
22
23
    def __init__(self, bot: commands.Bot):
24
        self.bot = bot
25
26
    @commands.command(
27
        name="check-school",
28
        help="Checks to see if <school> exists in the csv.\n" "Only returns True or False",
29
    )
30
    async def validate_school(self, ctx: commands.Context, *, school: str):
31
        """validate_school
32
        ---
33
34
        Validates school name. Only returns true or false.
35
36
        Arguments:
37
            ctx {discord.ext.commands.Context} -- Context of the command.
38
            school {str} -- Name of the school the user wants to validate.
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):
52
        """search-school
53
        ---
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
        Arguments:
59
        ---
60
            ctx {discord.ext.commands.Context} -- Context of the command.
61
            school {str} -- Part of the name the user wants to search for.
62
        """
63
        blocked_words = [
64
            "college",
65
            "university",
66
            "community",
67
            "arts",
68
            "technology",
69
            "institute",
70
        ]
71
        if school.lower() in blocked_words:
72
            return utils.error_message(
73
                ctx,
74
                f"Please refine your search as '{school}' returns a lot of results.",
75
                title="Search Error",
76
            )
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
        """search_state
92
        ---
93
94
        Returns all schools in a state.
95
96
        Arguments:
97
        ---
98
            ctx {discord.ext.commands.Context} -- Context of the command.
99
            state {str} -- Name of the state that the user wants to get schools from.
100
        """
101
        state = state.strip("\"'")
102
        schools = await utils.state_list(self.bot.school_list, state)
103
        if not any(schools):
104
            return await utils.error_message(ctx, message="No results found.")
105
        return await utils.list_message(ctx, schools, f"Schools in State '{state.title()}'")
106
107
108
def setup(bot):
109
    """Needed for extension loading"""
110
    bot.add_cog(SearchCog(bot))
111