Passed
Push — master ( 58f506...9afc6c )
by Cyb3r
01:28 queued 11s
created

cogs.search.SearchCog.search_state()   A

Complexity

Conditions 2

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nop 4
dl 0
loc 18
rs 10
c 0
b 0
f 0
1
"""Cog responsible for searching for schools and schools in state"""
2
from discord.ext import commands
3
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
    def __init__(self, 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"
28
             "Only returns True or False")
29
    async def validate_school(self, ctx, *, school: str):
30
        """validate_school
31
        ---
32
33
        Validates school name. Only returns true or false.
34
35
        Arguments:
36
            ctx {discord.ext.commands.Context} -- Context of the command.
37
            school {str} -- Name of the school the user wants to validate.
38
        """
39
        await utils.make_embed(ctx, title=str(await utils.school_check(school)))
40
41
    @commands.command(
42
        name="search-school",
43
        help="Search all schools for <school>.\n"
44
             "College, University, Community are blocked as they return a lot of results.\n"
45
             "The full list is at https://github.com/Cyb3r-Jak3/CCC-Bot/blob/master/utils/schools.csv"   # noqa: E501 pylint: disable=line-too-long
46
        )
47
    async def search_school(self, ctx, *, school: str):
48
        """search-school
49
        ---
50
51
        Searches for a school based on the school arguments. It search the school.csv in utils
52
        as a list using the `in` statement.
53
54
        Arguments:
55
        ---
56
            ctx {discord.ext.commands.Context} -- Context of the command.
57
            school {str} -- Part of the name the user wants to search for.
58
        """
59
        blocked_words = ["college", "university", "community", "arts"]
60
        if school.lower in blocked_words:
61
            return await utils.make_embed(ctx, title="Search error", color="FF0000",
62
                                          description="Please refine your search as \"{}\" returns a lot of results ".format(school),  # noqa: E501 pylint: disable=line-too-long
63
                                          )
64
        results = await utils.school_search(school)
65
        if len(results) == 0:
66
            await utils.make_embed(ctx, color="FF0000", title="No results found.")
67
        else:
68
            await utils.list_message(ctx, results, "Search Results:\n")
69
70
    @commands.command(name="search-state")
71
    async def search_state(self, ctx, *, state: str):
72
        """search_state
73
        ---
74
75
        Returns all schools in a state.
76
77
        Arguments:
78
        ---
79
            ctx {discord.ext.commands.Context} -- Context of the command.
80
            state {str} -- Name of the state that the user wants to get schools from.
81
        """
82
        schools = await utils.state_list(state)
83
        if len(schools) == 0:
84
            await utils.make_embed(ctx, color="FF0000", title="No results found.")
85
        else:
86
            title = "Schools in State '{}'".format(state.title())
87
            await utils.list_message(ctx, schools, title)
88
89
90
def setup(bot):
91
    """Needed for extension loading"""
92
    bot.add_cog(SearchCog(bot))
93