def test_permit_alwaysallowed(self): alloweduser = twitch.TwitchUser('alloweduser') self.assert_allowed('example.com', alloweduser) mod = twitch.TwitchUser('mod', display_name='Mod', is_moderator=True) self.assertTrue(self.permit_handler.check(self.message('!permit alloweduser', mod))) self.assertEqual(self.permit_handler.run(self.message('!permit alloweduser', mod)), "@Mod That's okay, alloweduser is always allowed to post links.")
def test_permit(self): self.assert_timeout('post a link', 15, 'example.com') mod = twitch.TwitchUser('mod', display_name='Mod', is_moderator=True) self.assertTrue(self.permit_handler.check(self.message('!permit user', mod))) self.assertEqual(self.permit_handler.run(self.message('!permit user', mod)), 'user is now permitted to post a link in the next 45 seconds.') self.assert_timeout('post a link', 15, 'example.com', twitch.TwitchUser('anotheruser')) self.assert_allowed('example.com') self.assert_timeout('post a link', 180, 'example.com')
def twitch_user(self, username, is_moderator: Optional[bool] = None) -> twitch.TwitchUser: return twitch.TwitchUser( name=username, display_name=self.twitch_util.get_display_name(username), is_moderator=is_moderator)
def __init__(self, util: twitch_util.TwitchUtil, on_call_mods: Set[str]): super().__init__() self.twitch_util = util self.on_call_mods = { twitch.TwitchUser(i.lower()) for i in on_call_mods }
def message(self, text: str, user: twitch.TwitchUser = None, action: bool = False, emotes: List[Tuple[str, int, int]] = None) -> twitch.TwitchMessage: if not user: user = twitch.TwitchUser('user', display_name='User') if not emotes: emotes = [] return twitch.TwitchMessage(self.conn, user, text, 'id', None, 12345, action, emotes)
def test_permit_with_at_sign(self): self.assert_timeout('post a link', 15, 'https://pcpartpicker.com/list/VpB96R') mod = twitch.TwitchUser('mod', display_name='Mod', is_moderator=True) self.assertTrue(self.permit_handler.check(self.message('!permit @user', mod))) self.assertEqual(self.permit_handler.run(self.message('!permit @user', mod)), 'user is now permitted to post a link in the next 45 seconds.') self.assert_allowed('a') self.assert_allowed('https://pcpartpicker.com/list/VpB96R')
def test_subs_can_say_most_things(self): sub = twitch.TwitchUser('sub', is_subscriber=True) self.assert_timeout('post a link', 15, 'link.com', user=sub) self.assert_allowed('colored text', user=sub, action=True) emotes = [('emote_id', i * 7, i * 7 + 5) for i in range(28)] self.assert_allowed('VoHiYo ' * 28, user=sub, emotes=emotes) self.assert_allowed('A' * 50, user=sub) self.assert_allowed('!' * 50, user=sub) self.assert_allowed('o' * 50, user=sub)
def test_mods_can_say_anything(self): mod = twitch.TwitchUser('mod', display_name='Mod', is_moderator=True) self.assert_allowed('link.com', user=mod) self.assert_allowed('colored text', user=mod, action=True) emotes = [('emote_id', i * 7, i * 7 + 5) for i in range(28)] self.assert_allowed('VoHiYo ' * 28, user=mod, emotes=emotes) self.assert_allowed('A' * 50, user=mod) self.assert_allowed('!' * 50, user=mod) self.assert_allowed('o' * 50, user=mod)
def run_permit(self, message: base.Message, username: str) -> Optional[str]: if not message.user.moderator: return None if username.startswith('@'): username = username[1:] user = twitch.TwitchUser(username.lower(), display_name=username) if user in self.link_allowed_users: return f"@{message.user} That's okay, {user} is always allowed to post links." if self.is_permitted(user): # We must've already done this just now -- treat it like a cooldown. return None now = datetime.now(timezone.utc) self.data.set_subkey('permitted', user.name, str(now)) return f'{user} is now permitted to post a link in the next 45 seconds.'
def _event_user(event: Dict[str, Any]) -> Optional[twitch.TwitchUser]: if event.get('is_anonymous', False): return None return twitch.TwitchUser(name=event['user_login'], display_name=event['user_name'])
def setUp(self): super().setUp() handlers = moderation_filter.module_group( re.compile('$nomatch'), '', {'allowedurl.fyi'}, {twitch.TwitchUser('alloweduser')}) self.mod_handler, self.permit_handler = handlers self.conn = mock.Mock()