diff --git a/bot.py b/bot.py index ff0b02b..88f4e57 100644 --- a/bot.py +++ b/bot.py @@ -6,10 +6,7 @@ import re privmsg_channel_re = re.compile(r"PRIVMSG (#*\w+)") nick_re = re.compile(r"^:([^!]*)!") -# timeout = 86400 # 24 hours -# these very low values are for testing -timeout = 15 -messages_within_timeout = 5 +default_timeout = 43200 # 12 hours in seconds host = "localhost" port = 6667 helptext = "i am a bot by ~nebula. i try to make it easier for users to discover new and more obscure channels on irc. instructions for use (or to block me from showing messages in your client) are available at https://git.tilde.town/nebula/chatterbot" @@ -66,9 +63,6 @@ class IRCBot(): elif isinstance(content, str): self.send_raw_line(f"PRIVMSG {channel} :{content}") - def help(_, __): - return helptext - def invite(self, _, arguments): if not arguments: return helptext_short @@ -102,18 +96,18 @@ class IRCBot(): def set_time(self, channel, this_time): self.state["times"][channel] = this_time - def counter(self, channel): + def set_timeout(self, channel, arguments): + timeout = int(timeout_hours * 60 * 60) + self.state["timeouts"][channel] = timeout + self.write_state() + return f"channel timeout set to {timeout_hours} hours" + + def get_timeout(self, channel): try: - self.state["counts"][channel] += 1 - value = self.state["counts"][channel] + self.state["timeouts"][channel] except KeyError: - value = self.state["counts"][channel] = 1 - self.write_state() - return value - - def reset_count(self, channel): - self.state["counts"][channel] = 0 - self.write_state() + self.state["timeouts"][channel] = default_timeout + self.write_state() def command_loop(self): while True: @@ -162,20 +156,14 @@ class IRCBot(): else: if channel in ("#tildetown", "#bots"): continue - # i have not figured out this part yet + channel_time = self.check_time(channel) + now = time() + channel_timeout = self.get_timeout(channel) + delta = now - channel_time + if delta > channel_timeout: + self.send("#bots", f"i hear activity in {channel}...") + self.set_time(channel, now) - # channel_time = self.check_time(channel) - # now = time() - # count = self.counter(channel) - # delta = now - channel_time - # if delta > timeout: - # if count < messages_within_timeout: - # self.send("#bots", f"i hear activity in {channel}...") - # self.reset_count(channel) - - # elif and channel_time : - # self.reset_count - # self.set_time(channel, now) if __name__ == "__main__": bot = IRCBot() diff --git a/state.json.example b/state.json.example index 775ad11..ea21244 100644 --- a/state.json.example +++ b/state.json.example @@ -3,6 +3,7 @@ "realname": "a bot by ~nebula", "channels": ["#bots"], "times": {}, - "counts": {} + "counts": {}, + "timeouts": {} } \ No newline at end of file