Greet now only greets on the first enter of a nick, if the bot restarts, or if the greeting changes.
This commit is contained in:
parent
b6b08503f4
commit
75f6dd6f4c
132
Greet/plugin.py
132
Greet/plugin.py
@ -20,17 +20,19 @@ class Greet(callbacks.Plugin):
|
|||||||
"""
|
"""
|
||||||
A plugin to greet users when they join a channel.
|
A plugin to greet users when they join a channel.
|
||||||
Features flood protection and timeout between greetings.
|
Features flood protection and timeout between greetings.
|
||||||
|
Only greets users the first time they join during a session,
|
||||||
|
and resets when the greeting message changes.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, irc):
|
def __init__(self, irc):
|
||||||
self.__parent = super(Greet, self)
|
self.__parent = super(Greet, self)
|
||||||
self.__parent.__init__(irc)
|
self.__parent.__init__(irc)
|
||||||
|
|
||||||
# Dictionary to store last greeting times per user
|
# Dictionary to track which users have been greeted in this session
|
||||||
self.lastGreetTime = {}
|
self.greetedUsers = {}
|
||||||
|
|
||||||
# Default cooldown between greetings for the same user (in seconds)
|
# Dictionary to store the greeting message used for each user
|
||||||
self.userCooldown = 300 # 5 minutes
|
self.userGreetingVersion = {}
|
||||||
|
|
||||||
# Debug mode
|
# Debug mode
|
||||||
self.debugMode = False
|
self.debugMode = False
|
||||||
@ -44,7 +46,8 @@ class Greet(callbacks.Plugin):
|
|||||||
|
|
||||||
# Log event for debugging
|
# Log event for debugging
|
||||||
if self.debugMode:
|
if self.debugMode:
|
||||||
self._logDebug(irc, f"JOIN event detected: {nick} joined {channel}")
|
self._logDebug(irc, f"JOIN event detected: {nick} joined
|
||||||
|
{channel}")
|
||||||
|
|
||||||
# Don't greet ourselves
|
# Don't greet ourselves
|
||||||
if nick == irc.nick:
|
if nick == irc.nick:
|
||||||
@ -65,29 +68,44 @@ class Greet(callbacks.Plugin):
|
|||||||
self._logDebug(irc, f"No greeting message set for {channel}")
|
self._logDebug(irc, f"No greeting message set for {channel}")
|
||||||
return
|
return
|
||||||
|
|
||||||
# Check if this user was greeted recently
|
# Check if we've already greeted this user with this greeting
|
||||||
userKey = f"{nick.lower()}@{channel}"
|
userKey = f"{nick.lower()}@{channel}"
|
||||||
now = time.time()
|
|
||||||
if userKey in self.lastGreetTime:
|
if userKey in self.greetedUsers and channel in
|
||||||
lastTime = self.lastGreetTime[userKey]
|
self.greetedUsers[userKey]:
|
||||||
if now - lastTime < self.userCooldown:
|
# If the greeting has changed since we last greeted this user, we
|
||||||
|
# should greet them again
|
||||||
|
lastGreetingUsed = self.userGreetingVersion.get(userKey, "")
|
||||||
|
if greeting == lastGreetingUsed:
|
||||||
if self.debugMode:
|
if self.debugMode:
|
||||||
self._logDebug(irc, f"User {nick} was greeted too recently")
|
self._logDebug(irc, f"User {nick} already greeted in this
|
||||||
|
session with current greeting")
|
||||||
return
|
return
|
||||||
|
else:
|
||||||
|
if self.debugMode:
|
||||||
|
self._logDebug(irc, f"Greeting for {channel} has changed,
|
||||||
|
will greet {nick} again")
|
||||||
|
|
||||||
# Get the timeout
|
# Get the timeout
|
||||||
timeout = self.registryValue('timeout', channel) or 3
|
timeout = self.registryValue('timeout', channel) or 3
|
||||||
|
|
||||||
if self.debugMode:
|
if self.debugMode:
|
||||||
self._logDebug(irc, f"Will greet in {channel} after {timeout}s: {greeting}")
|
self._logDebug(irc, f"Will greet in {channel} after {timeout}s:
|
||||||
|
{greeting}")
|
||||||
|
|
||||||
# Update last greet time for this user
|
# Mark this user as greeted in this channel
|
||||||
self.lastGreetTime[userKey] = now
|
if userKey not in self.greetedUsers:
|
||||||
|
self.greetedUsers[userKey] = set()
|
||||||
|
self.greetedUsers[userKey].add(channel)
|
||||||
|
|
||||||
|
# Store the greeting version we're using for this user
|
||||||
|
self.userGreetingVersion[userKey] = greeting
|
||||||
|
|
||||||
# Use a closure to capture the current values
|
# Use a closure to capture the current values
|
||||||
def sendGreeting(channelToGreet=channel, greetingMsg=greeting):
|
def sendGreeting(channelToGreet=channel, greetingMsg=greeting):
|
||||||
if self.debugMode:
|
if self.debugMode:
|
||||||
self._logDebug(irc, f"Sending greeting in {channelToGreet}: {greetingMsg}")
|
self._logDebug(irc, f"Sending greeting in {channelToGreet}:
|
||||||
|
{greetingMsg}")
|
||||||
try:
|
try:
|
||||||
# Make sure the channel is still valid when we send the message
|
# Make sure the channel is still valid when we send the message
|
||||||
if channelToGreet in irc.state.channels:
|
if channelToGreet in irc.state.channels:
|
||||||
@ -104,13 +122,15 @@ class Greet(callbacks.Plugin):
|
|||||||
try:
|
try:
|
||||||
# Send to the owner via private message instead of to a channel
|
# Send to the owner via private message instead of to a channel
|
||||||
for owner in self.registryValue('owners') or []:
|
for owner in self.registryValue('owners') or []:
|
||||||
irc.queueMsg(ircmsgs.privmsg(owner, f"[GREET-DEBUG] {message}"))
|
irc.queueMsg(ircmsgs.privmsg(owner, f"[GREET-DEBUG]
|
||||||
|
{message}"))
|
||||||
return
|
return
|
||||||
|
|
||||||
# Fallback: send to the first available channel
|
# Fallback: send to the first available channel
|
||||||
for channel in list(irc.state.channels):
|
for channel in list(irc.state.channels):
|
||||||
try:
|
try:
|
||||||
irc.queueMsg(ircmsgs.privmsg(channel, f"[GREET-DEBUG] {message}"))
|
irc.queueMsg(ircmsgs.privmsg(channel, f"[GREET-DEBUG]
|
||||||
|
{message}"))
|
||||||
break
|
break
|
||||||
except:
|
except:
|
||||||
continue
|
continue
|
||||||
@ -165,6 +185,27 @@ class Greet(callbacks.Plugin):
|
|||||||
else:
|
else:
|
||||||
irc.replySuccess("Debug mode disabled")
|
irc.replySuccess("Debug mode disabled")
|
||||||
|
|
||||||
|
@wrap(['channel'])
|
||||||
|
def resetgreethistory(self, irc, msg, args, channel):
|
||||||
|
"""[<channel>]
|
||||||
|
|
||||||
|
Resets the greeting history for the channel, causing all users to be
|
||||||
|
greeted again
|
||||||
|
on their next join (even if they were greeted before).
|
||||||
|
"""
|
||||||
|
# Clear the greeting history for this channel
|
||||||
|
for userKey in list(self.greetedUsers.keys()):
|
||||||
|
if channel in self.greetedUsers[userKey]:
|
||||||
|
self.greetedUsers[userKey].remove(channel)
|
||||||
|
# If this user has no more channels, remove the entry
|
||||||
|
# completely
|
||||||
|
if not self.greetedUsers[userKey]:
|
||||||
|
del self.greetedUsers[userKey]
|
||||||
|
if userKey in self.userGreetingVersion:
|
||||||
|
del self.userGreetingVersion[userKey]
|
||||||
|
|
||||||
|
irc.replySuccess(f"Greeting history for {channel} has been reset.")
|
||||||
|
|
||||||
@wrap(['channel'])
|
@wrap(['channel'])
|
||||||
def greeton(self, irc, msg, args, channel):
|
def greeton(self, irc, msg, args, channel):
|
||||||
"""[<channel>]
|
"""[<channel>]
|
||||||
@ -174,9 +215,11 @@ class Greet(callbacks.Plugin):
|
|||||||
self.setRegistryValue('enabled', True, channel)
|
self.setRegistryValue('enabled', True, channel)
|
||||||
greeting = self.registryValue('message', channel)
|
greeting = self.registryValue('message', channel)
|
||||||
if greeting:
|
if greeting:
|
||||||
irc.replySuccess(f"Greetings enabled for {channel}. Current greeting: \"{greeting}\"")
|
irc.replySuccess(f"Greetings enabled for {channel}. Current
|
||||||
|
greeting: \"{greeting}\"")
|
||||||
else:
|
else:
|
||||||
irc.error(f"Greetings enabled for {channel}, but no greeting message is set.")
|
irc.error(f"Greetings enabled for {channel}, but no greeting
|
||||||
|
message is set.")
|
||||||
|
|
||||||
@wrap(['channel'])
|
@wrap(['channel'])
|
||||||
def greetoff(self, irc, msg, args, channel):
|
def greetoff(self, irc, msg, args, channel):
|
||||||
@ -195,7 +238,8 @@ class Greet(callbacks.Plugin):
|
|||||||
"""
|
"""
|
||||||
self.setRegistryValue('message', '', channel)
|
self.setRegistryValue('message', '', channel)
|
||||||
self.setRegistryValue('enabled', False, channel)
|
self.setRegistryValue('enabled', False, channel)
|
||||||
irc.replySuccess(f"Greeting message cleared and disabled for {channel}.")
|
irc.replySuccess(f"Greeting message cleared and disabled for
|
||||||
|
{channel}.")
|
||||||
|
|
||||||
@wrap(['channel'])
|
@wrap(['channel'])
|
||||||
def greetstatus(self, irc, msg, args, channel):
|
def greetstatus(self, irc, msg, args, channel):
|
||||||
@ -207,12 +251,20 @@ class Greet(callbacks.Plugin):
|
|||||||
message = self.registryValue('message', channel)
|
message = self.registryValue('message', channel)
|
||||||
timeout = self.registryValue('timeout', channel) or 3
|
timeout = self.registryValue('timeout', channel) or 3
|
||||||
|
|
||||||
|
# Count how many users have been greeted in this channel
|
||||||
|
greetedCount = sum(1 for userKey in self.greetedUsers if channel in
|
||||||
|
self.greetedUsers[userKey])
|
||||||
|
|
||||||
if enabled and message:
|
if enabled and message:
|
||||||
irc.reply(f"Greetings are enabled for {channel}. Timeout: {timeout} seconds. Message: \"{message}\"")
|
irc.reply(f"Greetings are enabled for {channel}. Timeout: {timeout}
|
||||||
|
seconds. Message: \"{message}\"")
|
||||||
|
irc.reply(f"Users greeted in this session: {greetedCount}")
|
||||||
elif enabled and not message:
|
elif enabled and not message:
|
||||||
irc.reply(f"Greetings are enabled for {channel}, but no message is set.")
|
irc.reply(f"Greetings are enabled for {channel}, but no message is
|
||||||
|
set.")
|
||||||
else:
|
else:
|
||||||
irc.reply(f"Greetings are disabled for {channel}. Message: \"{message or 'None'}\"")
|
irc.reply(f"Greetings are disabled for {channel}. Message:
|
||||||
|
\"{message or 'None'}\"")
|
||||||
|
|
||||||
@wrap(['channel', 'positiveInt'])
|
@wrap(['channel', 'positiveInt'])
|
||||||
def greettimeout(self, irc, msg, args, channel, seconds):
|
def greettimeout(self, irc, msg, args, channel, seconds):
|
||||||
@ -222,15 +274,18 @@ class Greet(callbacks.Plugin):
|
|||||||
Default is 3 seconds.
|
Default is 3 seconds.
|
||||||
"""
|
"""
|
||||||
self.setRegistryValue('timeout', seconds, channel)
|
self.setRegistryValue('timeout', seconds, channel)
|
||||||
irc.replySuccess(f"Greeting timeout for {channel} set to {seconds} seconds.")
|
irc.replySuccess(f"Greeting timeout for {channel} set to {seconds}
|
||||||
|
seconds.")
|
||||||
|
|
||||||
@wrap(['channel', 'text'])
|
@wrap(['channel', 'text'])
|
||||||
def greet(self, irc, msg, args, channel, text):
|
def greet(self, irc, msg, args, channel, text):
|
||||||
"""[<channel>] <message>
|
"""[<channel>] <message>
|
||||||
|
|
||||||
Sets the greeting message for a channel.
|
Sets the greeting message for a channel.
|
||||||
The greeting will be sent when users join the channel, with flood
|
The greeting will be sent when users join the channel for the first
|
||||||
protection.
|
time
|
||||||
|
in a session. If the greeting message changes, users will be greeted
|
||||||
|
again.
|
||||||
|
|
||||||
To manage greetings, use the other commands:
|
To manage greetings, use the other commands:
|
||||||
greeton - Enables greetings for the channel
|
greeton - Enables greetings for the channel
|
||||||
@ -241,13 +296,36 @@ protection.
|
|||||||
greettrigger - Simulates a join event with timeout (owner only)
|
greettrigger - Simulates a join event with timeout (owner only)
|
||||||
greetstatus - Shows current status and message
|
greetstatus - Shows current status and message
|
||||||
greettimeout <seconds> - Sets delay before greeting
|
greettimeout <seconds> - Sets delay before greeting
|
||||||
|
resetgreethistory - Resets who has been greeted, greeting everyone
|
||||||
|
again
|
||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
greet #channel Welcome to our channel!
|
greet #channel Welcome to our channel!
|
||||||
"""
|
"""
|
||||||
|
oldGreeting = self.registryValue('message', channel)
|
||||||
self.setRegistryValue('message', text, channel)
|
self.setRegistryValue('message', text, channel)
|
||||||
self.setRegistryValue('enabled', True, channel)
|
self.setRegistryValue('enabled', True, channel)
|
||||||
irc.replySuccess(f"Greeting message for {channel} set to: \"{text}\"")
|
|
||||||
|
# If greeting message has changed, reset who has been greeted with this
|
||||||
|
# message
|
||||||
|
if oldGreeting != text:
|
||||||
|
# Mark that the greeting has changed by removing this channel from
|
||||||
|
# all users' greeted lists
|
||||||
|
for userKey in list(self.greetedUsers.keys()):
|
||||||
|
if channel in self.greetedUsers[userKey]:
|
||||||
|
self.greetedUsers[userKey].remove(channel)
|
||||||
|
# If this user has no more channels, remove the entry
|
||||||
|
# completely
|
||||||
|
if not self.greetedUsers[userKey]:
|
||||||
|
del self.greetedUsers[userKey]
|
||||||
|
if userKey in self.userGreetingVersion:
|
||||||
|
del self.userGreetingVersion[userKey]
|
||||||
|
|
||||||
|
irc.replySuccess(f"Greeting message for {channel} set to:
|
||||||
|
\"{text}\" (greeting history reset)")
|
||||||
|
else:
|
||||||
|
irc.replySuccess(f"Greeting message for {channel} set to:
|
||||||
|
\"{text}\"")
|
||||||
|
|
||||||
Class = Greet
|
Class = Greet
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user