integrations.discord.parser

integrations/discord/parser.py

 1"""
 2integrations/discord/parser.py
 3"""
 4
 5from typing import TYPE_CHECKING, cast
 6
 7from discord import Message, Thread
 8from discord.channel import TextChannel
 9
10import libs.global_value as g
11from integrations.base.interface import MessageParserDataMixin, MessageParserInterface
12from integrations.protocols import MsgData, PostData, StatusData
13from libs.types import CommandType
14
15if TYPE_CHECKING:
16    from integrations.discord.adapter import ServiceAdapter
17
18
19class MessageParser(MessageParserDataMixin, MessageParserInterface):
20    """メッセージ解析クラス"""
21
22    def __init__(self) -> None:
23        MessageParserDataMixin.__init__(self)
24        self.data: MsgData = MsgData()
25        self.post: PostData = PostData()
26        self.status: StatusData = StatusData()
27        self.discord_msg: Message
28
29    def parser(self, body: Message) -> None:
30        self.discord_msg = body
31        self.status.source = f"discord_{self.discord_msg.channel.id}"
32
33        self.data.text = self.discord_msg.content.strip()
34        self.data.event_ts = str(self.discord_msg.created_at.timestamp())
35
36        self.data.thread_ts = "0"
37        if self.discord_msg.reference:
38            if isinstance(self.discord_msg.reference.resolved, Message):
39                self.data.thread_ts = str(self.discord_msg.reference.resolved.created_at.timestamp())
40
41    @property
42    def in_thread(self) -> bool:
43        """
44        リプライメッセージか判定
45
46        Returns:
47            bool: 真偽値
48
49        Note: slackに合わせてプロパティ名に`in_thread`を使う
50
51        """
52        if self.status.command_flg:
53            return False
54
55        if self.discord_msg.reference:
56            return True
57        return False
58
59    @property
60    def is_bot(self) -> bool:
61        return self.discord_msg.author.bot
62
63    @property
64    def check_updatable(self) -> bool:
65        g.adapter = cast("ServiceAdapter", g.adapter)
66
67        # 突合処理中はチェック省略
68        if self.status.command_type == CommandType.COMPARISON:
69            return True
70
71        # スレッド内では常に禁止
72        if isinstance(self.discord_msg.channel, Thread):
73            return False
74
75        # 禁止リストが空の場合は全チャンネルで許可
76        if not g.adapter.conf.channel_limitations:
77            return True
78
79        # チャンネルIDでチェック
80        if str(self.discord_msg.channel.id) in g.adapter.conf.channel_limitations:
81            return True
82
83        # チャンネル名でチェック
84        if isinstance(self.discord_msg.channel, TextChannel):
85            if self.discord_msg.channel.name in g.adapter.conf.channel_limitations:
86                return True
87
88        return False
89
90    @property
91    def ignore_user(self) -> bool:
92        return False
20class MessageParser(MessageParserDataMixin, MessageParserInterface):
21    """メッセージ解析クラス"""
22
23    def __init__(self) -> None:
24        MessageParserDataMixin.__init__(self)
25        self.data: MsgData = MsgData()
26        self.post: PostData = PostData()
27        self.status: StatusData = StatusData()
28        self.discord_msg: Message
29
30    def parser(self, body: Message) -> None:
31        self.discord_msg = body
32        self.status.source = f"discord_{self.discord_msg.channel.id}"
33
34        self.data.text = self.discord_msg.content.strip()
35        self.data.event_ts = str(self.discord_msg.created_at.timestamp())
36
37        self.data.thread_ts = "0"
38        if self.discord_msg.reference:
39            if isinstance(self.discord_msg.reference.resolved, Message):
40                self.data.thread_ts = str(self.discord_msg.reference.resolved.created_at.timestamp())
41
42    @property
43    def in_thread(self) -> bool:
44        """
45        リプライメッセージか判定
46
47        Returns:
48            bool: 真偽値
49
50        Note: slackに合わせてプロパティ名に`in_thread`を使う
51
52        """
53        if self.status.command_flg:
54            return False
55
56        if self.discord_msg.reference:
57            return True
58        return False
59
60    @property
61    def is_bot(self) -> bool:
62        return self.discord_msg.author.bot
63
64    @property
65    def check_updatable(self) -> bool:
66        g.adapter = cast("ServiceAdapter", g.adapter)
67
68        # 突合処理中はチェック省略
69        if self.status.command_type == CommandType.COMPARISON:
70            return True
71
72        # スレッド内では常に禁止
73        if isinstance(self.discord_msg.channel, Thread):
74            return False
75
76        # 禁止リストが空の場合は全チャンネルで許可
77        if not g.adapter.conf.channel_limitations:
78            return True
79
80        # チャンネルIDでチェック
81        if str(self.discord_msg.channel.id) in g.adapter.conf.channel_limitations:
82            return True
83
84        # チャンネル名でチェック
85        if isinstance(self.discord_msg.channel, TextChannel):
86            if self.discord_msg.channel.name in g.adapter.conf.channel_limitations:
87                return True
88
89        return False
90
91    @property
92    def ignore_user(self) -> bool:
93        return False

メッセージ解析クラス

discord_msg: discord.message.Message
def parser(self, body: discord.message.Message) -> None:
30    def parser(self, body: Message) -> None:
31        self.discord_msg = body
32        self.status.source = f"discord_{self.discord_msg.channel.id}"
33
34        self.data.text = self.discord_msg.content.strip()
35        self.data.event_ts = str(self.discord_msg.created_at.timestamp())
36
37        self.data.thread_ts = "0"
38        if self.discord_msg.reference:
39            if isinstance(self.discord_msg.reference.resolved, Message):
40                self.data.thread_ts = str(self.discord_msg.reference.resolved.created_at.timestamp())

メッセージ解析

Arguments:
  • body (Any): 解析データ
in_thread: bool
42    @property
43    def in_thread(self) -> bool:
44        """
45        リプライメッセージか判定
46
47        Returns:
48            bool: 真偽値
49
50        Note: slackに合わせてプロパティ名に`in_thread`を使う
51
52        """
53        if self.status.command_flg:
54            return False
55
56        if self.discord_msg.reference:
57            return True
58        return False

リプライメッセージか判定

Returns:

bool: 真偽値

Note: slackに合わせてプロパティ名にin_threadを使う

is_bot: bool
60    @property
61    def is_bot(self) -> bool:
62        return self.discord_msg.author.bot

botのポストかチェック

Returns:

bool: 真偽値

  • True: botのポスト
  • False: ユーザのポスト
check_updatable: bool
64    @property
65    def check_updatable(self) -> bool:
66        g.adapter = cast("ServiceAdapter", g.adapter)
67
68        # 突合処理中はチェック省略
69        if self.status.command_type == CommandType.COMPARISON:
70            return True
71
72        # スレッド内では常に禁止
73        if isinstance(self.discord_msg.channel, Thread):
74            return False
75
76        # 禁止リストが空の場合は全チャンネルで許可
77        if not g.adapter.conf.channel_limitations:
78            return True
79
80        # チャンネルIDでチェック
81        if str(self.discord_msg.channel.id) in g.adapter.conf.channel_limitations:
82            return True
83
84        # チャンネル名でチェック
85        if isinstance(self.discord_msg.channel, TextChannel):
86            if self.discord_msg.channel.name in g.adapter.conf.channel_limitations:
87                return True
88
89        return False

DB操作の許可チェック

Returns:

bool: 真偽値

  • True: 許可
  • False: 禁止
ignore_user: bool
91    @property
92    def ignore_user(self) -> bool:
93        return False

ignore_useridに存在するユーザかチェック

Returns:

bool: 真偽値

  • True: 存在する(操作禁止ユーザ)
  • False: 存在しない