aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar refisio <[email protected]>2019-02-01 22:18:16 -0500
committerGravatar refisio <[email protected]>2019-02-01 22:18:16 -0500
commit52ca316efdf940edb09a84b70cc8477f47f393b1 (patch)
tree8487bdd2d0f6133ebd6bd031b4110e68f595bda8
parentMerge pull request #296 from python-discord/redirect-decorator-handling-delet... (diff)
Added rules cog.
Adds a command that allows users and staff to pull specific rules from a command instead of getting a basic embed pointing to a website. That functionality still exists, though.
-rw-r--r--bot/cogs/rules.py139
1 files changed, 139 insertions, 0 deletions
diff --git a/bot/cogs/rules.py b/bot/cogs/rules.py
new file mode 100644
index 000000000..6759d07a2
--- /dev/null
+++ b/bot/cogs/rules.py
@@ -0,0 +1,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))