From 3400b94c7cc6e0faa2b04c32287f6b5dafa192fb Mon Sep 17 00:00:00 2001 From: Robin Lambertz Date: Fri, 1 Mar 2019 07:07:47 +0100 Subject: [PATCH] Generic changes (#26) * Make lockdown configuration more generic * pin: Add reaction pinning functionality --- Robocop.py | 3 +- cogs/lockdown.py | 22 ++++++++------- cogs/pin.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++ config_template.py | 17 ++++++++++++ 4 files changed, 99 insertions(+), 11 deletions(-) create mode 100644 cogs/pin.py diff --git a/Robocop.py b/Robocop.py index 10447c4..c2b69ca 100755 --- a/Robocop.py +++ b/Robocop.py @@ -58,7 +58,8 @@ initial_extensions = ['cogs.common', 'cogs.links', 'cogs.remind', 'cogs.robocronp', - 'cogs.meme'] + 'cogs.meme', + 'cogs.pin'] bot = commands.Bot(command_prefix=get_prefix, description=config.bot_description, pm_help=True) diff --git a/cogs/lockdown.py b/cogs/lockdown.py index 33d9a8d..40e99aa 100644 --- a/cogs/lockdown.py +++ b/cogs/lockdown.py @@ -36,11 +36,12 @@ class Lockdown: channel = ctx.channel log_channel = self.bot.get_channel(config.modlog_channel) - if channel.id in config.community_channels: - roles = [config.named_roles["community"], - config.named_roles["hacker"]] - else: - roles = [config.named_roles["participant"]] + for key, lockdown_conf in config.lockdown_configs.items(): + if channel.id in lockdown_conf["channels"]: + roles = lockdown_conf["roles"] + + if roles is None: + roles = config.lockdown_configs["default"]["roles"] for role in roles: await self.set_sendmessage(channel, role, False, ctx.author) @@ -68,11 +69,12 @@ class Lockdown: channel = ctx.channel log_channel = self.bot.get_channel(config.modlog_channel) - if ctx.channel.id in config.community_channels: - roles = [config.named_roles["community"], - config.named_roles["hacker"]] - else: - roles = [config.named_roles["participant"]] + for key, lockdown_conf in config.lockdown_configs.items(): + if channel.id in lockdown_conf["channels"]: + roles = lockdown_conf["roles"] + + if roles is None: + roles = config.lockdown_configs["default"]["roles"] await self.unlock_for_staff(channel, ctx.author) diff --git a/cogs/pin.py b/cogs/pin.py new file mode 100644 index 0000000..7fecbf0 --- /dev/null +++ b/cogs/pin.py @@ -0,0 +1,68 @@ +import config +import pprint +from discord.enums import MessageType + +class Pin: + """ + Allow users to pin things + """ + def __init__(self, bot): + self.bot = bot + + # Use raw_reaction to allow pinning old messages. + async def on_raw_reaction_add(self, payload): + # TODO: handle more than 50 pinned message + # BODY: If there are more than 50 pinned messages, + # BODY: we should move the oldest pin to a pinboard + # BODY: channel to make room for the new pin. + # BODY: This is why we use the pin reaction to remember + # BODY: that a message is pinned. + + # Check that the user wants to pin this message + if payload.emoji.name not in ["📌", "📍"]: + return + + # Check that reaction pinning is allowd in this channel + if payload.channel_id not in config.allowed_pin_channels: + return + + target_guild = self.bot.get_guild(payload.guild_id) + if target_guild is None: + return + + # Check that the user is allowed to reaction-pin + target_user = target_guild.get_member(payload.user_id) + for role in config.staff_role_ids + config.allowed_pin_roles: + if role in [role.id for role in target_user.roles]: + target_chan = self.bot.get_channel(payload.channel_id) + target_msg = await target_chan.get_message(payload.message_id) + + # Check that the message hasn't already been pinned + for reaction in target_msg.reactions: + if reaction.emoji == "📌": + if reaction.me: + return + break + + # Wait for the automated "Pinned" message so we can delete it + waitable = self.bot.wait_for('message', check=check) + + # Pin the message + await target_msg.pin() + + # Delete the automated Pinned message + msg = await waitable + await msg.delete() + + # Add a Pin reaction so we remember that the message is pinned + await target_msg.add_reaction("📌") + + # Send custom pinned message + await target_chan.send("Pinned!") + +def check(msg): + return msg.type is MessageType.pins_add + + +def setup(bot): + bot.add_cog(Pin(bot)) diff --git a/config_template.py b/config_template.py index 325e2bf..65d2c86 100644 --- a/config_template.py +++ b/config_template.py @@ -66,6 +66,19 @@ community_channels = [269333940928512010, 435687501068501002, 286612533757083648] # Channels requiring community role +# Controls which roles are blocked during lockdown +lockdown_configs = { + # Used as a default value for channels without a config + "default": { + "channels": general_channels, + "roles": [named_roles["participant"]] + }, + "community": { + "channels": community_channels, + "roles": [named_roles["community"], named_roles["hacker"]] + } +} + # Mute role is applied to users when they're muted # As we no longer have mute role on ReSwitched, I set it to 0 here mute_role = 0 # Mute role in ReSwitched @@ -76,3 +89,7 @@ hourly_clean_channels = [] # Edited and deletes messages in these channels will be logged spy_channels = general_channels + +# Channels and roles where users can pin messages +allowed_pin_channels = [] +allowed_pin_roles = []