account for the discord bridge in the core bot addressed flag

This commit is contained in:
Brian S. Stephan 2023-02-19 21:12:01 -06:00
parent 88ea0dbbb4
commit 55d856b8fd
Signed by: bss
GPG Key ID: 3DE06D3180895FCB
2 changed files with 96 additions and 1 deletions

View File

@ -171,7 +171,18 @@ class DrReactor(irc.client.Reactor):
else:
all_nicks = connection.get_nickname()
addressed_pattern = r'^(({nicks})[:,]|@({nicks}))\s+(?P<addressed_msg>.*)'.format(nicks=all_nicks)
match = re.match(addressed_pattern, what, re.IGNORECASE)
# ignore the first word, a nick, if the speaker is the bridge
try:
channel = IrcChannel.objects.get(name=sent_location)
if sender_nick == channel.discord_bridge:
short_what = ' '.join(what.split(' ')[1:])
match = re.match(addressed_pattern, short_what, re.IGNORECASE)
else:
match = re.match(addressed_pattern, what, re.IGNORECASE)
except IrcChannel.DoesNotExist:
match = re.match(addressed_pattern, what, re.IGNORECASE)
if match:
event.addressed = True
event.addressed_msg = match.group('addressed_msg')

84
tests/test_ircbot_bot.py Normal file
View File

@ -0,0 +1,84 @@
"""Test core methods of the IRC bot."""
from unittest import mock
from django.test import TestCase
from ircbot.bot import DrReactor
from ircbot.models import IrcServer
class DrReactorTestCase(TestCase):
"""Test the bot innards."""
fixtures = ['tests/fixtures/irc_server_fixture.json']
def setUp(self):
"""Create common objects."""
self.bot = DrReactor()
self.mock_connection = mock.MagicMock()
self.mock_connection.get_nickname.return_value = 'test_bot'
self.mock_connection.server_config = IrcServer.objects.get(pk=1)
def test_handle_event_not_addressed(self):
"""Test that the core identifies not being addressed."""
mock_event = mock.MagicMock()
mock_event.arguments = ['someone: hello this is a test message']
mock_event.type = 'pubmsg'
mock_event.target = '#test'
self.bot._handle_event(self.mock_connection, mock_event)
self.assertFalse(mock_event.addressed)
def test_handle_event_addressed(self):
"""Test that the core identifies being addressed IRC style."""
mock_event = mock.MagicMock()
mock_event.arguments = ['test_bot: hello this is a test message']
mock_event.type = 'pubmsg'
mock_event.target = '#test'
self.bot._handle_event(self.mock_connection, mock_event)
self.assertTrue(mock_event.addressed)
def test_handle_event_addressed_alternate(self):
"""Test that the core identifies being addressed in the alternate (e.g. discord) style."""
mock_event = mock.MagicMock()
mock_event.arguments = ['@test_bot hello this is a test message']
mock_event.type = 'pubmsg'
mock_event.target = '#test'
self.bot._handle_event(self.mock_connection, mock_event)
self.assertTrue(mock_event.addressed)
def test_handle_event_bridge_addressed(self):
"""Test that the core identifies being addressed IRC style through a discord bridge."""
mock_event = mock.MagicMock()
mock_event.arguments = ['<someone> test_bot: hello this is a test message']
mock_event.type = 'pubmsg'
mock_event.target = '#test'
mock_event.source = 'bridge!bridge@localhost'
self.bot._handle_event(self.mock_connection, mock_event)
self.assertTrue(mock_event.addressed)
def test_handle_event_bridge_addressed_alternate(self):
"""Test that the core identifies being addressed in the alternate style through a discord bridge."""
mock_event = mock.MagicMock()
mock_event.arguments = ['<someone> @test_bot hello this is a test message']
mock_event.type = 'pubmsg'
mock_event.target = '#test'
mock_event.source = 'bridge!bridge@localhost'
self.bot._handle_event(self.mock_connection, mock_event)
self.assertTrue(mock_event.addressed)
def test_handle_event_not_addressed_not_bridge(self):
"""Test that the core identifies not to chop the first word from a paste not from the bridge user."""
mock_event = mock.MagicMock()
mock_event.arguments = ['<somebody> test_bot: hello this is a test message']
mock_event.type = 'pubmsg'
mock_event.target = '#test'
mock_event.source = 'not-bridge!not-bridge@localhost'
self.bot._handle_event(self.mock_connection, mock_event)
self.assertFalse(mock_event.addressed)