aboutsummaryrefslogtreecommitdiffstats
path: root/bot/cogs/rules.py
blob: 208607573dc7d25c7e9487d3e16cecf9af8c42dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import re
from typing import Optional

from bot.constants import Channels, Roles
from bot.decorators import redirect_output
from bot.pagination import LinePaginator

from discord import Colour, Embed
from discord.ext.commands import Bot, Context, command


STAFF = [
    Roles.admin,
    Roles.moderator,
    Roles.owner,
]


class Rules:

    def __init__(self, bot: Bot):
        self.bot = bot
        # We'll get the rules from the API when the
        # site has been updated to the Django Framwork.
        # Hard-code the rules for now until the new RulesView is released.
        self.rules = [
            "Be polite, and do not spam.",
            "Follow the [Discord Community Guidelines]",
            "(https://discordapp.com/guidelines).",
            (
                "Don't intentionally make other people uncomfortable - if "
                "someone asks you to stop discussing something,"
                " you should stop."
            ),
            (
                "Be patient both with users asking "
                "questions, and the users answering them."
            ),
            (
                "We will not help you with anything that might break "
                "a law or the terms of service of any other community, site, "
                "service, or otherwise - No piracy, brute-forcing, captcha "
                "circumvention, sneaker bots, or anything else of that nature."
            ),
            (
                "Listen to and respect the staff members - we're "
                "here to help, but we're all human beings."
            ),
            (
                "All discussion should be kept within the relevant "
                "channels for the subject - See the "
                f"[channels page](https://pythondiscord.com/about/channels) " 
                "for more information."
            ),
            (
                "This is an English-speaking server, so please speak English "
                f"to the best of your ability - [Google Translate]"
                "(https://translate.google.com/) should be fine if "
                "you're not sure."
            ),
            (
                "Keep all discussions safe for work - No gore, nudity, sexual "
                "soliciting, references to suicide, or anything else of "
                "that nature"
            ),
            (
                "We do not allow advertisements for communities (including "
                "other Discord servers) or commercial projects - Contact "
                "us directly if you want to discuss a partnership!"
            )
        ]
        self.default_desc = "The rules and guidelines that apply to this " \
                            "community can be found on our [rules page]" \
                            "(https://pythondiscord.com/about/rules). We " \
                            "expect all members of the community to have " \
                            "read and understood these."

    @command(
        aliases=['r', 'rule'],
        name='rules',
    )
    @redirect_output(
        destination_channel=Channels.bot,
        bypass_roles=STAFF)
    async def rules_command(
            self, ctx: Context, *, rules: Optional[str] = None):
        """
        Provides a link to the `rules` endpoint of the website, or displays
        specific rules, if they are requested.
        :param ctx: The Discord message context
        :param rules: The rules a user wants to get.
        :return:
        """
        rules_embed = Embed(
            title='Rules',
            color=Colour.blurple()
        )
        rules_embed.set_footer(text='https://pythondiscord.com/about/rules')

        if not rules:
            # Rules were not submitted. Return the default description.
            rules_embed.description = self.default_desc
            return await ctx.send(embed=rules_embed)

        # Regex magic time.
        # This code block will do the following:
        # Split the rules string across the provided characters ('/', ',', ' ')
        # Returns a list of numbers based on if the numbers correspond to item
        # indices in the rules list
        rules_to_get = []
        split_rules = re.split(r'[/, ]', rules)
        for item in split_rules:
            if not item.isdigit():
                if not item:
                    continue
                rule_match = re.search(r'\d?\d[:|-]1?\d', item)
                if rule_match:
                    a, b = sorted(
                        [int(x)-1 for x in re.split(r'[:-]',
                                                    rule_match.group())])
                    rules_to_get.extend(range(a, b+1)
                                        )
            else:
                rules_to_get.append(int(item)-1)
        final_rules = [f'Rule {i+1}: {self.rules[i]}'
                       for i in sorted(rules_to_get) if i < len(self.rules)]

        if not final_rules:
            # Param rules was processed, but no rules were matched.
            # Return the default description.
            rules_embed.description = self.default_desc
            return await ctx.send(embed=rules_embed)
        await LinePaginator.paginate(
            final_rules, ctx, rules_embed, max_lines=3
        )


def setup(bot):
    bot.add_cog(Rules(bot))