Add channel locks and server lockdown, closes #11

This commit is contained in:
Zeva Rose 2021-07-08 07:59:10 -06:00
parent d2e40a4208
commit 398fa14ca3
3 changed files with 160 additions and 3 deletions

View file

@ -181,6 +181,37 @@ async def unban():
db.jarvis.bans.bulk_write(updates) db.jarvis.bans.bulk_write(updates)
@loop(minutes=1)
async def unlock():
db = DBManager(get_config().mongo).mongo
locks = list(db.jarvis.locks.find({"active": True}))
updates = []
for lock in locks:
if lock["time"] + timedelta(minutes=lock["duration"]) < datetime.now():
guild = await jarvis.fetch_guild(lock["guild"])
channel = await jarvis.fetch_channel(lock["channel"])
if channel:
roles = await guild.fetch_roles()
for role in roles:
overrides = channel.overwrites_for(role)
overrides.send_messages = None
await channel.set_permissions(
role, overwrite=overrides, reason="Lock expired"
)
updates.append(
pymongo.UpdateOne(
{
"channel": channel.id,
"guild": guild.id,
"time": lock["time"],
},
{"$set": {"active": False}},
)
)
if updates:
db.jarvis.locks.bulk_write(updates)
def run(ctx=None): def run(ctx=None):
global restart_ctx global restart_ctx
if ctx: if ctx:
@ -196,6 +227,7 @@ def run(ctx=None):
) )
unmute.start() unmute.start()
unban.start() unban.start()
unlock.start()
jarvis.max_messages = config.max_messages jarvis.max_messages = config.max_messages
jarvis.run(config.token, bot=True, reconnect=True) jarvis.run(config.token, bot=True, reconnect=True)
for cog in jarvis.cogs: for cog in jarvis.cogs:

View file

@ -2,7 +2,7 @@ import re
from datetime import datetime, timedelta from datetime import datetime, timedelta
import pymongo import pymongo
from discord import Member, User from discord import Member, Role, TextChannel, User
from discord.ext import commands from discord.ext import commands
from discord.utils import find, get from discord.utils import find, get
from discord_slash import SlashContext, cog_ext from discord_slash import SlashContext, cog_ext
@ -552,6 +552,123 @@ class AdminCog(commands.Cog):
) )
await ctx.send(f"{user.mention} has been unmuted.") await ctx.send(f"{user.mention} has been unmuted.")
async def _lock_channel(
self,
channel: TextChannel,
role: Role,
admin: User,
reason: str,
allow_send=False,
):
overrides = channel.overwrites_for(role)
overrides.send_messages = allow_send
await channel.set_permissions(role, overwrite=overrides, reason=reason)
@cog_ext.cog_slash(
name="lock",
description="Locks a channel",
guild_ids=[418094694325813248, 578757004059738142, 862402786116763668],
options=[
create_option(
name="reason",
description="Lock Reason",
option_type=3,
required=True,
),
create_option(
name="duration",
description="Lock duration in minutes (default 10)",
option_type=4,
required=False,
),
create_option(
name="channel",
description="Channel to lock",
option_type=7,
required=False,
),
],
)
async def _lock(
self,
ctx: SlashContext,
reason: str,
duration: int = 10,
channel: TextChannel = None,
):
await ctx.defer()
if not channel:
channel = ctx.channel
for role in ctx.guild.roles:
try:
await self._lock_channel(channel, role, ctx.author, reason)
except Exception:
continue # Just continue on error
self.db.jarvis.locks.insert_one(
{
"channel": channel.id,
"guild": ctx.guild.id,
"admin": ctx.author.id,
"reason": reason,
"duration": duration,
"active": True,
"time": datetime.now(),
}
)
await ctx.send(f"{channel.mention} locked for {duration} minute(s)")
@cog_ext.cog_slash(
name="lockdown",
description="Locks a server",
guild_ids=[418094694325813248, 578757004059738142, 862402786116763668],
options=[
create_option(
name="reason",
description="Lockdown Reason",
option_type=3,
required=True,
),
create_option(
name="duration",
description="Lockdown duration in minutes (default 10)",
option_type=4,
required=False,
),
],
)
async def _lockdown(
self,
ctx: SlashContext,
reason: str,
duration: int = 10,
):
await ctx.defer()
channels = ctx.guild.channels
roles = ctx.guild.roles
updates = []
for channel in channels:
for role in roles:
try:
await self._lock_channel(channel, role, ctx.author, reason)
except Exception:
continue # Just continue on error
updates.append(
pymongo.InsertOne(
{
"channel": channel.id,
"guild": ctx.guild.id,
"admin": ctx.author.id,
"reason": reason,
"duration": duration,
"active": True,
"time": datetime.now(),
}
)
)
if updates:
self.db.jarvis.locks.bulk_write(updates)
await ctx.send(f"Server locked for {duration} minute(s)")
def setup(bot): def setup(bot):
bot.add_cog(AdminCog(bot)) bot.add_cog(AdminCog(bot))

View file

@ -358,8 +358,16 @@ class ModlogCog(commands.Cog):
if modlog: if modlog:
channel = before.guild.get_channel(modlog["value"]) channel = before.guild.get_channel(modlog["value"])
fields = [ fields = [
Field("Original Message", before.content, False), Field(
Field("New Message", after.content, False), "Original Message",
before.content if before.content else "N/A",
False,
),
Field(
"New Message",
after.content if after.content else "N/A",
False,
),
] ]
embed = build_embed( embed = build_embed(
title="Message Edited", title="Message Edited",