def test_complex_recode(self): orig = twitchirc.auto_message( '@badge-info=subscriber/13;badges=subscriber/12,' 'glhf-pledge/1;color=#DAA520;display-name=Mm2PL;emotes=;flags=;id=4d18ce89-b62a-4f4e-be71-b432b8dd3f86' ';mod=0;room-id=11148817;subscriber=1;tmi-sent-ts=1625487203887;turbo=0;user-id=117691339;user-type= ' ':[email protected] PRIVMSG #pajlada :this is a message with spaces' ) line = bytes(orig).decode().strip('\r\n') self.assertEqual(orig.raw_data, line) self.assertEqual(orig, twitchirc.auto_message(line))
def test_automsg_parse_privmsg(self): input_str = ( '@badge-info=subscriber/13;badges=subscriber/12,' 'glhf-pledge/1;color=#DAA520;display-name=Mm2PL;emotes=;flags=;id=4d18ce89-b62a-4f4e-be71-b432b8dd3f86' ';mod=0;room-id=11148817;subscriber=1;tmi-sent-ts=1625487203887;turbo=0;user-id=117691339;user-type= ' ':[email protected] PRIVMSG #pajlada :-tags') msg = twitchirc.auto_message(input_str) # noinspection DuplicatedCode self.assertEqual('[email protected]', msg.source) self.assertEqual('PRIVMSG', msg.action) self.assertEqual(False, msg.outgoing) self.assertEqual(input_str, msg.raw_data) self.assertEqual('#pajlada :-tags', msg.args) self.assertEqual(['#pajlada', '-tags'], msg.new_args) self.assertEqual( { "badge-info": "subscriber/13", "badges": "subscriber/12,glhf-pledge/1", "color": "#DAA520", "display-name": "Mm2PL", "emotes": "", "flags": "", "id": "4d18ce89-b62a-4f4e-be71-b432b8dd3f86", "mod": "0", "room-id": "11148817", "subscriber": "1", "tmi-sent-ts": "1625487203887", "turbo": "0", "user-id": "117691339", "user-type": "" }, msg.flags)
def _load_recents(self, channel): log('info', f'Attempting to fetch recent messages for channel {channel}.') req = requests.get( f'https://recent-messages.robotty.de/api/v2/recent-messages/{channel}' ) try: data = req.json() except json.decoder.JSONDecodeError as e: log( 'err', f'Unable to fetch recent messages for channel {channel}!\n' f'{e}\n' f'{traceback.format_exc()}') return if req.status_code != 200: log('err', f'Unable to fetch recent messages for channel {channel}!') log('info', repr(data)) return for msg in data['messages']: message = twitchirc.auto_message(msg, main.bot) if isinstance(message, twitchirc.ChannelMessage ): # ignore messages other than normal text self.on_message('recent', message)
def test_parse_escaped_tags(self): input_str = (r'@key=\\.\:\r\n\svalue UNKNOWNCMD') msg = twitchirc.auto_message(input_str) self.assertTrue(msg.__class__ == twitchirc.Message, 'bad automsg') self.assertEqual(None, msg.source) self.assertEqual('UNKNOWNCMD', msg.action) self.assertEqual(False, msg.outgoing) self.assertEqual(input_str, msg.raw_data) self.assertEqual('', msg.args) self.assertEqual([], msg.new_args) self.assertEqual({'key': '\\.;\r\n value'}, msg.flags)
def test_parse_unknown(self): input_str = ('UNKNOWNCMD justinfan1234 :these are some args') msg = twitchirc.auto_message(input_str) self.assertTrue(msg.__class__ == twitchirc.Message, 'bad automsg') self.assertEqual(None, msg.source) self.assertEqual('UNKNOWNCMD', msg.action) self.assertEqual(False, msg.outgoing) self.assertEqual(input_str, msg.raw_data) self.assertEqual('justinfan1234 :these are some args', msg.args) self.assertEqual(['justinfan1234', 'these are some args'], msg.new_args) self.assertEqual({}, msg.flags)
def test_parse_pong(self): input_str = ('PONG :tmi.twitch.tv') msg = twitchirc.auto_message(input_str) self.assertTrue(isinstance(msg, twitchirc.PongMessage), f'bad automsg') self.assertEqual(None, msg.source) self.assertEqual('PONG', msg.action) self.assertEqual(False, msg.outgoing) self.assertEqual(input_str, msg.raw_data) self.assertEqual(':tmi.twitch.tv', msg.args) self.assertEqual(['tmi.twitch.tv'], msg.new_args) self.assertEqual({}, msg.flags)
def test_parse_usernotice(self): input_str = ( '@badge-info=subscriber/0;badges=subscriber/0,' 'turbo/1;color=#E88911;display-name=some_user;emotes=;flags=;id=596ef165-6af4-4f08-acde-b6c72ee50c37;' 'login=some_user;mod=0;msg-id=sub;msg-param-cumulative-months=1;msg-param-months=0;msg-param' '-multimonth-duration=1;msg-param-multimonth-tenure=0;msg-param-should-share-streak=0;msg-param-sub-plan' '-name=look\sat\sthose\sshitty\semotes,\srip\s$5\sLUL;msg-param-sub-plan=1000;msg-param-was-gifted=false;' 'room-id=11148817;subscriber=1;system-msg=some_user\ssubscribed\sat\sTier\s1.;' 'tmi-sent-ts=1626800016285;user-id=493534660;user-type= ' ':tmi.twitch.tv USERNOTICE #pajlada') msg = twitchirc.auto_message(input_str) self.assertTrue(isinstance(msg, twitchirc.UsernoticeMessage), f'bad automsg') self.assertEqual('tmi.twitch.tv', msg.source) self.assertEqual('USERNOTICE', msg.action) self.assertEqual(False, msg.outgoing) self.assertEqual(input_str, msg.raw_data) self.assertEqual(':#pajlada', msg.args) self.assertEqual(['#pajlada'], msg.new_args) expected = { 'badge-info': 'subscriber/0', 'badges': 'subscriber/0,turbo/1', 'color': '#E88911', 'display-name': 'some_user', 'emotes': '', 'flags': '', 'id': '596ef165-6af4-4f08-acde-b6c72ee50c37', 'login': '******', 'mod': '0', 'msg-id': 'sub', 'msg-param-cumulative-months': '1', 'msg-param-months': '0', 'msg-param-multimonth-duration': '1', 'msg-param-multimonth-tenure': '0', 'msg-param-should-share-streak': '0', 'msg-param-sub-plan-name': 'look at those shitty emotes, rip $5 LUL', 'msg-param-sub-plan': '1000', 'msg-param-was-gifted': 'false', 'room-id': '11148817', 'subscriber': '1', 'system-msg': 'some_user subscribed at Tier 1.', 'tmi-sent-ts': '1626800016285', 'user-id': '493534660', 'user-type': '' } covered = set() for k, v in msg.flags.items(): self.assertEqual(expected[k], v, f'bad {k} value') covered.add(k) self.assertEqual(covered, set(expected.keys()))
def test_parse_part(self): input_str = ( ':[email protected] PART #mm2pl' ) msg = twitchirc.auto_message(input_str) self.assertTrue(isinstance(msg, twitchirc.PartMessage), f'bad automsg') self.assertEqual( '[email protected]', msg.source) self.assertEqual('PART', msg.action) self.assertEqual(False, msg.outgoing) self.assertEqual(input_str, msg.raw_data) self.assertEqual(':#mm2pl', msg.args) self.assertEqual(['#mm2pl'], msg.new_args) self.assertEqual({}, msg.flags)
def test_parse_whisper(self): input_str = ( '@badges=glhf-pledge/1;color=#DAA520;display-name=Mm2PL;emotes=;message-id=5439;thread-id' '=117691339_442600612;turbo=0;user-id=117691339;user-type= :[email protected] WHISPER ' 'mm_sutilitybot :test') msg = twitchirc.auto_message(input_str) self.assertEqual(twitchirc.WhisperMessage, msg.__class__, 'bad automsg') self.assertEqual('[email protected]', msg.source) self.assertEqual('WHISPER', msg.action) self.assertEqual(False, msg.outgoing) self.assertEqual(input_str, msg.raw_data) self.assertEqual('mm_sutilitybot :test', msg.args) self.assertEqual(['mm_sutilitybot', 'test'], msg.new_args) self.assertEqual('test', msg.text) self.assertEqual('mm_sutilitybot', msg.user_to) self.assertEqual('mm2pl', msg.user_from) self.assertEqual('mm2pl', msg.user)
def process_messages(self, max_messages: int = 1, mode=-1) -> typing.List[twitchirc.Message]: """ Process the messages from self.receive_buffer Modes: * -1 every message. Nothing goes to self.receive_queue. * 0 chat messages. Other messages go to self.receive_queue. * 1 other messages. Chat messages go to self.receive_queue. * 2 do not return anything. Everything goes to self.receive_queue. :param max_messages: Maximum amount of messages to process. :param mode: What messages to return. :return: All messages specified by `mode`. """ self._remove_parted_channels() messages_to_return = [] messages_to_add_to_recv_queue = [] for _ in range(max_messages): if '\n' not in self.receive_buffer: break message = self.receive_buffer.split('\n', 1)[0] self.receive_buffer = self.receive_buffer.replace( message + '\n', '', 1) # Remove `message` from the buffer. if message == '': continue m = twitchirc.auto_message(message, parent=self) o = self.call_middleware('receive', dict(message=m), True) if o is False: continue if isinstance(m, twitchirc.ChannelMessage): if mode in [-1, 0]: messages_to_return.append(m) else: messages_to_add_to_recv_queue.append(m) else: if mode in [-1, 1]: messages_to_return.append(m) else: messages_to_add_to_recv_queue.append(m) self.receive_queue.extend(messages_to_add_to_recv_queue) return messages_to_return
def test_parse_notice(self): input_str = ( '@msg-id=slow_on :tmi.twitch.tv NOTICE #mm2pl :This room is now in slow mode. You may send messages every ' '10 seconds.') msg = twitchirc.auto_message(input_str) self.assertTrue(isinstance(msg, twitchirc.NoticeMessage), f'bad automsg') self.assertEqual('tmi.twitch.tv', msg.source) self.assertEqual('NOTICE', msg.action) self.assertEqual(False, msg.outgoing) self.assertEqual(input_str, msg.raw_data) self.assertEqual( '#mm2pl :This room is now in slow mode. You may send messages every 10 seconds.', msg.args) self.assertEqual([ '#mm2pl', 'This room is now in slow mode. You may send messages every 10 seconds.' ], msg.new_args) self.assertEqual({'msg-id': 'slow_on'}, msg.flags) self.assertEqual('slow_on', msg.message_id)