diff --git a/dms.py b/dms.py index 14ca3d3e..5e21ec81 100644 --- a/dms.py +++ b/dms.py @@ -22,18 +22,6 @@ REQUESTS_LIMIT_EXPIRE = timedelta(days=1) REQUESTS_LIMIT_USER = 10 -COMMANDS = ( - 'block', - 'did', - 'help', - 'no', - 'ok', - 'start', - 'stop', - 'unblock', - 'username', - 'yes', -) # populated by the command() decorator _commands = {} @@ -339,17 +327,15 @@ def receive(*, from_user, obj): if not tokens or len(tokens) > 2: return r'¯\_(ツ)_/¯', 204 - if tokens[0].lstrip('/') in COMMANDS: - cmd = tokens[0].lstrip('/') - arg = tokens[1] if len(tokens) > 1 else None - else: - cmd = None - arg = tokens[0] + if fn := _commands.get(tokens[0]): + return fn(from_user, to_proto, dm_as1=inner_as1, + cmd=tokens[0], cmd_arg=tokens[1] if len(tokens) == 2 else None) + elif len(tokens) == 1: + fn = _commands.get(None) + assert fn, tokens[0] + return fn(from_user, to_proto, dm_as1=inner_as1, cmd=None, cmd_arg=tokens[0]) - if fn := _commands.get(cmd): - return fn(from_user, to_proto, cmd, arg, inner_as1) - - error(f"Couldn't understand DM: {text}", status=304) + return r'¯\_(ツ)_/¯', 204 def load_user(proto, handle): diff --git a/tests/test_dms.py b/tests/test_dms.py index 122de1d0..281c3221 100644 --- a/tests/test_dms.py +++ b/tests/test_dms.py @@ -429,16 +429,18 @@ def test_receive_username_fails(self, _): self.assertEqual({}, OtherFake.usernames) def test_receive_help(self): - self.make_user(id='other.brid.gy', cls=Web) - alice = self.make_user(id='efake:alice', cls=ExplicitFake, - enabled_protocols=['other'], obj_as1={'x': 'y'}) - obj = Object(our_as1={ - **DM_BASE, - 'content': '/help', - }) - self.assertEqual(('OK', 200), receive(from_user=alice, obj=obj)) - self.assert_replied(OtherFake, alice, '?', "
Hi! I'm a friendly bot") - self.assertEqual({}, OtherFake.usernames) + for command in 'help', 'hello', '?': + ExplicitFake.sent = [] + self.make_user(id='other.brid.gy', cls=Web) + alice = self.make_user(id='efake:alice', cls=ExplicitFake, + enabled_protocols=['other'], obj_as1={'x': 'y'}) + obj = Object(our_as1={ + **DM_BASE, + 'content': command, + }) + self.assertEqual(('OK', 200), receive(from_user=alice, obj=obj)) + self.assert_replied(OtherFake, alice, '?', "
Hi! I'm a friendly bot") + self.assertEqual({}, OtherFake.usernames) def test_receive_help_strip_mention_of_bot(self): self.make_user(id='other.brid.gy', cls=Web) @@ -446,10 +448,10 @@ def test_receive_help_strip_mention_of_bot(self): enabled_protocols=['other'], obj_as1={'x': 'y'}) for content in ( - '@other.brid.gy /help', - 'other.brid.gy@other.brid.gy /help', - '@other.brid.gy@other.brid.gy /help', - 'https://other.brid.gy/other.brid.gy /help', + '@other.brid.gy help', + 'other.brid.gy@other.brid.gy help', + '@other.brid.gy@other.brid.gy help', + 'https://other.brid.gy/other.brid.gy help', ): ExplicitFake.sent = [] with self.subTest(content=content):