bot: remember joined channels

This commit is contained in:
dece 2020-11-01 19:23:42 +01:00
parent 721d99dba3
commit ff88bfbb05

View file

@ -12,6 +12,8 @@ from edmond.log import Logger
class Bot(irc.client.SimpleIRCClient, Logger): class Bot(irc.client.SimpleIRCClient, Logger):
CHANNELS_RUNTIME_KEY = "_channels"
def __init__(self, config, logger): def __init__(self, config, logger):
super().__init__() super().__init__()
self.config = config self.config = config
@ -30,6 +32,13 @@ class Bot(irc.client.SimpleIRCClient, Logger):
def names(self): def names(self):
return (self.nick, *self.config["alternative_nicks"]) return (self.nick, *self.config["alternative_nicks"])
@property
def channels(self):
"""List of joined channels."""
if self.CHANNELS_RUNTIME_KEY not in self.values:
self.values[self.CHANNELS_RUNTIME_KEY] = []
return self.values[self.CHANNELS_RUNTIME_KEY]
def _get_storage(self): def _get_storage(self):
"""Load data from storage.""" """Load data from storage."""
try: try:
@ -61,11 +70,13 @@ class Bot(irc.client.SimpleIRCClient, Logger):
def on_join(self, connection, event): def on_join(self, connection, event):
if event.source.nick == self.nick: if event.source.nick == self.nick:
self.log_i(f"Joined {event.target}.") self.log_i(f"Joined {event.target}.")
self.channels.append(event.target)
self.run_plugin_callbacks(event) self.run_plugin_callbacks(event)
def on_part(self, connection, event): def on_part(self, connection, event):
if event.source.nick == self.nick: if event.source.nick == self.nick:
self.log_i(f"Left {event.target} (args: {event.arguments[0]}).") self.log_i(f"Left {event.target} (args: {event.arguments[0]}).")
self.channels.remove(event.target)
self.run_plugin_callbacks(event) self.run_plugin_callbacks(event)
def on_pubmsg(self, connection, event): def on_pubmsg(self, connection, event):