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,
12                                         MessageParserInterface)
13from integrations.protocols import MsgData, PostData, StatusData
14
15if TYPE_CHECKING:
16    from integrations.discord.adapter import ServiceAdapter
17
18
19class MessageParser(MessageParserDataMixin, MessageParserInterface):
20    """メッセージ解析クラス"""
21
22    def __init__(self):
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):
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        Note: slackに合わせてプロパティ名に`in_thread`を使う
45        """
46
47        if self.status.command_flg:
48            return False
49
50        if self.discord_msg.reference:
51            return True
52        return False
53
54    @property
55    def is_bot(self) -> bool:
56        return self.discord_msg.author.bot
57
58    @property
59    def check_updatable(self) -> bool:
60        g.adapter = cast("ServiceAdapter", g.adapter)
61
62        # 突合処理中はチェック省略
63        if self.status.command_type == "comparison":
64            return True
65
66        # スレッド内では常に禁止
67        if isinstance(self.discord_msg.channel, Thread):
68            return False
69
70        # 禁止リストが空の場合は全チャンネルで許可
71        if not g.adapter.conf.channel_limitations:
72            return True
73
74        # チャンネルIDでチェック
75        if self.discord_msg.channel.id in g.adapter.conf.channel_limitations:
76            return True
77
78        # チャンネル名でチェック
79        if isinstance(self.discord_msg.channel, TextChannel):
80            if self.discord_msg.channel.name in g.adapter.conf.channel_limitations:
81                return True
82
83        return False
84
85    @property
86    def ignore_user(self) -> bool:
87        return False
20class MessageParser(MessageParserDataMixin, MessageParserInterface):
21    """メッセージ解析クラス"""
22
23    def __init__(self):
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):
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        Note: slackに合わせてプロパティ名に`in_thread`を使う
46        """
47
48        if self.status.command_flg:
49            return False
50
51        if self.discord_msg.reference:
52            return True
53        return False
54
55    @property
56    def is_bot(self) -> bool:
57        return self.discord_msg.author.bot
58
59    @property
60    def check_updatable(self) -> bool:
61        g.adapter = cast("ServiceAdapter", g.adapter)
62
63        # 突合処理中はチェック省略
64        if self.status.command_type == "comparison":
65            return True
66
67        # スレッド内では常に禁止
68        if isinstance(self.discord_msg.channel, Thread):
69            return False
70
71        # 禁止リストが空の場合は全チャンネルで許可
72        if not g.adapter.conf.channel_limitations:
73            return True
74
75        # チャンネルIDでチェック
76        if self.discord_msg.channel.id in g.adapter.conf.channel_limitations:
77            return True
78
79        # チャンネル名でチェック
80        if isinstance(self.discord_msg.channel, TextChannel):
81            if self.discord_msg.channel.name in g.adapter.conf.channel_limitations:
82                return True
83
84        return False
85
86    @property
87    def ignore_user(self) -> bool:
88        return False

メッセージ解析クラス

discord_msg: discord.message.Message
def parser(self, body: discord.message.Message):
30    def parser(self, body: Message):
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        Note: slackに合わせてプロパティ名に`in_thread`を使う
46        """
47
48        if self.status.command_flg:
49            return False
50
51        if self.discord_msg.reference:
52            return True
53        return False

リプライメッセージか判定 Note: slackに合わせてプロパティ名にin_threadを使う

is_bot: bool
55    @property
56    def is_bot(self) -> bool:
57        return self.discord_msg.author.bot

botのポストかチェック

Returns:

bool: 真偽値

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

DB操作の許可チェック

Returns:

bool: 真偽値

  • True : 許可
  • False : 禁止
ignore_user: bool
86    @property
87    def ignore_user(self) -> bool:
88        return False

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

Returns:

bool: 真偽値

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