<commit_before>import six
import json
import requests

from peewee import fn
from disco.bot import CommandLevels
from disco.types.channel import Channel
from disco.types.message import MessageTable, MessageEmbed, MessageEmbedField, MessageEmbedThumbnail

from rowboat import RowboatPlugin as Plugin
from rowboat.util import C
from rowboat.util.images import get_dominant_colors_user
from rowboat.redis import rdb
from rowboat.types.plugin import PluginConfig
from rowboat.models.user import User, Infraction
from rowboat.models.message import Message


EMOJI_STATS_SQL = """
WITH emojis AS (
    SELECT jsonb_array_elements_text(emojis) as id
    FROM messages WHERE guild_id={gid} AND jsonb_array_length(emojis) > 0
)
SELECT gm.emoji_id, count(*), gm.name
FROM emojis
JOIN guildemojis gm ON gm.emoji_id=emojis.id::bigint
WHERE gm.guild_id={gid}
GROUP BY gm.emoji_id
{}
LIMIT 10;
"""


class AdminConfig(PluginConfig):
    pass


# TODO: unban tempbans

class AdminPlugin(Plugin):
    @Plugin.command('roles', level=CommandLevels.MOD)
    def roles(self, event):
        roles = []
        for role in event.guild.roles.values():
            roles.append('{} - {}'.format(role.id, role.name))
        return event.msg.reply('```{}```'.format('\n'.join(roles)))

    @Plugin.command('kick', '<user:user> [reason:str...]', level=CommandLevels.MOD)
    def kick(self, event, user, reason=None):
        """
        Kick a user from the server (with an optional reason for the modlog).
        """

        member = event.guild.get_member(user)
        if member:
            Infraction.kick(self, event, member, reason)
        else:
            event.msg.reply(':warning: Invalid user!')

    @Plugin.command('ban', '<user:user> [reason:str...]', level=CommandLevels.MOD)
    def ban(self, event, user, reason=None):
        """
        Ban a user from the server (with an optional reason for the modlog).
        """

        member = event.guild.get_member(user)
        if member:
            Infraction.ban(self, event, member, reason)
        else:
            event.msg.reply(':warning: Invalid user!')

    @Plugin.command('softban', '<user:user> [reason:str...]', level=CommandLevels.MOD)
    def softban(self, event, user, reason=None):
        """
        Ban then unban a user from the server (with an optional reason for the modlog).
        """

        member = event.guild.get_member(user)
        if member:
            Infraction.softban(self, event, member, reason)
        else:
            event.msg.reply(':warning: Invalid user!')

    @Plugin.command('tempban', '<user:user> <duration:duration> [reason:str...]', level=CommandLevels.MOD)
    def tempban(self, event, duration, user, reason=None):
        """
        Ban a user from the server for a given duration (with an optional reason for the modlog).
        """

        member = event.guild.get_member(user)
        if member:
            Infraction.tempban(self, event, member, reason, duration)
        else:
            event.msg.reply(':warning: Invalid user!')

    @Plugin.command('archive', '[size:int] [fmt:str]', level=CommandLevels.MOD)<commit_msg>Add option to confirm admin actions inline<commit_after>import six
import json
import requests

from peewee import fn
from disco.bot import CommandLevels
from disco.types.channel import Channel
from disco.types.message import MessageTable, MessageEmbed, MessageEmbedField, MessageEmbedThumbnail

from rowboat import RowboatPlugin as Plugin
from rowboat.util import C
from rowboat.util.images import get_dominant_colors_user
from rowboat.redis import rdb
from rowboat.types import Field
from rowboat.types.plugin import PluginConfig
from rowboat.models.user import User, Infraction
from rowboat.models.message import Message


EMOJI_STATS_SQL = """
WITH emojis AS (
    SELECT jsonb_array_elements_text(emojis) as id
    FROM messages WHERE guild_id={gid} AND jsonb_array_length(emojis) > 0
)
SELECT gm.emoji_id, count(*), gm.name
FROM emojis
JOIN guildemojis gm ON gm.emoji_id=emojis.id::bigint
WHERE gm.guild_id={gid}
GROUP BY gm.emoji_id
{}
LIMIT 10;
"""


class AdminConfig(PluginConfig):
    confirm_actions = Field(bool, default=True)


# TODO: unban tempbans

class AdminPlugin(Plugin):
    @Plugin.command('roles', level=CommandLevels.MOD)
    def roles(self, event):
        roles = []
        for role in event.guild.roles.values():
            roles.append('{} - {}'.format(role.id, role.name))
        return event.msg.reply('```{}```'.format('\n'.join(roles)))

    @Plugin.command('kick', '<user:user> [reason:str...]', level=CommandLevels.MOD)
    def kick(self, event, user, reason=None):
        """
        Kick a user from the server (with an optional reason for the modlog).
        """
        member = event.guild.get_member(user)
        if member:
            Infraction.kick(self, event, member, reason)
            if event.config.confirm_actions:
                event.msg.reply(':ok_hand: kicked {} for `{}`'.format(user, reason or 'no reason given'))
        else:
            event.msg.reply(':warning: Invalid user!')

    @Plugin.command('ban', '<user:user> [reason:str...]', level=CommandLevels.MOD)
    def ban(self, event, user, reason=None):
        """
        Ban a user from the server (with an optional reason for the modlog).
        """

        member = event.guild.get_member(user)
        if member:
            Infraction.ban(self, event, member, reason)
            if event.config.confirm_actions:
                event.msg.reply(':ok_hand: banned {} for `{}`'.format(user, reason or 'no reason given'))
        else:
            event.msg.reply(':warning: Invalid user!')

    @Plugin.command('softban', '<user:user> [reason:str...]', level=CommandLevels.MOD)
    def softban(self, event, user, reason=None):
        """
        Ban then unban a user from the server (with an optional reason for the modlog).
        """

        member = event.guild.get_member(user)
        if member:
            Infraction.softban(self, event, member, reason)
            if event.config.confirm_actions:
                event.msg.reply(':ok_hand: soft-banned {} for `{}`'.format(user, reason or 'no reason given'))
        else:
            event.msg.reply(':warning: Invalid user!')

    @Plugin.command('tempban', '<user:user> <duration:duration> [reason:str...]', level=CommandLevels.MOD)
    def tempban(self, event, duration, user, reason=None):
        """
        Ban a user from the server for a given duration (with an optional reason for the modlog).
        """

        member = event.guild.get_member(user)
        if member:
            Infraction.tempban(self, event, member, reason, duration)
            if event.config.confirm_actions:
                event.msg.reply(':ok_hand: temp-banned {} for `{}`'.format(user, reason or 'no reason given'))
        else:
            event.msg.reply(':warning: Invalid user!')

    @Plugin.command('archive', '[size:int] [fmt:str]', level=CommandLevels.MOD)
{'fraction_non_alphanumeric': 0.1250894518391298, 'fraction_numerical': 0.0008587376556462001, 'mean_word_length': 3.0580720092915215, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6477949', 'n_tokens_mistral': 2171, 'n_tokens_neox': 2098, 'n_words': 577}