Пример #1
0
    def split_paragraphs(self, text):
        parts = text.split("\n\n")
        done = []

        for i, part in enumerate(parts):
            if i < len(parts) - 1:
                done.append(part + "\n\u200b")
            else:
                done.append(part)

        return line_splitter(done, 2000, split_only=True)
Пример #2
0
 async def render(self) -> List[str]:
     return line_splitter([self.template.format(line) for line in self.items], 2000)
Пример #3
0
    async def do_relay(self, message):
        targets = self.data_manager.get_all_targets(message.channel)
        prefixed = self.data_manager.get_prefixes(message.channel)

        content = message.content
        lower_content = content.lower()

        for prefix, target in prefixed.items():
            if lower_content.startswith(prefix):
                targets.add(target)
                content = content[len(prefix):]
                break

        del lower_content

        avatar = message.author.avatar_url

        for channel_id in targets:
            if channel_id == message.channel.id:
                continue

            hook = self.webhooks.get(channel_id, None)

            if hook is None:
                h = await self.ensure_relay_hook(channel_id)

                if h:
                    self.webhooks[channel_id] = h

                hook = self.webhooks.get(channel_id, None)

            if hook is None:
                await self.send_message(
                    message.channel,
                    "Webhook for channel `{}` is missing - unlinking channel entirely"
                    .format(channel_id))
                self.data_manager.unlink_all(channel_id)
                self.data_manager.save()
            else:
                try:
                    if content:
                        await self.execute_webhook(
                            hook["id"],
                            hook["token"],
                            wait=True,
                            content=content,
                            username=message.author.display_name,
                            avatar_url=avatar if avatar else None,
                            embeds=message.embeds)
                    elif message.embeds:
                        await self.execute_webhook(
                            hook["id"],
                            hook["token"],
                            wait=True,
                            username=message.author.display_name,
                            avatar_url=avatar if avatar else None,
                            embeds=message.embeds)

                    if message.attachments:
                        lines = ["__**Attachments**__\n"]

                        for attachment in message.attachments:
                            lines.append("**{}**: {}".format(
                                attachment["filename"], attachment["url"]))

                        for split_line in line_splitter(lines, 2000):
                            await self.execute_webhook(
                                hook["id"],
                                hook["token"],
                                wait=True,
                                content=split_line,
                                username=message.author.display_name,
                                avatar_url=avatar if avatar else None)
                except Exception as e:
                    await self.send_message(
                        message.channel,
                        "Error executing webhook for channel `{}` - unlinking channel\n\n```{}```"
                        .format(channel_id, e))
                    self.data_manager.remove_targets(channel_id)
                    self.data_manager.save()
                    raise
Пример #4
0
    async def command_links(self, data, data_string, message):
        links = self.data_manager.get_targets(message.channel)
        relays = self.data_manager.get_relays(message.channel)
        groups = self.data_manager.find_groups(message.channel)
        prefixes = self.data_manager.get_prefixes(message.channel)

        lines = []

        if not links and not relays and not groups:
            return await self.send_message(
                message.channel,
                "This channel is not linked to any others in any way.")

        if links:
            lines.append("**Two-way linked channels**")

            for target in links:
                channel = self.get_channel(target)

                if not channel:
                    lines.append("• {}".format(target))
                else:
                    lines.append("• {}".format(self.get_channel_info(channel)))
        else:
            lines.append("**No two-way linked channels**")
        lines.append("")

        if relays:
            lines.append("**One-way relay channels**")

            for target in relays:
                channel = self.get_channel(target)

                if not channel:
                    lines.append("• {}".format(target))
                else:
                    lines.append("• {}".format(self.get_channel_info(channel)))
        else:
            lines.append("**No one-way relay channels**")
        lines.append("")

        if groups:
            lines.append("**Channel groups**")

            for group in groups:
                channels = self.data_manager.get_channels_for_group(group)

                if channels:
                    lines.append("_Group: `{}`_".format(group))

                    for target in channels:
                        if target == message.channel.id:
                            continue

                        channel = self.get_channel(target)

                        if not channel:
                            lines.append("• {}".format(target))
                        else:
                            lines.append("• {}".format(
                                self.get_channel_info(channel)))
                else:
                    lines.append("_Group: `{}`_ - No other channels in group")
        else:
            lines.append("**No grouped channels**")

        if prefixes:
            lines.append("**Prefix links**")

            for prefix, target in prefixes.items():
                channel = self.get_channel(target)

                if not channel:
                    lines.append("• `{}` -> {}".format(prefix, target))
                else:
                    lines.append("• `{}` -> {}".format(
                        prefix, self.get_channel_info(channel)))
        else:
            lines.append("**No channels linked by prefix**")

        for line in line_splitter(lines, 2000):
            await self.send_message(message.channel, line)
Пример #5
0
 async def render(self) -> List[str]:
     return line_splitter([self.template.format(i + 1, line) for i, line in enumerate(self.items)], 2000)