bunch of refactoring i guess

This commit is contained in:
= 2025-04-04 06:54:31 +00:00
parent ea79b23b28
commit 5f0c0b2182
2 changed files with 165 additions and 172 deletions

123
bot.py
View File

@ -3,8 +3,8 @@ from time import sleep, time
from json import load, dump
import re
channel_re = re.compile(r"PRIVMSG (#*\w+)")
name_re = re.compile(r"^:([^!]*)!")
privmsg_channel_re = re.compile(r"PRIVMSG (#*\w+)")
nick_re = re.compile(r"^:([^!]*)!")
# timeout = 86400 # 24 hours
timeout = 15
@ -18,55 +18,52 @@ class IRCBot():
def __init__(self):
try:
with open("config.json", "r") as f:
self.config = load(f)
self.state = load(f)
except FileNotFoundError:
exit("no config.json")
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.connect((host, port))
self.nick = self.config["nick"]
self.sendline(f"NICK {self.nick}")
self.sendline(f"USER {self.nick} 0 * :{self.config['realname']}")
for channel in self.config["channels"]:
self.sendline(f"JOIN {channel}")
self.nick = self.state["nick"]
self.send_raw_line(f"NICK {self.nick}")
self.send_raw_line(f"USER {self.nick} 0 * :{self.state['realname']}")
for channel in self.state["channels"]:
self.send_raw_line(f"JOIN {channel}")
self.commands = [
("invite", self.invite),
("kick", self.kick)
]
def write_config(self):
with open("config.json", "w") as f:
dump(self.config, f, indent=2)
def write_state(self):
with open("state.json", "w") as f:
dump(self.state, f, indent=2)
def join_channel(self, channel):
self.sendline(f"JOIN {channel}")
self.config["channels"].append(channel)
self.write_config()
self.send_raw_line(f"JOIN {channel}")
self.state["channels"].append(channel)
self.write_state()
def part_channel(self, channel):
if channel == "#tildetown":
self.sendline("i will not leave #tildetown. want to block me? see https://git.tilde.town/nebula/chatterbot")
if channel in ("#tildetown", "#bots"):
self.send_raw_line(f"i will not leave {channel}. want to block me? see https://git.tilde.town/nebula/chatterbot")
return
elif channel == "#bots":
self.sendline("i will not leave #bots. want to block me? see https://git.tilde.town/nebula/chatterbot")
return
self.sendline(f"PART {channel}")
self.config["channels"].remove(channel)
del self.config["times"][channel]
del self.config["counts"][channel]
self.write_config()
self.send_raw_line(f"PART {channel}")
self.state["channels"].remove(channel)
del self.state["times"][channel]
del self.state["counts"][channel]
self.write_state()
def sendline(self, line):
def send_raw_line(self, line):
if line:
return self.s.send(bytes(f"{line}\r\n", "UTF-8"))
return None
return
def send(self, channel, content):
if isinstance(content, list):
for line in content:
self.sendline(f"PRIVMSG {channel} :{line}")
self.send_raw_line(f"PRIVMSG {channel} :{line}")
sleep(0.5)
elif isinstance(content, str):
self.sendline(f"PRIVMSG {channel} :{content}")
self.send_raw_line(f"PRIVMSG {channel} :{content}")
def help(_, __):
return helptext
@ -79,7 +76,7 @@ class IRCBot():
if not channel.startswith("#"):
lines.append("channel name must start with #")
continue
elif channel in self.config["channels"]:
elif channel in self.state["channels"]:
lines.append(f"i am already in {channel}!")
else:
self.join_channel(channel)
@ -89,33 +86,33 @@ class IRCBot():
def kick(self, channel, arguments):
if not arguments:
self.part_channel(channel)
return None
return
for channel in arguments:
self.part_channel(channel)
def check_time(self, channel):
try:
this_time = self.config["times"][channel]
this_time = self.state["times"][channel]
except KeyError:
this_time = self.config["times"][channel] = time()
self.write_config()
this_time = self.state["times"][channel] = time()
self.write_state()
return this_time
def set_time(self, channel, this_time):
self.config["times"][channel] = this_time
self.state["times"][channel] = this_time
def counter(self, channel):
try:
self.config["counts"][channel] += 1
value = self.config["counts"][channel]
self.state["counts"][channel] += 1
value = self.state["counts"][channel]
except KeyError:
value = self.config["counts"][channel] = 1
self.write_config()
value = self.state["counts"][channel] = 1
self.write_state()
return value
def reset_count(self, channel):
self.config["counts"][channel] = 0
self.write_config()
self.state["counts"][channel] = 0
self.write_state()
def command_loop(self):
while True:
@ -130,25 +127,25 @@ class IRCBot():
line = line.decode("UTF-8").strip()
if line.startswith("PING"):
pong = "PONG " + line[5:]
self.sendline(pong)
self.send_raw_line(pong)
continue
channel_search = channel_re.search(line)
if not channel_search:
privmsg_channel_search = privmsg_channel_re.search(line)
if not privmsg_channel_search:
continue
channel = channel_search.group(1)
name_search = name_re.search(line)
if name_search:
name = name_search.group(1)
channel = privmsg_channel_search.group(1)
nick_search = nick_re.search(line)
if nick_search:
nick = nick_search.group(1)
else:
name = None
if name and not channel.startswith("#"):
channel = name
nick = None
if nick and not channel.startswith("#"):
channel = nick
try:
message_body = line[line.index(" :") + 2:]
except (IndexError, ValueError):
message_body = ""
if message_body:
if message_body.startswith("!rollcall"):
if message_body.startswith("!rollcall") or message_body.startswith("!help"):
self.send(channel, helptext)
continue
elif message_body.startswith("!chatterbot"):
@ -164,16 +161,20 @@ class IRCBot():
else:
if channel in ("#tildetown", "#bots"):
continue
channel_time = self.check_time(channel)
now = time()
count = self.counter(channel)
delta = now - channel_time
if delta > timeout and count < messages_within_timeout:
self.reset_count(channel)
self.send("#bots", f"i hear activity in {channel}...")
elif count < messages_within_timeout and delta > timeout:
self.reset_count
self.set_time(channel, now)
# i have not figured out this part yet
# 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()

View File

@ -1,8 +0,0 @@
{
"nick": "chatterbot",
"realname": "a bot by ~nebula",
"channels": ["#bots"],
"times": {},
"counts": {}
}