<commit_before>from logging import getLogger

from discord.ext.commands import bot_has_permissions, group, guild_only, has_permissions

from ..utils import maybe_send
from .basecog import BaseCog

logger = getLogger(__name__)


class Stop(BaseCog):
    """
    Utilities for locking down chat channels.
    """

    def __init__(self, bot):
        super().__init__(bot)
        self.perm_cache = {}

    @group()
    @guild_only()
    @has_permissions(manage_channels=True)
    @bot_has_permissions(manage_channels=True)
    async def stop(self, ctx):
        """
        Commands to (un)restrict access to a channel.

        Required context: Server

        Required permissions:
            - Manage Channels

        Required bot permissions:
            - Manage Channels
        """
        if ctx.invoked_subcommand is None:
            await maybe_send(ctx, 'Invalid subcommand passed. Possible options are "on" and "off".')

    @stop.command()
    async def on(self, ctx):
        """
        Restrict messaging to a channel for everyone.
        Note that the user issueing this command should probably have some form of way to
        still write to the channel, or they will need to release the lock manually.
        """
        channel = ctx.channel

        await maybe_send(ctx, 'Sending messages to this channel has been restricted.')

        overwrite = channel.overwrites_for(ctx.guild.default_role)
        self.perm_cache.setdefault(channel.id, overwrite.send_messages)
        overwrite.send_messages = False
        await channel.set_permissions(ctx.guild.default_role, overwrite=overwrite)

    @stop.command()
    async def off(self, ctx):
        """
        Re-open a channel after it was locked down.
        This restores the default role's permission to send messages to this channel
        to its previous value.
        If the channel was not previously locked down, that permission is set to the global default.
        """
        channel = ctx.channel

        overwrite = channel.overwrites_for(ctx.guild.default_role)
        overwrite.send_messages = self.perm_cache.pop(channel.id, None)
        overwrite = overwrite if not overwrite.is_empty() else None  # Clear overwrite if empty
        await channel.set_permissions(ctx.guild.default_role, overwrite=overwrite)

        await maybe_send(ctx, 'Sending messages to this channel has been unrestricted.')<commit_msg>Stopping a channel now restricts reactions<commit_after>from collections import namedtuple
from logging import getLogger

from discord.ext.commands import bot_has_permissions, group, guild_only, has_permissions

from ..utils import maybe_send
from .basecog import BaseCog

logger = getLogger(__name__)

_perms = namedtuple('perms', ['send_messages', 'add_reactions'])


class Stop(BaseCog):
    """
    Utilities for locking down chat channels.
    """

    def __init__(self, bot):
        super().__init__(bot)
        self.perm_cache = {}

    @group()
    @guild_only()
    @has_permissions(manage_channels=True)
    @bot_has_permissions(manage_channels=True)
    async def stop(self, ctx):
        """
        Commands to (un)restrict access to a channel.

        Required context: Server

        Required permissions:
            - Manage Channels

        Required bot permissions:
            - Manage Channels
        """
        if ctx.invoked_subcommand is None:
            await maybe_send(ctx, 'Invalid subcommand passed. Possible options are "on" and "off".')

    @stop.command()
    async def on(self, ctx):
        """
        Restrict messaging and reactions to a channel for everyone.
        Note that the user issueing this command should probably have some form of way to
        still write to the channel, or they will need to release the lock manually.
        """
        channel = ctx.channel

        await maybe_send(ctx, 'Sending messages to this channel has been restricted.')

        overwrite = channel.overwrites_for(ctx.guild.default_role)
        self.perm_cache.setdefault(channel.id,
                                   _perms(overwrite.send_messages, overwrite.add_reactions))
        overwrite.send_messages = False
        overwrite.add_reactions = False
        await channel.set_permissions(ctx.guild.default_role, overwrite=overwrite)

    @stop.command()
    async def off(self, ctx):
        """
        Re-open a channel after it was locked down.
        This restores the default role's modified permissions,
        i.e. Send Messages and Add Reactions, to their previous values.
        If the channel was not previously locked down, nothing happens.
        """
        channel = ctx.channel

        overwrite = channel.overwrites_for(ctx.guild.default_role)

        perms = self.perm_cache.pop(channel.id, _perms(None, None))
        overwrite.send_messages = perms.send_messages
        overwrite.add_reactions = perms.add_reactions

        overwrite = overwrite if not overwrite.is_empty() else None  # Clear overwrite if empty
        await channel.set_permissions(ctx.guild.default_role, overwrite=overwrite)

        await maybe_send(ctx, 'Sending messages to this channel has been unrestricted.')
{'fraction_non_alphanumeric': 0.07675947409126063, 'fraction_numerical': 0.0, 'mean_word_length': 2.7539912917271407, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2455876', 'n_tokens_mistral': 1434, 'n_tokens_neox': 1384, 'n_words': 445}