Skip to content

Commit

Permalink
Adds basic raid protection. (#610)
Browse files Browse the repository at this point in the history
* Adds basic raid protection.

Initial commit including new command

* Allows sudos to run command too

@Sh3llcod3 requested this

* Combines some logic

Avoids checking bool twice unnecessarily

* Fixes Beano's quibbles: 1) invocator's name in audit log 2) Now not a toggle, :raid will fetch status and :raid [on/off] sets. I also renamed it to just :raid

* Removes useless print statements

* Fixes linter moaning

Co-authored-by: Daniel Milnes <[email protected]>
  • Loading branch information
404dcd and thebeanogamer authored Jan 27, 2021
1 parent fcb6c1a commit 931dc6b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
72 changes: 67 additions & 5 deletions cdbot/cogs/admin.py
Original file line number Diff line number Diff line change
@@ -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
)


Expand Down Expand Up @@ -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 (
Expand All @@ -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
Expand Down Expand Up @@ -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))
2 changes: 1 addition & 1 deletion cdbot/cogs/fun.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cdbot/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down

0 comments on commit 931dc6b

Please sign in to comment.