2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【d.py】ココフォリア形式のログをどどんとふ形式のログに変換する

Posted at

概要

discord.py用のcogコードです。
main.pyからload_extensionする事を想定しています。
main.pyの作例はこちら

バージョン情報

Python 3.8.12
discord.py 2.1.0

実装される機能

TRPGセッションツールココフォリアで出力されるログを、hybridコマンドを通して古き良きどどんとふ形式のログに変換します。
どどんとふ全タブ用コンバータを通した後に文字化けしている状態を修復するhybridコマンドも同梱しています。

html_conv.py
import discord
from discord.ext import commands
import re #正規表現を扱うモジュール
import typing #typing.Optional解禁

class HTML_conv(commands.Cog, name = 'ログ変換'):
  """ココフォリア形式のログをどどんとふ形式に変換します"""
  
  def __init__(self, bot: commands.Bot):
    super().__init__()
    self.bot: commands.Bot = bot

  async def cog_check(self, ctx: commands.Context):
    if ctx.author.bot:
      return False
    else:
      return True

  @commands.hybrid_command(name = "ココフォログ変換", aliases = ["ココフォ"])
  @discord.app_commands.rename(log = "ログ")
  async def log_conv(self, ctx: commands.Context, log: typing.Optional[discord.Attachment]):
    """
    ココフォリア形式のログをどどんとふ形式のログに変換します
    
    Parameters
    -----------
    log
      htmlログを添付してください
    """
    if log is None:
      await ctx.send("ログが無いです", ephemeral = True)
      return
    if not log.content_type in 'text/html; charset=utf-8':
      await ctx.send(f"これ{log.content_type}です\n文字コードUTF-8のHTMLログください", ephemeral = True)
      return
    data = await log.read()
    data = data.decode("utf-8")
    data = re.sub(r'\s+<p style="color:(?P<color>.*);">\s+<span> (?P<tab>\[.*\])</span>\s+<span>(?P<text>.*)</span> :\s+<span>\s*', r"\n\g<tab><font color='\g<color>'><b>\g<text></b>:", data)
    data = re.sub(r'\s*</span>\s+</p>', r"</font><br>", data)
    data = re.sub(r'(#\d+)\n', r"<br>\1", data)
    data = re.sub(r'\s+<meta name="viewport" content="width=device-width, initial-scale=1.0" />', r"", data)
    with open(log.filename, "w") as f:
      f.write(data)
    comp = discord.File(log.filename)
    await ctx.send("お次はこちら\nhttp://dummy.flop.jp/trpg/conv/", file = comp, ephemeral = True)

  @commands.hybrid_command(name = "ログ修復", aliases = ["整形ログ修復", "修復"])
  @discord.app_commands.rename(log = "ログ")
  async def log_repair(self, ctx: commands.Context, log: typing.Optional[discord.Attachment]):
    """
    webで加工後のログの文字化けを修復します
    
    Parameters
    -----------
    log
      htmlログを添付してください
    """
    if log is None:
      await ctx.send("ログが無いです", ephemeral = True)
      return
    if not log.content_type in 'text/html; charset=utf-8':
      await ctx.send(f"これ{log.content_type}です\n文字コードUTF-8のHTMLログください", ephemeral = True)
      return
    data = await log.read()
    data = data.decode("utf-8")
    data = '<!DOCTYPE html>\n<html lang="ja">\n  <head>\n    <meta charset="UTF-8" />' + data
    fname = log.filename.replace(".html", "_rep.html")
    with open(fname, "w") as f:
      f.write(data)
    comp = discord.File(fname)
    await ctx.send("完成しました!", file = comp)

async def setup(bot: commands.Bot):
  await bot.add_cog(HTML_conv(bot))
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?