diff --git a/markov/ircplugin.py b/markov/ircplugin.py index 251e3e8..733740b 100644 --- a/markov/ircplugin.py +++ b/markov/ircplugin.py @@ -74,6 +74,11 @@ class Markov(Plugin): else: # learn the line learning_what = what + + # don't learn the speaker's nick if this came over a bridge + if channel and who == channel.discord_bridge: + learning_what = ' '.join(learning_what.split(' ')[1:]) + # remove our own nick and aliases from what we learn if connection.server_config.additional_addressed_nicks: all_nicks = '|'.join(connection.server_config.additional_addressed_nicks.split('\n') + @@ -82,10 +87,6 @@ class Markov(Plugin): all_nicks = connection.get_nickname() learning_what = re.sub(r'^(({nicks})[:,]|@({nicks}))\s+'.format(nicks=all_nicks), '', learning_what) - # don't learn the speaker's nick if this came over a bridge - if channel and who == channel.discord_bridge: - learning_what = ' '.join(learning_what.split(' ')[1:]) - recursing = getattr(event, 'recursing', False) if not recursing: log.debug("learning %s", learning_what) diff --git a/tests/test_markov_ircplugin.py b/tests/test_markov_ircplugin.py index f159547..c2a6f82 100644 --- a/tests/test_markov_ircplugin.py +++ b/tests/test_markov_ircplugin.py @@ -72,3 +72,31 @@ class MarkovTestCase(TestCase): self.plugin.handle_chatter(self.mock_connection, mock_event) self.assertEqual(mock_learn_line.call_args.args[0], 'hello this is a test message') + + def test_learn_bridge_and_self_edit(self): + """Test that we don't learn our own name when learning something addressed to us, discord style.""" + mock_event = mock.MagicMock() + mock_event.arguments = [' test_bot: hello this is a test message'] + mock_event.target = '#test' + mock_event.recursing = False + mock_event.source = 'bridge!bridge@localhost' + + with mock.patch('markov.lib.learn_line') as mock_learn_line: + with mock.patch('markov.lib.generate_line'): + self.plugin.handle_chatter(self.mock_connection, mock_event) + + self.assertEqual(mock_learn_line.call_args.args[0], 'hello this is a test message') + + def test_learn_bridge_and_variant_self_edit(self): + """Test that we don't learn our own name when learning something addressed to us, discord style.""" + mock_event = mock.MagicMock() + mock_event.arguments = [' @test_bot hello this is a test message'] + mock_event.target = '#test' + mock_event.recursing = False + mock_event.source = 'bridge!bridge@localhost' + + with mock.patch('markov.lib.learn_line') as mock_learn_line: + with mock.patch('markov.lib.generate_line'): + self.plugin.handle_chatter(self.mock_connection, mock_event) + + self.assertEqual(mock_learn_line.call_args.args[0], 'hello this is a test message')