diff options
author | 2019-10-15 09:05:05 +1000 | |
---|---|---|
committer | 2019-10-15 09:05:05 +1000 | |
commit | 8040724d1b452b79a16c7df073bd2a8f546570a5 (patch) | |
tree | bc58ef80a3dae6b31832379deb5aa983ed09801f | |
parent | Merge pull request #503 from avayert/master (diff) | |
parent | Merge branch 'master' into master (diff) |
Create the !mention command. (#493)
Create the !mention command.
Co-authored-by: null <[email protected]>
-rw-r--r-- | bot/cogs/utils.py | 50 | ||||
-rw-r--r-- | bot/constants.py | 7 | ||||
-rw-r--r-- | config-default.yml | 4 |
3 files changed, 58 insertions, 3 deletions
diff --git a/bot/cogs/utils.py b/bot/cogs/utils.py index 57f5d6197..793fe4c1a 100644 --- a/bot/cogs/utils.py +++ b/bot/cogs/utils.py @@ -1,15 +1,18 @@ import logging import re import unicodedata +from asyncio import TimeoutError, sleep from email.parser import HeaderParser from io import StringIO from typing import Tuple -from discord import Colour, Embed +from dateutil import relativedelta +from discord import Colour, Embed, Message, Role from discord.ext.commands import Bot, Cog, Context, command -from bot.constants import Channels, STAFF_ROLES -from bot.decorators import in_channel +from bot.constants import Channels, MODERATION_ROLES, Mention, STAFF_ROLES +from bot.decorators import in_channel, with_role +from bot.utils.time import humanize_delta log = logging.getLogger(__name__) @@ -130,6 +133,47 @@ class Utils(Cog): await ctx.send(embed=embed) + @command() + @with_role(*MODERATION_ROLES) + async def mention(self, ctx: Context, *, role: Role) -> None: + """Set a role to be mentionable for a limited time.""" + if role.mentionable: + await ctx.send(f"{role} is already mentionable!") + return + + await role.edit(reason=f"Role unlocked by {ctx.author}", mentionable=True) + + human_time = humanize_delta(relativedelta.relativedelta(seconds=Mention.message_timeout)) + await ctx.send( + f"{role} has been made mentionable. I will reset it in {human_time}, or when someone mentions this role." + ) + + def check(m: Message) -> bool: + """Checks that the message contains the role mention.""" + return role in m.role_mentions + + try: + msg = await self.bot.wait_for("message", check=check, timeout=Mention.message_timeout) + except TimeoutError: + await role.edit(mentionable=False, reason="Automatic role lock - timeout.") + await ctx.send(f"{ctx.author.mention}, you took too long. I have reset {role} to be unmentionable.") + return + + if any(r.id in MODERATION_ROLES for r in msg.author.roles): + await sleep(Mention.reset_delay) + await role.edit(mentionable=False, reason=f"Automatic role lock by {msg.author}") + await ctx.send( + f"{ctx.author.mention}, I have reset {role} to be unmentionable as " + f"{msg.author if msg.author != ctx.author else 'you'} sent a message mentioning it." + ) + return + + await role.edit(mentionable=False, reason=f"Automatic role lock - unauthorised use by {msg.author}") + await ctx.send( + f"{ctx.author.mention}, I have reset {role} to be unmentionable " + f"as I detected unauthorised use by {msg.author} (ID: {msg.author.id})." + ) + def setup(bot: Bot) -> None: """Utils cog load.""" diff --git a/bot/constants.py b/bot/constants.py index 1deeaa3b8..f4f45eb2c 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -475,6 +475,13 @@ class Free(metaclass=YAMLGetter): cooldown_per: float +class Mention(metaclass=YAMLGetter): + section = 'mention' + + message_timeout: int + reset_delay: int + + class RedirectOutput(metaclass=YAMLGetter): section = 'redirect_output' diff --git a/config-default.yml b/config-default.yml index 0dac9bf9f..ca405337e 100644 --- a/config-default.yml +++ b/config-default.yml @@ -347,6 +347,10 @@ free: cooldown_rate: 1 cooldown_per: 60.0 +mention: + message_timeout: 300 + reset_delay: 5 + redirect_output: delete_invocation: true delete_delay: 15 |