From 52f8af5d8c380eb7467f9160e7baffd8dbd3434b Mon Sep 17 00:00:00 2001 From: chillymosh <86857777+chillymosh@users.noreply.github.com> Date: Sat, 27 May 2023 14:29:22 +0100 Subject: [PATCH] Add notice events Add notice events --- docs/changelog.rst | 5 ++++- twitchio/client.py | 51 +++++++++++++++++++++++++++++++++++++++++++ twitchio/parse.py | 12 +--------- twitchio/websocket.py | 10 +++++++++ 4 files changed, 66 insertions(+), 12 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 5b83965c..bddab345 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -7,7 +7,10 @@ Master - Added :func:`~twitchio.Client.fetch_global_chat_badges` - Added User method :func:`~twitchio.PartialUser.fetch_chat_badges` - Added repr for :class:`~twitchio.SearchUser` - + - Added two new events + - Added ::func:`~twitchio.Client.event_notice` + - Added ::func:`~twitchio.Client.event_raw_notice` + - Bug fixes - Fix :func:`~twitchio.Client.search_categories` due to :attr:`~twitchio.Game.igdb_id` being added to :class:`~twitchio.Game` - Made Chatter :attr:`~twitchio.Chatter.id` property public diff --git a/twitchio/client.py b/twitchio/client.py index bbeed278..6df91621 100644 --- a/twitchio/client.py +++ b/twitchio/client.py @@ -1055,3 +1055,54 @@ async def event_channel_join_failure(self, channel: str): The channel name that was attempted to be joined. """ logger.error(f'The channel "{channel}" was unable to be joined. Check the channel is valid.') + + async def event_raw_notice(self, data: str): + """|coro| + + + Event called with the raw NOTICE data received by Twitch. + + Parameters + ------------ + data: str + The raw NOTICE data received from Twitch. + + Example + --------- + .. code:: py + + @bot.event() + async def event_raw_notice(data): + print(data) + """ + pass + + async def event_notice(self, msg_id: str, channel: Channel, message: str): + """|coro| + + + Event called with the NOTICE data received by Twitch. + + .. tip:: + + For more information on NOTICE msg_ids visit: + https://dev.twitch.tv/docs/irc/msg-id/ + + Parameters + ------------ + msg_id: str + The msg_id that indicates what the NOTICE type. + channel: str + The channel the NOTICE message originated from. + message: str + The message of the NOTICE. + + Example + --------- + .. code:: py + + @bot.event() + async def event_notice(msg_id, channel, message): + print(data) + """ + pass diff --git a/twitchio/parse.py b/twitchio/parse.py index 6ae5e7ce..2cff6e9d 100644 --- a/twitchio/parse.py +++ b/twitchio/parse.py @@ -31,17 +31,7 @@ if typing.TYPE_CHECKING: from .websocket import WSConnection -ACTIONS = ( - "JOIN", - "PART", - "PING", - "PRIVMSG", - "PRIVMSG(ECHO)", - "USERSTATE", - "MODE", - "WHISPER", - "USERNOTICE", -) +ACTIONS = ("JOIN", "PART", "PING", "PRIVMSG", "PRIVMSG(ECHO)", "USERSTATE", "MODE", "WHISPER", "USERNOTICE", "NOTICE") ACTIONS2 = ("USERSTATE", "ROOMSTATE", "PRIVMSG", "USERNOTICE", "WHISPER") USER_SUB = re.compile(r":(?P.*)!") MESSAGE_RE = re.compile(r":(?P\S+) (?P\S+) (?P\S+)( :(?P.*))?$") diff --git a/twitchio/websocket.py b/twitchio/websocket.py index e969f814..c90fa98d 100644 --- a/twitchio/websocket.py +++ b/twitchio/websocket.py @@ -82,6 +82,7 @@ def __init__( "PRIVMSG(ECHO)": self._privmsg_echo, "USERSTATE": self._userstate, "USERNOTICE": self._usernotice, + "NOTICE": self._notice, "JOIN": self._join, "MODE": self._mode, "RECONNECT": self._reconnect, @@ -501,6 +502,15 @@ async def _usernotice(self, parsed): self.dispatch("raw_usernotice", channel, tags) + async def _notice(self, parsed): + log.debug(f'ACTION: NOTICE:: {parsed["channel"]}') + msg_id = parsed["groups"][0].split("=")[1] + channel = Channel(name=parsed["channel"], websocket=self) + message = parsed["message"] + + self.dispatch("raw_notice", parsed["data"]) + self.dispatch("notice", msg_id, channel, message) + async def _join(self, parsed): log.debug(f'ACTION: JOIN:: {parsed["channel"]}') channel = parsed["channel"]