async def send_message(self, message):
        try:
            if int(message.conversation.id) > 0:
                chat = self.client.get_user(message.conversation.id)
            else:
                chat = self.client.get_channel(positive(message.conversation.id))
            await chat.trigger_typing()
            if message.type == 'text':
                content = self.add_discord_mentions(chat, message.content)
                if message.extra:
                    if 'format' in message.extra and message.extra['format'] == 'HTML':
                        content = html_to_discord_markdown(content)
                    if 'preview' in message.extra and not message.extra['preview']:
                        content = re.sub(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', r'<\g<0>>', content, flags=re.MULTILINE)

                if len(content) > 2000:
                    texts = split_large_message(content, 2000)
                    for text in texts:
                        await chat.send(text)

                else:
                    await chat.send(content)

            elif message.type == 'photo' or message.type == 'document' or message.type == 'video' or message.type == 'voice':
                send_content = True
                embed = Embed()

                if message.extra and 'caption' in message.extra and message.extra['caption']:
                    lines = message.extra['caption'].split('\n')
                    embed.title = lines[0]
                    embed.description = '\n'.join(lines[1:])
                    send_content = False

                if send_content:
                    if message.content.startswith('/'):
                        await chat.send(file=discord.File(message.content, filename=message.type + get_extension(message.content)))
                    else:
                        await chat.send(message.content)

                else:
                    if message.content.startswith('/'):
                        await chat.send(file=discord.File(message.content, filename=message.type + get_extension(message.content)), embed=embed)
                    elif message.content.startswith('http'):
                        if message.type == 'photo':
                            embed.set_image(url=message.content)
                        elif message.type == 'video':
                            embed.set_video(url=message.content)
                        else:
                            embed.url = message.content
                        await chat.send(embed=embed)

        except KeyboardInterrupt:
            pass

        except Exception as e:
            logging.error(e)
            if self.bot.started:
                catch_exception(e, self.bot)
Exemple #2
0
 def _make_embed_skeleton(self, header: bool, desc: bool, footer: bool, image: bool):
     embed = self.template
     e_new = Embed(colour=embed.colour, type=embed.type)
     if header:
         e_new.title = embed.title
         e_new.url = embed.url
         author = embed.author
         if author.name is not EmptyEmbed:
             e_new.set_author(name=author.name, url=author.url, icon_url=author.icon_url)
     if desc:
         e_new.description = embed.description
     if image:
         if embed.image.url is not EmptyEmbed:
             e_new.set_image(url=embed.image.url)
         if embed.thumbnail.url is not EmptyEmbed:
             e_new.set_thumbnail(url=embed.thumbnail.url)
     if footer:
         e_new.timestamp = embed.timestamp
         footer = embed.footer
         e_new.set_footer(text=footer.text, icon_url=footer.icon_url)
     return e_new