wiki-postbot/wiki_postbot/templates/wiki.py

93 lines
No EOL
2.5 KiB
Python

"""
Templates for representing different kinds of messages on mediawiki
"""
from typing import TYPE_CHECKING
from wiki_postbot.formats.wiki import WikiPage
from abc import abstractmethod
import warnings
if TYPE_CHECKING:
from wiki_postbot.clients.slack import SlackMessage
try:
from discord.message import Message
except ImportError:
Message = None
class WikiTemplate(WikiPage):
@abstractmethod
def format_discord(self, msg:'Message') -> str:
"""
Format a discord message into a template string
"""
class TemplateMessage(WikiTemplate):
@classmethod
def format_discord(self, msg:'Message') -> str:
# try/catch the avatar URL
try:
avatar = msg.author.avatar.url
except Exception as e:
warnings.warn("No avatar found!")
avatar = ""
return (
"{{Message\n"
f"|Author={msg.author.name}\n"
f"|Avatar={avatar}\n"
f"|Date Sent={msg.created_at.strftime('%y-%m-%d %H:%M:%S')}\n"
f"|Channel={msg.channel}\n"
f"|Text={msg.content}\n"
f"|Link={msg.jump_url}\n"
"}}"
)
@classmethod
def format_slack(cls, msg:'SlackMessage') -> str:
return (
"{{Message\n"
f"|Author={msg.author}\n"
f"|Avatar={msg.avatar}\n"
f"|Date Sent={msg.date_sent.strftime('%y-%m-%d %H:%M:%S')}\n"
f"|Channel={msg.channel}\n"
f"|Text={msg.content}\n"
f"|Link={msg.permalink}\n"
"}}"
)
#
# template_message = TemplateMessage.from_source(
# title="Template:Message",
# source="""<noinclude>
# <pre>
# {{Message
# |Author=
# |Avatar=
# |Date Sent=
# |Channel=(Optional)
# |Text=
# |Link=
# }}
# </pre>
# </noinclude>
# <includeonly>
#
# {{#subobject:{{{Author}}}-{{{Date Sent}}}
# |Message topic={{PAGENAME}}
# |Has author={{{Author}}}
# |Date sent={{{Date Sent}}}
# |Has URL={{{Link}}}
# |Contains text={{{Text}}}
# }}
# <div style="border: 1px solid black; border-radius: 5px;padding:5px"
# >
# <div style="display:flex; flex-direction:row; align-items:center; border-bottom:1px solid black; gap:10px; padding-bottom:5px;"><img src={{{Avatar|}}} style="width:30px;border-radius:10px"/><span style="font-weight:bold;">{{{Author}}}</span><span><nowiki>#</nowiki>{{{Channel|}}}</span><span style="font-style:italic;color:#999999">[{{{Link}}} {{{Date Sent}}}]</span></div>
# <div>
# {{{Text}}}
# </div>
# </div>
# </includeonly>
# """
# )