diff --git a/cdbot/cogs/admin.py b/cdbot/cogs/admin.py index daa30829..6da7ace7 100644 --- a/cdbot/cogs/admin.py +++ b/cdbot/cogs/admin.py @@ -1,15 +1,18 @@ import re -from discord import AuditLogAction, Member -from discord.ext.commands import Bot, Cog +from discord import AuditLogAction, Colour, Embed, Member +from discord.ext.commands import Bot, Cog, Context, command, has_any_role from cdbot.constants import ( ADMIN_MENTOR_ROLE_ID, ADMIN_ROLES, CD_BOT_ROLE_ID, + LOGGING_CHANNEL_ID, NICKNAME_PATTERNS, PLACEHOLDER_NICKNAME, + ROOT_ROLE_ID, STATIC_NICKNAME_ROLE_ID, + SUDO_ROLE_ID ) @@ -39,8 +42,6 @@ async def on_member_update(self, member_before: Member, member_after: Member): # if this entry was to the user in question, and was this specific nickname change if entry.target == member_before and entry.after.nick == member_after.nick: corresponding_audit_entry = entry - print(entry.user) - print(entry.user.roles) break if ( @@ -58,7 +59,6 @@ async def on_member_update(self, member_before: Member, member_after: Member): ) if not (admin_role_check or bot_role_check or mentor_role_check): for i in member_after.roles: - print(i.id) if i.id == STATIC_NICKNAME_ROLE_ID: # user has Static Name role await member_after.edit( nick=member_before.display_name @@ -89,6 +89,68 @@ async def on_member_join(self, member: Member): # assign placeholder nickname await member.edit(nick=PLACEHOLDER_NICKNAME) + @command() + @has_any_role(ROOT_ROLE_ID, SUDO_ROLE_ID) + async def raid( + self, + ctx: Context, + operand: str = "" + ): + """ + Allows an admin user to lock down the server in case of a raid. + This command toggles invite link generation for @everyone and + revokes all existing invite links. + """ + + everyone = ctx.channel.guild.default_role + perms = everyone.permissions + enabled = not perms.create_instant_invite + logs_channel = self.bot.get_channel(LOGGING_CHANNEL_ID) + + operand = operand.lower() + boolonoff = ("OFF", "ON") + + action = True + embed = None + + if not operand: # status query + await ctx.send(f"Raid protection currently {boolonoff[enabled]}. Use `:raid [on/off]` to toggle.") + action = False + + elif operand in ("on", "yes") and not enabled: # need to turn it on + enabled = True + perms.update(create_instant_invite=False) + embed = Embed( + color=Colour.blue(), + title="Raid Protection ON.", + description=("Raid protection now ON - All invite links were" + " deleted and members may not create new ones") + ) + for invite in await ctx.channel.guild.invites(): # delete links + await invite.delete() + + elif operand in ("off", "no") and enabled: + enabled = False + perms.update(create_instant_invite=True) + embed = Embed( + color=Colour.blue(), + title="Raid Protection OFF.", + description=("Raid protection now OFF - Members can now create" + " new invite links") + ) + + else: # no changes + await ctx.send(f"Raid protection {boolonoff[enabled]}, nothing was changed.") + action = False + + if action: # if we toggled it + msg = f"{ctx.author.name} toggled raid protection {boolonoff[enabled]}." + await everyone.edit(reason=msg, permissions=perms) # make the perm change + await ctx.send(msg) # direct response to invocation + + embed.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url) + await logs_channel.send(embed=embed) # log the event + def setup(bot): bot.add_cog(Admin(bot)) diff --git a/cdbot/cogs/fun.py b/cdbot/cogs/fun.py index 97fcfd99..7146a30a 100644 --- a/cdbot/cogs/fun.py +++ b/cdbot/cogs/fun.py @@ -5,8 +5,8 @@ import textwrap from io import BytesIO from math import ceil -from random import randint import re +from random import randint from string import ascii_lowercase from typing import List from urllib.parse import urlencode diff --git a/cdbot/constants.py b/cdbot/constants.py index da152cee..ddd2bfef 100644 --- a/cdbot/constants.py +++ b/cdbot/constants.py @@ -108,7 +108,7 @@ class Exchange: REACT_EMOTES = ["\N{ONCOMING POLICE CAR}", "\N{DUCK}", "\U0001f645 \N{NO ENTRY} \N{CROSSED SWORDS}"] REACT_TRIGGERS = {"kali": REACT_EMOTES[0], "duck": REACT_EMOTES[1], "cybergame": "*CyberStart Game", "cyberstart access": "*CyberStart Assess", "13.1": REACT_EMOTES[2]} -WORD_MATCH_RE = r"^.*\b{}\b.*$" +WORD_MATCH_RE = r"^.*\b{}\b.*$" # noqa # General constants WELCOME_MESSAGE = ("Welcome to the Cyber Discovery discussion discord! Before you begin, please check the "