@ -9,8 +9,8 @@ class Plugin:
def __init__ ( self , bot ) :
self . bot = bot
self . name = self . __class__ . __name__ . lower ( ) [ : - 6 ] # Remove "Plugin".
self . config = self . _ get_config( )
self . is_ready = self . _ check_config( )
self . config = self . _ _ get_config( )
self . is_ready = self . _ _ check_config( )
@property
def callbacks ( self ) :
@ -21,7 +21,7 @@ class Plugin:
if cb . startswith ( " on_ " ) and callable ( getattr ( self , cb ) )
}
def _ get_config( self ) :
def _ _ get_config( self ) :
""" Return the plugin section from the bot config, plus common values. """
plugins_configs = self . bot . config [ " plugins " ]
config = plugins_configs [ " common " ] . copy ( )
@ -37,7 +37,7 @@ class Plugin:
self . bot . log_e ( f " Could not load resource at { resource_path } . " )
return config
def _ check_config( self ) :
def _ _ check_config( self ) :
""" Return True if the plugin config is properly setup. """
missing = False
for key in self . REQUIRED_CONFIGS :
@ -88,10 +88,16 @@ class Plugin:
def should_answer_question ( self , message ) :
""" Store Question in object and return True if it should answer it. """
words = message . split ( )
# Is the message addressed to me?
if words [ 0 ] . lower ( ) not in self . bot . names :
return False
question = message [ len ( words [ 0 ] ) : ] . strip ( )
# Are the config conditions to answer a question valid?
if not self . __respects_handling_conditions ( ) :
return False
# Is it a question I can answer?
question = message [ len ( words [ 0 ] ) : ] . strip ( )
for q in self . config . get ( " questions " , [ ] ) :
if question . startswith ( q ) :
self . question = Question ( q , question [ len ( q ) : ] . strip ( ) )
@ -101,9 +107,16 @@ class Plugin:
def should_handle_command ( self , message , no_content = False ) :
""" Store Command in object and return True if it should handle it. """
# Are the config conditions to handle a command valid?
if not self . __respects_handling_conditions ( ) :
return False
# Is it a valid command?
command = self . parse_command ( message , no_content = no_content )
if not command :
return False
# Is it a command I can handle?
commands = self . config . get ( " commands " , [ ] )
if (
any ( command . ident == c for c in commands ) or
@ -127,6 +140,19 @@ class Plugin:
content = " " . join ( words [ 2 : - 1 ] )
return Command ( ident , content )
def __respects_handling_conditions ( self ) :
""" Check if question conditions are valid. """
conditions = self . config . get ( " handling_conditions " , { } )
for plugin_ns , plugin_conditions in conditions . items ( ) :
for condition_key , condition_value in plugin_conditions . items ( ) :
value = self . get_runtime_value ( condition_key , ns = plugin_ns )
if condition_value != value :
self . bot . log_d (
f " Handling condition { plugin_ns } . { condition_key } false. "
)
return False
return True
def signal_failure ( self , target ) :
""" Signal a plugin failure to target. """
self . bot . say ( target , self . bot . config [ " error_message " ] )