Skip to content

Commit

Permalink
Merge pull request #43 from Dunedan/improve-bot-reactions
Browse files Browse the repository at this point in the history
Improve bot reactions
  • Loading branch information
Dunedan authored May 24, 2024
2 parents 532bd1b + f131d4b commit 8a59fe7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
3 changes: 3 additions & 0 deletions xpartamupp/echelon.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,9 @@ def _muc_message(self, msg):
msg (slixmpp.stanza.message.Message): Received MUC
message
"""
if msg["delay"]["stamp"]:
return

if msg['mucnick'] == self.nick or self.nick.lower() not in msg['body'].lower():
return

Expand Down
40 changes: 39 additions & 1 deletion xpartamupp/modbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
ArgumentError, ArgumentParser, HelpFormatter, Namespace,
_MutuallyExclusiveGroup)
from asyncio import Future, Task
from datetime import datetime, timezone
from datetime import datetime, timedelta, timezone
from typing import Iterable, Optional, Tuple

import dateparser
Expand All @@ -44,6 +44,10 @@
UnmuteEvent)
from xpartamupp.utils import ArgumentParserWithConfigFile

# Number of seconds to not respond to mentions after having responded
# to a mention.
INFO_MSG_COOLDOWN_SECONDS = 120

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -232,9 +236,12 @@ def __init__(self, jid: JID, password: str, nick: str, rooms: list[JID], command
self.unmute_tasks: dict[JID, asyncio.Task] = {}
self.cmd_parser = get_cmd_parser()

self.last_info_msg = None

self.add_event_handler("session_start", self._session_start)

for room in self.rooms:
self.add_event_handler(f"muc::{room}::message", self._muc_message)
self.add_event_handler(f"muc::{room}::presence", self._muc_presence_change)
self.add_event_handler(f"muc::{self.command_room}::message", self._muc_command_message)

Expand Down Expand Up @@ -341,6 +348,37 @@ async def _muc_presence_change(self, presence: MUCPresence) -> None:
except IqError:
logger.exception("Unmuting %s (%s) on join failed.", nick, jid)

async def _muc_message(self, msg):
"""Process messages in the MUC room.
Respond to messages highlighting the bots name with an
informative message. After responding once, cool down before
responding again to avoid spamming info messages when mentioned
repeatedly.
Arguments:
msg (slixmpp.stanza.message.Message): Received MUC message
"""
if msg["delay"]["stamp"]:
return

if msg['mucnick'] == self.nick or self.nick.lower() not in msg['body'].lower():
return

if (
self.last_info_msg and
self.last_info_msg + timedelta(seconds=INFO_MSG_COOLDOWN_SECONDS) > datetime.now(
tz=timezone.utc)
):
return

self.last_info_msg = datetime.now(tz=timezone.utc)
self.send_message(mto=msg['from'].bare,
mbody="I am just a bot and I'm here to monitor that you respect the "
"terms of use and interact with each other in a respectful "
"manner.",
mtype='groupchat')

async def _muc_command_message(self, msg: Message) -> None:
"""Process messages in the command MUC room.
Expand Down
3 changes: 3 additions & 0 deletions xpartamupp/xpartamupp.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ def _muc_message(self, msg):
msg (slixmpp.stanza.message.Message): Received MUC
message
"""
if msg["delay"]["stamp"]:
return

if msg['mucnick'] == self.nick or self.nick.lower() not in msg['body'].lower():
return

Expand Down

0 comments on commit 8a59fe7

Please sign in to comment.