ryuko-ng/cogs/lockdown.py

100 lines
3.3 KiB
Python
Raw Normal View History

2018-12-23 19:06:32 +00:00
from discord.ext import commands
from discord.ext.commands import Cog
2018-12-23 20:33:03 +00:00
import config
import discord
from helpers.checks import check_if_staff
2018-12-23 19:06:32 +00:00
class Lockdown(Cog):
2018-12-23 19:06:32 +00:00
def __init__(self, bot):
self.bot = bot
async def set_sendmessage(
self, channel: discord.TextChannel, role, allow_send, issuer
):
2019-02-20 22:20:03 +00:00
try:
roleobj = channel.guild.get_role(role)
overrides = channel.overwrites_for(roleobj)
overrides.send_messages = allow_send
await channel.set_permissions(
roleobj, overwrite=overrides, reason=str(issuer)
)
2019-02-20 22:20:03 +00:00
except:
pass
async def unlock_for_staff(self, channel: discord.TextChannel, issuer):
for role in config.staff_role_ids:
2019-02-20 22:20:03 +00:00
await self.set_sendmessage(channel, role, True, issuer)
2018-12-23 20:33:03 +00:00
@commands.guild_only()
@commands.check(check_if_staff)
@commands.command()
async def lock(self, ctx, channel: discord.TextChannel = None, soft: bool = False):
"""Prevents people from speaking in a channel, staff only.
Defaults to current channel."""
if not channel:
channel = ctx.channel
2019-02-04 22:54:40 +00:00
log_channel = self.bot.get_channel(config.modlog_channel)
2018-12-23 20:33:03 +00:00
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"]
2018-12-23 20:33:03 +00:00
for role in roles:
2019-02-20 22:20:03 +00:00
await self.set_sendmessage(channel, role, False, ctx.author)
await self.unlock_for_staff(channel, ctx.author)
2018-12-23 20:33:03 +00:00
public_msg = "🔒 Channel locked down. "
if not soft:
public_msg += (
"Only staff members may speak. "
"Do not bring the topic to other channels or risk "
"disciplinary actions."
)
2018-12-23 20:33:03 +00:00
await ctx.send(public_msg)
2019-01-07 08:49:19 +00:00
safe_name = await commands.clean_content().convert(ctx, str(ctx.author))
msg = (
f"🔒 **Lockdown**: {ctx.channel.mention} by {ctx.author.mention} "
f"| {safe_name}"
)
2018-12-23 20:33:03 +00:00
await log_channel.send(msg)
@commands.guild_only()
@commands.check(check_if_staff)
@commands.command()
async def unlock(self, ctx, channel: discord.TextChannel = None):
2018-12-23 20:33:03 +00:00
"""Unlocks speaking in current channel, staff only."""
if not channel:
channel = ctx.channel
2019-02-04 22:54:40 +00:00
log_channel = self.bot.get_channel(config.modlog_channel)
2018-12-23 20:33:03 +00:00
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"]
2018-12-23 20:33:03 +00:00
await self.unlock_for_staff(channel, ctx.author)
2018-12-23 20:33:03 +00:00
for role in roles:
2019-02-20 22:20:03 +00:00
await self.set_sendmessage(channel, role, True, ctx.author)
2018-12-23 20:33:03 +00:00
2019-01-07 08:49:19 +00:00
safe_name = await commands.clean_content().convert(ctx, str(ctx.author))
2018-12-23 20:33:03 +00:00
await ctx.send("🔓 Channel unlocked.")
msg = (
f"🔓 **Unlock**: {ctx.channel.mention} by {ctx.author.mention} "
f"| {safe_name}"
)
2018-12-23 20:33:03 +00:00
await log_channel.send(msg)
2018-12-23 19:15:44 +00:00
2018-12-23 19:08:34 +00:00
2018-12-23 19:06:32 +00:00
def setup(bot):
2018-12-23 19:15:44 +00:00
bot.add_cog(Lockdown(bot))