From ec1767e38b899304072d2a21a53ee4365ee8869d Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Sat, 18 Feb 2023 18:54:22 -0600 Subject: [PATCH] remove the speaker from messages coming over the bridge when learning --- markov/ircplugin.py | 13 ++++++++++--- tests/test_markov_ircplugin.py | 13 +++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/markov/ircplugin.py b/markov/ircplugin.py index 0784f60..251e3e8 100644 --- a/markov/ircplugin.py +++ b/markov/ircplugin.py @@ -73,17 +73,24 @@ class Markov(Plugin): log.debug("not learning from %s as i've been told to ignore it", channel) else: # learn the line + learning_what = what + # 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') + [connection.get_nickname()]) else: all_nicks = connection.get_nickname() - trimmed_what = re.sub(r'^(({nicks})[:,]|@({nicks}))\s+'.format(nicks=all_nicks), '', what) + 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", trimmed_what) + log.debug("learning %s", learning_what) context = markovlib.get_or_create_target_context(target) - markovlib.learn_line(trimmed_what, context) + markovlib.learn_line(learning_what, context) log.debug("searching '%s' for '%s'", what, all_nicks) if re.search(all_nicks, what, re.IGNORECASE) is not None: diff --git a/tests/test_markov_ircplugin.py b/tests/test_markov_ircplugin.py index aaebc92..02f5c8d 100644 --- a/tests/test_markov_ircplugin.py +++ b/tests/test_markov_ircplugin.py @@ -33,3 +33,16 @@ 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_edit(self): + """Test that we don't learn the speaker's nick when learning a message from the bridge.""" + mock_event = mock.MagicMock() + mock_event.arguments = [' 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: + self.plugin.handle_chatter(self.mock_connection, mock_event) + + self.assertEqual(mock_learn_line.call_args.args[0], 'hello this is a test message')