Exemplo n.º 1
0
def dash_index():
    if g.user:
        if g.user.admin:
            stats = json.loads(rdb.get('web:dashboard:stats') or '{}')

            if not stats or 'refresh' in request.args:
                stats['messages'] = pretty_number(Message.select().count())
                stats['guilds'] = pretty_number(Guild.select().count())
                stats['users'] = pretty_number(User.select().count())
                stats['channels'] = pretty_number(Channel.select().count())

                rdb.setex('web:dashboard:stats', json.dumps(stats), 300)

            guilds = Guild.select().order_by(Guild.guild_id)
        else:
            stats = {}
            guilds = Guild.select(
                Guild, Guild.config['web'][str(g.user.user_id)].alias('role')
            ).where(
                (Guild.enabled == 1) &
                (~(Guild.config['web'][str(g.user.user_id)] >> None))
            )

        return render_template(
            'dashboard.html',
            stats=stats,
            guilds=guilds,
        )
    return render_template('login.html')
Exemplo n.º 2
0
    def violate(self, violation):
        key = 'lv:{e.member.guild_id}:{e.member.id}'.format(e=violation.event)
        last_violated = int(rdb.get(key) or 0)
        rdb.setex(
            'lv:{e.member.guild_id}:{e.member.id}'.format(e=violation.event),
            int(time.time()), 60)

        if not last_violated > time.time() - 10:
            self.call('ModLogPlugin.log_action_ext',
                      Actions.SPAM,
                      violation.event.guild.id,
                      v=violation)

            punishment = violation.check.punishment or violation.rule.punishment
            punishment_duration = violation.check.punishment_duration or violation.rule.punishment_duration

            if punishment == PunishmentType.MUTE:
                Infraction.mute(
                    self,
                    violation.event,
                    violation.member,
                    'Spam Detected',
                )
            elif punishment == PunishmentType.TEMPMUTE:
                Infraction.tempmute(
                    self, violation.event, violation.member, 'Spam Detected',
                    datetime.utcnow() + timedelta(seconds=punishment_duration))
            elif punishment == PunishmentType.KICK:
                Infraction.kick(self, violation.event, violation.member,
                                'Spam Detected')
            elif punishment == PunishmentType.TEMPBAN:
                Infraction.tempban(
                    self, violation.event, violation.member, 'Spam Detected',
                    datetime.utcnow() + timedelta(seconds=punishment_duration))
            elif punishment == PunishmentType.BAN:
                Infraction.ban(self, violation.event, violation.member,
                               'Spam Detected', violation.event.guild)

            # Clean messages if requested
            if punishment != PunishmentType.NONE and violation.rule.clean:
                msgs = Message.select(Message.id, Message.channel_id).where(
                    (Message.guild_id == violation.event.guild.id)
                    & (Message.author_id == violation.member.id)
                    & (Message.timestamp > (datetime.utcnow() - timedelta(
                        seconds=violation.rule.clean_duration)))).limit(
                            violation.rule.clean_count).tuples()

                channels = defaultdict(list)
                for mid, chan in msgs:
                    channels[chan].append(mid)

                for channel, messages in list(channels.items()):
                    channel = self.state.channels.get(channel)
                    if not channel:
                        continue

                    channel.delete_messages(messages)
Exemplo n.º 3
0
    def update_subreddit(self, sub, configs):
        # TODO: use before on this request
        r = requests.get('https://www.reddit.com/r/{}/new.json'.format(sub),
                         headers={
                             'User-Agent':
                             'discord:RowBoat:v0.0.1 (by /u/b1naryth1ef)'
                         })
        r.raise_for_status()

        data = list(
            reversed(map(lambda i: i['data'],
                         r.json()['data']['children'])))

        # TODO:
        #  1. instead of tracking per guild, just track globally per subreddit
        #  2. fan-out posts to each subscribed channel

        for gid, config in configs:
            guild = self.state.guilds.get(gid)
            if not guild:
                self.log.warning('Skipping non existant guild %s', gid)
                continue

            channel = self.get_channel(guild, config.channel)
            if not channel:
                self.log.warning(
                    'Skipping non existant channel %s for guild %s (%s)',
                    channel, guild.name, gid)
                continue
            last = float(
                rdb.get('rdt:lpid:{}:{}'.format(channel.id, sub)) or 0)

            item_count, high_time = 0, last
            for item in data:
                if item['created_utc'] > last:
                    try:
                        self.send_post(config, channel, item)
                    except:
                        self.log.exception(
                            'Failed to post reddit content from %s\n\n', item)
                    item_count += 1

                    if item['created_utc'] > high_time:
                        rdb.set('rdt:lpid:{}:{}'.format(channel.id, sub),
                                item['created_utc'])
                        high_time = item['created_utc']

                if item_count > 10:
                    break
Exemplo n.º 4
0
def stats():
    stats = json.loads(rdb.get('web:dashboard:stats') or '{}')

    if not stats or 'refresh' in request.args:
        # stats['messages'] = pretty_number(Message.select().count())
        # stats['guilds'] = pretty_number(Guild.select().count())
        # stats['users'] = pretty_number(User.select().count())
        # stats['channels'] = pretty_number(Channel.select().count())
        stats['messages'] = Message.select().count()
        stats['guilds'] = Guild.select().count()
        stats['users'] = User.select().count()
        stats['channels'] = Channel.select().count()

        rdb.setex('web:dashboard:stats', json.dumps(stats), 300)

    return jsonify(stats)
Exemplo n.º 5
0
def get_dominant_colors_guild(guild):
    import requests
    from rowboat.redis import rdb
    from PIL import Image
    from six import BytesIO

    key = 'guild:color:{}'.format(guild.icon)
    if rdb.exists(key):
        return int(rdb.get(key))
    else:
        r = requests.get(guild.icon_url)
        try:
            r.raise_for_status()
        except:
            return 0
        color = int(get_dominant_colors(Image.open(BytesIO(r.content)))[0], 16)
        rdb.set(key, color)
        return color
Exemplo n.º 6
0
def get_dominant_colors_user(user, url=None):
    import requests
    from rowboat.redis import rdb
    from PIL import Image
    from six import BytesIO

    key = 'avatar:color:{}'.format(user.avatar)
    if rdb.exists(key):
        return int(rdb.get(key))
    else:
        r = requests.get(url or user.avatar_url)
        try:
            r.raise_for_status()
        except:
            return 0
        color = int(get_dominant_colors(Image.open(BytesIO(r.content)))[0], 16)
        rdb.set(key, color)
        return color
Exemplo n.º 7
0
    def get_invite_info(self, code):
        if rdb.exists('inv:{}'.format(code)):
            return json.loads(rdb.get('inv:{}'.format(code)))

        try:
            obj = self.client.api.invites_get(code)
        except:
            return

        obj = {
            'id': obj.guild.id,
            'name': obj.guild.name,
            'icon': obj.guild.icon
        }

        # Cache for 12 hours
        rdb.setex('inv:{}'.format(code), json.dumps(obj), 43200)
        return obj
Exemplo n.º 8
0
def get_dominant_colors_user(user, url=None):
    import requests
    from rowboat.redis import rdb
    from PIL import Image
    from io import BytesIO

    key = 'avatar:color:{}'.format(user.avatar)
    if rdb.exists(key):
        return int(rdb.get(key))
    else:
        r = requests.get(url or user.get_avatar_url())
        try:
            r.raise_for_status()
        except:
            return 0

        rgbcolor = get_dominant_colors(Image.open(BytesIO(r.content)))[0]
        color = int('%02x%02x%02x' % (rgbcolor[0], rgbcolor[1], rgbcolor[2]),
                    16)  # Math is hard. https://stackoverflow.com/a/8340936
        rdb.set(key, color)

        return color
Exemplo n.º 9
0
    def get_invite_info(self, code):
        if rdb.exists('inv:{}'.format(code)):
            return json.loads(rdb.get('inv:{}'.format(code)))

        try:
            obj = self.client.api.invites_get(code)
        except:
            return

        if obj.channel and obj.channel.type == ChannelType.GROUP_DM:
            obj = {
                'id': obj.channel.id,
                'name': obj.channel.name
            }
        else:
            obj = {
                'id': obj.guild.id,
                'name': obj.guild.name,
                'icon': obj.guild.icon
            }

        # Cache for 12 hours
        rdb.setex('inv:{}'.format(code), 43200, json.dumps(obj))
        return obj
Exemplo n.º 10
0
    def server(self, event, guild_id=None):
        guild = self.state.guilds.get(guild_id) if guild_id else event.guild
        if not guild:
            raise CommandFail('invalid server')

        self.client.api.channels_typing(event.channel.id)

        embed = MessageEmbed()

        # Server Information
        content_server = []

        created_at = to_datetime(guild.id)
        content_server.append(u'**Created:** {} ago ({})'.format(
            humanize.naturaldelta(datetime.utcnow() - created_at),
            created_at.isoformat(),
        ))
        content_server.append(u'**Members:** {:,}'.format(guild.member_count))
        if len(guild.features) > 0:
            content_server.append(u'**Features:** {}'.format(', '.join(guild.features) or 'none'))
        content_server.append(u'**Voice region:** {}'.format(guild.region))

        if not rdb.exists('gmm:{}'.format(guild.id)):
            self.state.guilds[guild.id].inplace_update(self.client.api.guilds_get(guild.id), ignored=[
                'channels',
                'members',
                'voice_states',
                'presences',
            ])
            rdb.set('gmp:{}'.format(guild.id), self.state.guilds[guild.id].max_presences)
            rdb.set('gmm:{}'.format(guild.id), self.state.guilds[guild.id].max_members)

        content_server.append(u'**Max presences:** {:,}'.format(int(rdb.get('gmp:{}'.format(guild.id)))))
        content_server.append(u'**Max members:** {:,}'.format(int(rdb.get('gmm:{}'.format(guild.id)))))

        embed.add_field(name=u'\u276F Server Information', value='\n'.join(content_server), inline=False)

        # Counts
        content_counts = []
        count = {}
        for c in guild.channels.values():
            if not c.type:
                continue
            ctype = c.type.name.split('_')[1]
            count[ctype] = count.get(ctype, 0) + 1
        content_counts.append(u'<{}> {} channel categories'.format(CHANNEL_CATEGORY_EMOJI, count.get('category', 0)))
        content_counts.append(u'<{}> {} text channels'.format(TEXT_CHANNEL_EMOJI, count.get('text', 0)))
        content_counts.append(u'<{}> {} voice channels'.format(VOICE_CHANNEL_EMOJI, count.get('voice', 0)))
        embed.add_field(name=u'\u276F Counts', value='\n'.join(content_counts), inline=True)

        content_counts2 = []
        content_counts2.append(u'<{}> {} roles'.format(ROLE_EMOJI, len(guild.roles)))
        static_emojis = len(filter(lambda e: not guild.emojis.get(e).animated, guild.emojis))
        animated_emojis = len(filter(lambda e: guild.emojis.get(e).animated, guild.emojis))
        content_counts2.append(u'<{}> {}/{total} static emojis'.format(
            EMOJI_EMOJI,
            static_emojis,
            total=self.get_max_emoji_slots(guild))
        )
        content_counts2.append(u'<{}> {}/{total} animated emojis'.format(
            EMOJI_EMOJI,
            animated_emojis,
            total=self.get_max_emoji_slots(guild))
        )
        embed.add_field(name=u'\u200B', value='\n'.join(content_counts2), inline=True)

        # Members
        invite = guild.get_preview()

        status_online = None
        if invite:
            if not rdb.exists('apc:{}'.format(guild.id)):
                rdb.set('apc:{}'.format(guild.id), invite.approximate_presence_count, 900)
            status_online = int(rdb.get('apc:{}'.format(guild.id)))

            content_members = []
            content_members.append('<{}> {}'.format(STATUS_EMOJI[Status.ONLINE], status_online))
            content_members.append('<{}> {}'.format(STATUS_EMOJI[Status.OFFLINE], (guild.member_count - status_online)))

            # status_counts = defaultdict(int)
            # for member in guild.members.values():
            #     if not member.user.presence:
            #         status = Status.OFFLINE
            #     else:
            #         status = member.user.presence.status
            #     status_counts[status] += 1

            # for status, count in sorted(status_counts.items(), key=lambda i: Status[i[0]]):
            #     content_members.append(u'<{}> - {}'.format(
            #         STATUS_EMOJI[status], count
            #     ))

            embed.add_field(name=u'\u276F Members', value='\n'.join(content_members), inline=True)

        # Boosts
        content_boosts = []
        content_boosts.append(u'<{}> Level {}'.format(PREMIUM_GUILD_TIER_EMOJI[guild.premium_tier], int(guild.premium_tier)))
        # real_boost_count = len(filter(lambda y: guild.members.get(y).premium_since, guild.members))
        content_boosts.append(u'<{}> {} boosts'.format(
            PREMIUM_GUILD_ICON_EMOJI,
            guild.premium_subscription_count,
            # '({} members)'.format(real_boost_count) if real_boost_count < guild.premium_subscription_count else ''
        ))
        embed.add_field(name=u'\u276F Server Boost', value='\n'.join(content_boosts), inline=True)

        if guild.icon:
            embed.set_thumbnail(url=guild.icon_url)
            embed.color = get_dominant_colors_guild(guild, guild.get_icon_url('png'))
        event.msg.reply('', embed=embed)
Exemplo n.º 11
0
    def violate(self, violation):
        key = 'lv:{e.member.guild_id}:{e.member.id}'.format(e=violation.event)
        last_violated = int(rdb.get(key) or 0)
        rdb.setex(
            'lv:{e.member.guild_id}:{e.member.id}'.format(e=violation.event),
            int(time.time()), 60)

        if not last_violated > time.time() - 10:
            self.bot.plugins.get('ModLogPlugin').log_action_ext(
                Actions.SPAM_DEBUG, violation.event, v=violation)

            with self.bot.plugins.get(
                    'CorePlugin').send_control_message() as embed:
                embed.title = '{} Violated'.format(violation.label)
                embed.color = 0xfdfd96
                embed.description = violation.msg
                embed.add_field(name='Guild',
                                value=violation.event.guild.name,
                                inline=True)
                embed.add_field(name='Guild ID',
                                value=violation.event.guild.id,
                                inline=True)
                embed.add_field(name=ZERO_WIDTH_SPACE,
                                value=ZERO_WIDTH_SPACE,
                                inline=True)
                embed.add_field(name='User',
                                value=unicode(violation.member),
                                inline=True)
                embed.add_field(name='User ID',
                                value=violation.event.member.id,
                                inline=True)
                embed.add_field(name=ZERO_WIDTH_SPACE,
                                value=ZERO_WIDTH_SPACE,
                                inline=True)

            punishment = violation.check.punishment or violation.rule.punishment
            punishment_duration = violation.check.punishment_duration or violation.rule.punishment_duration

            if punishment == PunishmentType.MUTE:
                Infraction.mute(self, violation.event, violation.member,
                                'Spam Detected')
            elif punishment == PunishmentType.TEMPMUTE:
                Infraction.tempmute(
                    self, violation.event, violation.member, 'Spam Detected',
                    datetime.utcnow() + timedelta(seconds=punishment_duration))
            elif punishment == PunishmentType.KICK:
                Infraction.kick(self, violation.event, violation.member,
                                'Spam Detected')
            elif punishment == PunishmentType.TEMPBAN:
                Infraction.tempban(
                    self, violation.event, violation.member, 'Spam Detected',
                    datetime.utcnow() + timedelta(seconds=punishment_duration))
            elif punishment == PunishmentType.BAN:
                Infraction.ban(self, violation.event, violation.member,
                               'Spam Detected', violation.event.guild)

            # Clean messages if requested
            if punishment != PunishmentType.NONE and violation.rule.clean:
                msgs = Message.select(Message.id, Message.channel_id).where(
                    (Message.guild_id == violation.event.guild.id)
                    & (Message.author_id == violation.member.id)
                    & (Message.timestamp > (datetime.utcnow() - timedelta(
                        seconds=violation.rule.clean_duration)))).limit(
                            violation.rule.clean_count).tuples()

                channels = defaultdict(list)
                for mid, chan in msgs:
                    channels[chan].append(mid)

                for channel, messages in channels.items():
                    channel = self.state.channels.get(channel)
                    if not channel:
                        continue

                    channel.delete_messages(messages)
Exemplo n.º 12
0
    def violate(self, violation):
        key = 'lv:{e.member.guild_id}:{e.member.id}'.format(e=violation.event)
        last_violated = int(rdb.get(key) or 0)
        rdb.setex(
            'lv:{e.member.guild_id}:{e.member.id}'.format(e=violation.event),
            int(time.time()), 60)

        if not last_violated > time.time() - 10:
            self.call('ModLogPlugin.log_action_ext',
                      Actions.SPAM_DEBUG,
                      violation.event.guild.id,
                      v=violation)

            with self.bot.plugins.get(
                    'CorePlugin').send_spam_control_message() as embed:
                embed.title = '{} Violated'.format(violation.label)
                embed.color = 0xfdfd96
                embed.description = violation.msg
                embed.add_field(name='Guild',
                                value=violation.event.guild.name,
                                inline=True)
                embed.add_field(name='Guild ID',
                                value=violation.event.guild.id,
                                inline=True)
                embed.add_field(name=ZERO_WIDTH_SPACE,
                                value=ZERO_WIDTH_SPACE,
                                inline=True)
                embed.add_field(name='User',
                                value=unicode(violation.member),
                                inline=True)
                embed.add_field(name='User ID',
                                value=violation.event.member.id,
                                inline=True)
                embed.add_field(name=ZERO_WIDTH_SPACE,
                                value=ZERO_WIDTH_SPACE,
                                inline=True)

            punishment = violation.check.punishment or violation.rule.punishment
            punishment_duration = violation.check.punishment_duration or violation.rule.punishment_duration

            if punishment == PunishmentType.MUTE:
                if violation.rule.punishment_dms:
                    try:
                        infractions, embed = infraction_message(
                            violation.event,
                            violation.member.id,
                            'mute',
                            violation.event.guild.name,
                            str(self.state.me),
                            'Spam Detected',
                            auto=True)
                        dm = self.client.api.users_me_dms_create(
                            violation.member.id)
                        dm.send_message('You\'ve been {} in **{}**.'.format(
                            'muted', violation.event.guild.name),
                                        embed=embed)
                    except APIException:
                        pass

                Infraction.mute(self, violation.event, violation.member,
                                'Spam Detected')
            elif punishment == PunishmentType.TEMPMUTE:
                expiration_date = datetime.utcnow() + timedelta(
                    seconds=punishment_duration)
                if violation.rule.punishment_dms:
                    try:
                        infractions, embed = infraction_message(
                            violation.event,
                            violation.member.id,
                            'tempmute',
                            violation.event.guild.name,
                            str(self.state.me),
                            'Spam Detected',
                            expires=expiration_date,
                            auto=True)
                        dm = self.client.api.users_me_dms_create(
                            violation.member.id)
                        dm.send_message('You\'ve been {} in **{}**.'.format(
                            'temporarily muted', violation.event.guild.name),
                                        embed=embed)
                    except APIException:
                        pass

                Infraction.tempmute(self, violation.event, violation.member,
                                    'Spam Detected', expiration_date)
            elif punishment == PunishmentType.KICK:
                if violation.rule.punishment_dms:
                    try:
                        infractions, embed = infraction_message(
                            violation.event,
                            violation.member.id,
                            'kick',
                            violation.event.guild.name,
                            str(self.state.me),
                            'Spam Detected',
                            auto=True)
                        dm = self.client.api.users_me_dms_create(
                            violation.member.id)
                        dm.send_message('You\'ve been {} from **{}**.'.format(
                            'kicked', violation.event.guild.name),
                                        embed=embed)
                    except APIException:
                        pass

                Infraction.kick(self, violation.event, violation.member,
                                'Spam Detected')
            elif punishment == PunishmentType.TEMPBAN:
                expiration_date = datetime.utcnow() + timedelta(
                    seconds=punishment_duration)
                if violation.rule.punishment_dms:
                    try:
                        infractions, embed = infraction_message(
                            violation.event,
                            violation.member.id,
                            'tempban',
                            violation.event.guild.name,
                            str(self.state.me),
                            'Spam Detected',
                            expires=expiration_date,
                            auto=True)
                        dm = self.client.api.users_me_dms_create(
                            violation.member.id)
                        dm.send_message('You\'ve been {} from **{}**.'.format(
                            'temporarily banned', violation.event.guild.name),
                                        embed=embed)
                    except APIException:
                        pass

                Infraction.tempban(self, violation.event, violation.member,
                                   'Spam Detected', expiration_date)
            elif punishment == PunishmentType.BAN:
                if violation.rule.punishment_dms:
                    try:
                        infractions, embed = infraction_message(
                            violation.event,
                            violation.member.id,
                            'ban',
                            violation.event.guild.name,
                            str(self.state.me),
                            'Spam Detected',
                            auto=True)
                        dm = self.client.api.users_me_dms_create(
                            violation.member.id)
                        dm.send_message('You\'ve been {} from **{}**.'.format(
                            'banned', violation.event.guild.name),
                                        embed=embed)
                    except APIException:
                        pass

                Infraction.ban(self, violation.event, violation.member,
                               'Spam Detected', violation.event.guild)

            # Clean messages if requested
            if punishment != PunishmentType.NONE and violation.rule.clean:
                msgs = Message.select(Message.id, Message.channel_id).where(
                    (Message.guild_id == violation.event.guild.id)
                    & (Message.author_id == violation.member.id)
                    & (Message.timestamp > (datetime.utcnow() - timedelta(
                        seconds=violation.rule.clean_duration)))).limit(
                            violation.rule.clean_count).tuples()

                channels = defaultdict(list)
                for mid, chan in msgs:
                    channels[chan].append(mid)

                for channel, messages in channels.items():
                    channel = self.state.channels.get(channel)
                    if not channel:
                        continue

                    channel.delete_messages(messages)
Exemplo n.º 13
0
 def get_guild_state(self, guild_id, channel_id):
     data = rdb.get(TWITCH_GUILD_STATE_KEY.format(guild_id, channel_id))
     if not data:
         return {}
     return json.loads(data)
Exemplo n.º 14
0
 def get_stream_state(self, channel_id):
     data = rdb.get(TWITCH_STREAM_STATE_KEY.format(channel_id))
     if not data:
         return {}
     return json.loads(data)
Exemplo n.º 15
0
def guild_stats_self(guild):
    def serialize_user(gcc):
        for i in gcc:
            user_raw = '''
                SELECT username, discriminator
                FROM users
                WHERE
                    user_id=%s AND
                    bot=false;
            '''

            user = list(User.raw(user_raw, i[1]).tuples())

            if user:
                return {
                    'user': {
                        'username': user[0][0],
                        'discrim': str(user[0][1]),
                        'id': i[1]
                    },
                    'user_count': int(i[0]),
                }

        return {
            'user': '******',
            'user_count': 0,
        }

    def serialize_emoji(gcc):
        for i in gcc:
            emoji_raw = '''
                SELECT emoji_id
                FROM guild_emojis
                WHERE
                    emoji_id=%s AND
                    guild_id=%s;
            '''

            emoji = list(
                GuildEmoji.raw(emoji_raw, i[0], guild.guild_id).tuples())

            if emoji:
                return str(emoji[0][0])

        return '230870076126003200'

    data = json.loads(
        rdb.get('web:guild:{}:stats'.format(guild.guild_id)) or '{}')

    if not data:
        # Totals
        totals_messages = Message.select(Message.id).where(
            (Message.guild_id == guild.guild_id)).count()

        totals_infractions = Infraction.select(Infraction.id).where(
            (Infraction.guild_id == guild.guild_id)).count()

        # Peaks
        ## Messages
        peaks_messages_raw = '''
            SELECT count(id), author_id
            FROM
                messages
            WHERE
                guild_id=%s
            GROUP BY author_id
            ORDER BY count DESC
            LIMIT 5;
        '''

        peaks_messages = list(
            Message.raw(peaks_messages_raw, guild.guild_id).tuples())

        ## Infractions
        peaks_infractions_raw = '''
            SELECT count(id), user_id
            FROM
                infractions
            WHERE
                guild_id=%s
            GROUP BY user_id
            ORDER BY count DESC
            LIMIT 5;
        '''

        peaks_infractions = list(
            Infraction.raw(peaks_infractions_raw, guild.guild_id).tuples())

        ## Emoji
        peaks_emoji_raw = '''
            SELECT id, count(*)
            FROM (
                SELECT unnest(emojis) as id
                FROM messages
                WHERE guild_id=%s and
                cardinality(emojis) > 0
            ) q
            GROUP BY 1
            ORDER BY 2 DESC
            LIMIT 5
        '''

        peaks_emoji = list(
            Message.raw(peaks_emoji_raw, guild.guild_id).tuples())

        ## Command
        peaks_command_raw = '''
            SELECT count(c.command), c.command
            FROM
                commands c
            INNER JOIN messages m
            ON (c.message_id = m.id)
            WHERE
                m.guild_id=%s
            GROUP BY 2
            ORDER BY 1 DESC
            LIMIT 1;
        '''

        peaks_command = list(
            Command.raw(peaks_command_raw, guild.guild_id).tuples())

        if totals_messages:
            totals_messages = totals_messages
        else:
            totals_messages = 0

        if totals_infractions:
            totals_infractions = totals_infractions
        else:
            totals_infractions = 0

        if peaks_messages:
            pm = serialize_user(peaks_messages)
        else:
            pm = {
                'user': '******',
                'user_count': 0,
            }

        if peaks_infractions:
            pi = serialize_user(peaks_infractions)
        else:
            pi = {
                'user': '******',
                'user_count': 0,
            }

        if peaks_emoji:
            anim = False

            peaks_emoji_id = serialize_emoji(peaks_emoji)
            url = 'https://discordapp.com/api/emojis/{}.gif'.format(
                peaks_emoji_id)
            r = requests.get(url)
            try:
                r.raise_for_status()
                anim = True
            except requests.HTTPError:
                pass

            if anim:
                peaks_emoji_ext = 'gif'
            else:
                peaks_emoji_ext = 'png'
        else:
            peaks_emoji_id = '230870076126003200'
            peaks_emoji_ext = 'png'

        if peaks_command:
            peaks_command = '{1}'.format(*peaks_command[0])
        else:
            peaks_command = 'N/A'

        data = {
            'totals': {
                'messages': totals_messages,
                'infractions': totals_infractions,
            },
            'peaks': {
                'messages': pm,
                'infractions': pi,
                'emoji': {
                    'id': peaks_emoji_id,
                    'ext': peaks_emoji_ext,
                },
                'command': peaks_command,
            },
        }
        rdb.setex('web:guild:{}:stats'.format(guild.guild_id),
                  json.dumps(data), 600)

    return jsonify(data)