g1mp/plugins/whois.py

56 lines
2.1 KiB
Python
Raw Permalink Normal View History

2025-02-13 04:55:42 +00:00
import irc3
import re
from irc3 import dec
@irc3.plugin
class AsyncNotifyOnJoin:
def __init__(self, bot):
self.bot = bot
# Ensure async_cmds is available
if not hasattr(self.bot, 'async_cmds'):
self.bot.log.error("async_cmds plugin not loaded. Please add 'irc3.plugins.asynchronious' to your config.")
raise RuntimeError("Missing async_cmds plugin")
@dec.extend
async def notify_users(self, channel, joiner):
"""Async method to get channel users and send notifications"""
try:
# Ensure channel is in the bot's channel list
if channel not in self.bot.channels:
self.bot.log.warning(f"Bot is not in channel: {channel}")
return
# Debug: Log the async_cmds object
self.bot.log.debug(f"async_cmds: {self.bot.async_cmds}")
# Debug: Log the names method
if hasattr(self.bot.async_cmds, 'names'):
self.bot.log.debug("async_cmds.names() is available")
else:
self.bot.log.error("async_cmds.names() is missing!")
# Get fresh user list through IRC protocol
result = await self.bot.async_cmds.names(channel)
self.bot.log.debug(f"Result from async_cmds.names(): {result}")
if result and result.get('success'):
for nickname in result['names']:
# Clean nickname from mode prefixes
clean_nick = re.sub(r'^[@~+&%]', '', nickname)
if clean_nick != self.bot.nick:
self.bot.privmsg(
clean_nick,
f"{joiner} has joined {channel}. Welcome them!"
)
else:
self.bot.log.error(f"Failed to fetch user list for {channel}")
except Exception as e:
self.bot.log.error(f"Notification failed: {e}")
@irc3.event(irc3.rfc.JOIN)
def on_join(self, channel, **kwargs):
"""Trigger async notification process on join"""
joiner = kwargs['mask'].nick
if joiner != self.bot.nick:
# Create async task to handle notifications
self.bot.loop.create_task(self.notify_users(channel, joiner))