Пример #1
0
    def on_tags_raw(self, event, name):
        tag = self.fetch_tag(name, event.guild.id)

        if not tag:
            raise CommandFail('no tag exists by that name')

        content = tag.content

        source = self.source_re.search(content)
        if source:
            source = source.group(1).lower()
            url = self.remote_url.format(source)
            r = requests.get(url)
            r = self.import_parser.search(r.content)
            r = r.group(1).decode('utf8').strip('`\n') if r else ''
            data = {'here': '@here', 'everyone': '@everyone'}
            r = self.replace_variables(r, data)
            if r and content == r:
                return event.msg.reply((
                    'This tag was imported from `{source}`: <https://github.com/ThaTiemsz'
                    '/RawgoatTags/blob/master/tags/{source}.md>').format(
                        source=source))
            content = self.source_re.sub('', content)
            Tag.update(content=content).where((Tag.name == tag.name) & (
                Tag.guild_id == tag.guild_id)).execute()

        if len(S(content, False, True)) > 1990:
            return event.msg.reply(
                'This tag is too long. See attached file for the source.',
                attachments=[('tag_raw_{}.txt'.format(event.msg.id), content)])

        event.msg.reply(u'```\n{}\n```'.format(S(content, False, True)))
Пример #2
0
    def on_tags(self, event, name):
        try:
            tag = Tag.select(Tag, User).join(
                User, on=(User.user_id == Tag.author_id
                          )).where((Tag.guild_id == event.guild.id)
                                   & (Tag.name == S(name))).get()
        except Tag.DoesNotExist:
            raise CommandFail('no tag by that name exists')

        # Track the usage of the tag
        Tag.update(times_used=Tag.times_used +
                   1).where((Tag.guild_id == tag.guild_id)
                            & (Tag.name == tag.name)).execute()

        event.msg.reply(':information_source: {}'.format(tag.content))
Пример #3
0
 def fetch_tag(self, name, guild_id):
     try:
         tag = Tag.select(Tag).where((Tag.name == S(name))
                                     & (Tag.guild_id == guild_id)).get()
     except Tag.DoesNotExist:
         return
     return tag
Пример #4
0
    def on_tags(self, event, name, text='', debug=False):
        tag = self.fetch_tag(name, event.guild.id)

        if not tag:
            raise CommandFail('no tag exists by that name')

        # Track the usage of the tag
        Tag.update(times_used=Tag.times_used +
                   1).where((Tag.name == tag.name)
                            & (Tag.guild_id == tag.guild_id)).execute()

        content = self.compile_tag(event, tag.content, text, debug)

        if not debug:
            event.msg.reply(
                clamp(u':information_source: {}'.format(content), 2000))
Пример #5
0
    def on_tags_info(self, event, name):
        try:
            tag = Tag.select(Tag, User).join(
                User, on=(User.user_id == Tag.author_id).alias(
                    'author')).where((Tag.name == S(name))
                                     & (Tag.guild_id == event.guild.id)).get()
        except Tag.DoesNotExist:
            raise CommandFail('no tag exists by that name')

        content = tag.content
        source = self.source_re.search(content)
        if source:
            source = source.group(1).lower()
            url = self.remote_url.format(source)
            r = requests.get(url)
            r = self.import_parser.search(r.content)
            r = r.group(1).decode('utf8').strip('`\n') if r else ''
            data = {'here': '@here', 'everyone': '@everyone'}
            r = self.replace_variables(r, data)
            if not (r and content == r):
                source = None
                content = self.source_re.sub('', content)
                Tag.update(content=content).where((Tag.name == tag.name) & (
                    Tag.guild_id == tag.guild_id)).execute()

        embed = MessageEmbed()
        embed.title = tag.name
        embed.description = clamp(content, 2048)
        embed.add_field(name='Author', value=unicode(tag.author), inline=True)
        embed.add_field(name='Times Used',
                        value=str(tag.times_used),
                        inline=True)
        embed.add_field(
            name='Imported',
            inline=True,
            value='No' if not source else
            ('Yes: [{source}]'
             '(https://github.com/ThaTiemsz/RawgoatTags/blob/master/tags/{source}.md)'
             ).format(source=source))
        embed.timestamp = tag.created_at.isoformat()
        event.msg.reply(embed=embed)
Пример #6
0
    def on_tags_list(self, event):
        tags = Tag.select(Tag, User).join(
            User, on=(User.user_id == Tag.author_id)).where(
                (Tag.guild_id == event.guild.id)).order_by(Tag.name)

        if not tags:
            raise CommandFail('No tags exist')

        embed = MessageEmbed()
        embed.title = 'Tags for {}'.format(event.guild.name)
        embed.description = '\n'.join(
            '- `{}` by {}'.format(tag.name, tag.user.name) for tag in tags)

        event.msg.reply(embed=embed)
Пример #7
0
    def on_tags_remove(self, event, name):
        try:
            tag = Tag.select(Tag, User).join(
                User, on=(User.user_id == Tag.author_id
                          )).where((Tag.guild_id == event.guild.id)
                                   & (Tag.name == S(name))).get()
        except Tag.DoesNotExist:
            raise CommandFail('no tag by that name exists')

        if tag.author_id != event.author.id:
            if event.user_level <= event.config.min_level_remove_others:
                raise CommandFail(
                    'you do not have the required permissions to remove other users tags'
                )

        tag.delete_instance()
        raise CommandSuccess(u'ok, deleted tag `{}`'.format(tag.name))
Пример #8
0
    def on_tags_create(self, event, name, content):
        if re.search(r'<a?:\S+:[0-9]+>', name):
            raise CommandFail('tag names cannot contain emotes')

        name = S(name)

        import_tag = re.match(r'^import:([a-z]+)$', content, re.I)
        if import_tag:
            remote = import_tag.group(1).lower()
            r = requests.get(self.remote_url.format(remote))
            try:
                r.raise_for_status()
            except requests.HTTPError:
                raise CommandFail('{}not found'.format(remote))

            content = self.import_parser.search(r.content)
            if not content:
                raise CommandFail(
                    'remote tag `{}` is malformed - please tell Rawgoat staff'.
                    format(remote))

            content = content.group(1).decode('utf8').strip('`\n')
            if not self.source_re.search(content):
                content = u'{{source:{}}}{}'.format(remote, content)
        else:
            content = self.source_re.sub('', content)

        data = {'here': '@here', 'everyone': '@everyone'}

        content = self.replace_variables(content, data)

        if len(content) > event.config.max_tag_length:
            raise CommandFail(
                'tag content is too long (max {} characters)'.format(
                    event.config.max_tag_length))

        if self.fetch_tag(name, event.guild.id):
            raise CommandFail('a tag by that name already exists')

        _, created = Tag.get_or_create(guild_id=event.guild.id,
                                       author_id=event.author.id,
                                       name=name,
                                       content=content)

        raise CommandSuccess(u'ok, your tag named `{}` has been {}'.format(
            name, 'imported' if import_tag else 'created'))
Пример #9
0
    def on_tags_info(self, event, name):
        try:
            tag = Tag.select(Tag, User).join(
                User, on=(User.user_id == Tag.author_id).alias('author')
            ).where(
                (Tag.guild_id == event.guild.id) &
                (Tag.name == S(name))
            ).get()
        except Tag.DoesNotExist:
            raise CommandFail('no tag by that name exists')

        embed = MessageEmbed()
        embed.title = tag.name
        embed.description = tag.content
        embed.add_field(name='Author', value=unicode(tag.author), inline=True)
        embed.add_field(name='Times Used', value=str(tag.times_used), inline=True)
        embed.timestamp = tag.created_at.isoformat()
        event.msg.reply(embed=embed)
Пример #10
0
    def on_tags_create(self, event, name, content):
        name = S(name)
        content = S(content)

        if len(content) > event.config.max_tag_length:
            raise CommandFail('tag content is too long (max {} characters)'.format(event.config.max_tag_length))

        _, created = Tag.get_or_create(
            guild_id=event.guild.id,
            author_id=event.author.id,
            name=name,
            content=content
        )

        if not created:
            raise CommandFail('a tag by that name already exists')

        raise CommandSuccess('ok, your tag named `{}` has been created'.format(name))
Пример #11
0
    def on_tags_list(self, event, pattern=None):
        buff = u'Available tags: '
        query = Tag.select(Tag.name).where(Tag.guild_id == event.guild.id)
        tags = sorted([i[0] for i in query.tuples()])
        if not tags:
            return event.msg.reply('No tags found for this server.')
        found = 0
        for tag in tags:
            if pattern and tag.lower().find(pattern.lower()) == -1: continue
            tag = u'`{}`, '.format(S(tag, escape_codeblocks=True))
            if len(tag) + len(buff) > 1980:
                event.msg.reply(buff[:-2])
                buff = u''
            buff += tag
            found += 1

        if not found:
            return event.msg.reply('No tags found for this server.')
        return event.msg.reply(u'{} (total: {})'.format(buff[:-2], found))