Some minor code cleanup.

This commit is contained in:
Storm Dragon
2025-03-30 13:24:36 -04:00
parent d97e7fa7b5
commit b6b08503f4
4 changed files with 107 additions and 107 deletions

View File

@ -21,17 +21,17 @@ class Greet(callbacks.Plugin):
A plugin to greet users when they join a channel.
Features flood protection and timeout between greetings.
"""
def __init__(self, irc):
self.__parent = super(Greet, self)
self.__parent.__init__(irc)
# Dictionary to store last greeting times per user
self.lastGreetTime = {}
# Default cooldown between greetings for the same user (in seconds)
self.userCooldown = 300 # 5 minutes
# Debug mode
self.debugMode = False
@ -41,30 +41,30 @@ class Greet(callbacks.Plugin):
"""
channel = msg.args[0]
nick = msg.nick
# Log event for debugging
if self.debugMode:
self._logDebug(irc, f"JOIN event detected: {nick} joined {channel}")
# Don't greet ourselves
if nick == irc.nick:
if self.debugMode:
self._logDebug(irc, f"Not greeting self (bot)")
return
# Check if greetings are enabled for this channel
if not self.registryValue('enabled', channel):
if self.debugMode:
self._logDebug(irc, f"Greetings disabled for {channel}")
return
# Get the greeting message for this channel
greeting = self.registryValue('message', channel)
if not greeting:
if self.debugMode:
self._logDebug(irc, f"No greeting message set for {channel}")
return
# Check if this user was greeted recently
userKey = f"{nick.lower()}@{channel}"
now = time.time()
@ -74,16 +74,16 @@ class Greet(callbacks.Plugin):
if self.debugMode:
self._logDebug(irc, f"User {nick} was greeted too recently")
return
# Get the timeout
timeout = self.registryValue('timeout', channel) or 3
if self.debugMode:
self._logDebug(irc, f"Will greet in {channel} after {timeout}s: {greeting}")
# Update last greet time for this user
self.lastGreetTime[userKey] = now
# Use a closure to capture the current values
def sendGreeting(channelToGreet=channel, greetingMsg=greeting):
if self.debugMode:
@ -95,10 +95,10 @@ class Greet(callbacks.Plugin):
except Exception as e:
if self.debugMode:
self._logDebug(irc, f"Error sending greeting: {str(e)}")
# Schedule the greeting
schedule.addEvent(sendGreeting, time.time() + timeout)
def _logDebug(self, irc, message):
"""Send debug messages to the first available channel"""
try:
@ -106,7 +106,7 @@ class Greet(callbacks.Plugin):
for owner in self.registryValue('owners') or []:
irc.queueMsg(ircmsgs.privmsg(owner, f"[GREET-DEBUG] {message}"))
return
# Fallback: send to the first available channel
for channel in list(irc.state.channels):
try:
@ -121,7 +121,7 @@ class Greet(callbacks.Plugin):
@wrap(['channel'])
def greettest(self, irc, msg, args, channel):
"""[<channel>]
Test the greeting by sending it immediately.
"""
greeting = self.registryValue('message', channel)
@ -134,29 +134,29 @@ class Greet(callbacks.Plugin):
@wrap(['owner', 'channel'])
def greettrigger(self, irc, msg, args, channel):
"""[<channel>]
Simulates a join event with timeout (owner only).
"""
greeting = self.registryValue('message', channel)
if not greeting:
irc.error(f"No greeting message is set for {channel}")
return
# Get the timeout
timeout = self.registryValue('timeout', channel) or 3
irc.replySuccess(f"Simulating join event with {timeout}s delay")
# Schedule the greeting
def sendGreeting():
irc.queueMsg(ircmsgs.privmsg(channel, greeting))
schedule.addEvent(sendGreeting, time.time() + timeout)
@wrap(['owner', 'boolean'])
def greetdebug(self, irc, msg, args, enable):
"""<on|off>
Enables or disables debug mode (owner only).
"""
self.debugMode = enable
@ -164,11 +164,11 @@ class Greet(callbacks.Plugin):
irc.replySuccess("Debug mode enabled")
else:
irc.replySuccess("Debug mode disabled")
@wrap(['channel'])
def greeton(self, irc, msg, args, channel):
"""[<channel>]
Enables greetings for a channel.
"""
self.setRegistryValue('enabled', True, channel)
@ -181,7 +181,7 @@ class Greet(callbacks.Plugin):
@wrap(['channel'])
def greetoff(self, irc, msg, args, channel):
"""[<channel>]
Disables greetings for a channel.
"""
self.setRegistryValue('enabled', False, channel)
@ -190,34 +190,34 @@ class Greet(callbacks.Plugin):
@wrap(['channel'])
def greetclear(self, irc, msg, args, channel):
"""[<channel>]
Removes the greeting message for a channel.
"""
self.setRegistryValue('message', '', channel)
self.setRegistryValue('enabled', False, channel)
irc.replySuccess(f"Greeting message cleared and disabled for {channel}.")
@wrap(['channel'])
def greetstatus(self, irc, msg, args, channel):
"""[<channel>]
Shows the current greeting status and message for a channel.
"""
enabled = self.registryValue('enabled', channel)
message = self.registryValue('message', channel)
timeout = self.registryValue('timeout', channel) or 3
if enabled and message:
irc.reply(f"Greetings are enabled for {channel}. Timeout: {timeout} seconds. Message: \"{message}\"")
elif enabled and not message:
irc.reply(f"Greetings are enabled for {channel}, but no message is set.")
else:
irc.reply(f"Greetings are disabled for {channel}. Message: \"{message or 'None'}\"")
@wrap(['channel', 'positiveInt'])
def greettimeout(self, irc, msg, args, channel, seconds):
"""[<channel>] <seconds>
Sets the timeout in seconds between joins before sending a greeting.
Default is 3 seconds.
"""
@ -227,11 +227,11 @@ class Greet(callbacks.Plugin):
@wrap(['channel', 'text'])
def greet(self, irc, msg, args, channel, text):
"""[<channel>] <message>
Sets the greeting message for a channel.
The greeting will be sent when users join the channel, with flood
protection.
To manage greetings, use the other commands:
greeton - Enables greetings for the channel
greetoff - Disables greetings for the channel
@ -241,7 +241,7 @@ protection.
greettrigger - Simulates a join event with timeout (owner only)
greetstatus - Shows current status and message
greettimeout <seconds> - Sets delay before greeting
Examples:
greet #channel Welcome to our channel!
"""

View File

@ -8,7 +8,7 @@ from supybot.test import *
class GreetTestCase(PluginTestCase):
plugins = ('Greet',)
def testGreet(self):
# Set a greeting message
self.assertNotError('greet #test Hello, world!')
@ -26,13 +26,13 @@ class GreetTestCase(PluginTestCase):
self.assertNotError('greetclear #test')
# Check if it's cleared and disabled
self.assertRegexp('greetstatus #test', 'disabled')
def testTimeout(self):
# Set a timeout
self.assertNotError('greettimeout #test 5')
# Check if it's set
self.assertRegexp('greetstatus #test', '5 seconds')
def testTestAndTrigger(self):
# Set a greeting message
self.assertNotError('greet #test Hello, world!')