reset line indentation back to 4
This commit is contained in:
parent
ad7164edb7
commit
0d4b658d1c
300
bot.py
300
bot.py
@ -15,172 +15,172 @@ helptext = "i am a bot by ~nebula. i try to make it easier for users to discover
|
|||||||
helptext_short = "see https://git.tilde.town/nebula/chatterbot for instructions"
|
helptext_short = "see https://git.tilde.town/nebula/chatterbot for instructions"
|
||||||
|
|
||||||
class IRCBot():
|
class IRCBot():
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
try:
|
try:
|
||||||
with open("config.json", "r") as f:
|
with open("config.json", "r") as f:
|
||||||
self.state = load(f)
|
self.state = load(f)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
exit("no config.json")
|
exit("no config.json")
|
||||||
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
self.s.connect((host, port))
|
self.s.connect((host, port))
|
||||||
self.nick = self.state["nick"]
|
self.nick = self.state["nick"]
|
||||||
self.send_raw_line(f"NICK {self.nick}")
|
self.send_raw_line(f"NICK {self.nick}")
|
||||||
self.send_raw_line(f"USER {self.nick} 0 * :{self.state['realname']}")
|
self.send_raw_line(f"USER {self.nick} 0 * :{self.state['realname']}")
|
||||||
for channel in self.state["channels"]:
|
for channel in self.state["channels"]:
|
||||||
self.send_raw_line(f"JOIN {channel}")
|
self.send_raw_line(f"JOIN {channel}")
|
||||||
self.commands = [
|
self.commands = [
|
||||||
("invite", self.invite),
|
("invite", self.invite),
|
||||||
("kick", self.kick)
|
("kick", self.kick)
|
||||||
]
|
]
|
||||||
|
|
||||||
def write_state(self):
|
def write_state(self):
|
||||||
with open("state.json", "w") as f:
|
with open("state.json", "w") as f:
|
||||||
dump(self.state, f, indent=2)
|
dump(self.state, f, indent=2)
|
||||||
|
|
||||||
def join_channel(self, channel):
|
def join_channel(self, channel):
|
||||||
self.send_raw_line(f"JOIN {channel}")
|
self.send_raw_line(f"JOIN {channel}")
|
||||||
self.state["channels"].append(channel)
|
self.state["channels"].append(channel)
|
||||||
self.write_state()
|
self.write_state()
|
||||||
|
|
||||||
def part_channel(self, channel):
|
def part_channel(self, channel):
|
||||||
if channel in ("#tildetown", "#bots"):
|
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")
|
self.send_raw_line(f"i will not leave {channel}. want to block me? see https://git.tilde.town/nebula/chatterbot")
|
||||||
return
|
return
|
||||||
self.send_raw_line(f"PART {channel}")
|
self.send_raw_line(f"PART {channel}")
|
||||||
self.state["channels"].remove(channel)
|
self.state["channels"].remove(channel)
|
||||||
del self.state["times"][channel]
|
del self.state["times"][channel]
|
||||||
del self.state["counts"][channel]
|
del self.state["counts"][channel]
|
||||||
self.write_state()
|
self.write_state()
|
||||||
|
|
||||||
def send_raw_line(self, line):
|
def send_raw_line(self, line):
|
||||||
if line:
|
if line:
|
||||||
return self.s.send(bytes(f"{line}\r\n", "UTF-8"))
|
return self.s.send(bytes(f"{line}\r\n", "UTF-8"))
|
||||||
return
|
return
|
||||||
|
|
||||||
def send(self, channel, content):
|
def send(self, channel, content):
|
||||||
if isinstance(content, list):
|
if isinstance(content, list):
|
||||||
for line in content:
|
for line in content:
|
||||||
self.send_raw_line(f"PRIVMSG {channel} :{line}")
|
self.send_raw_line(f"PRIVMSG {channel} :{line}")
|
||||||
sleep(0.5)
|
sleep(0.5)
|
||||||
elif isinstance(content, str):
|
elif isinstance(content, str):
|
||||||
self.send_raw_line(f"PRIVMSG {channel} :{content}")
|
self.send_raw_line(f"PRIVMSG {channel} :{content}")
|
||||||
|
|
||||||
def help(_, __):
|
def help(_, __):
|
||||||
return helptext
|
return helptext
|
||||||
|
|
||||||
def invite(self, _, arguments):
|
def invite(self, _, arguments):
|
||||||
if not arguments:
|
if not arguments:
|
||||||
return helptext_short
|
return helptext_short
|
||||||
lines = []
|
lines = []
|
||||||
for channel in arguments:
|
for channel in arguments:
|
||||||
if not channel.startswith("#"):
|
if not channel.startswith("#"):
|
||||||
lines.append("channel name must start with #")
|
lines.append("channel name must start with #")
|
||||||
continue
|
continue
|
||||||
elif channel in self.state["channels"]:
|
elif channel in self.state["channels"]:
|
||||||
lines.append(f"i am already in {channel}!")
|
lines.append(f"i am already in {channel}!")
|
||||||
else:
|
else:
|
||||||
self.join_channel(channel)
|
self.join_channel(channel)
|
||||||
lines.append(f"i have (allegedly) joined {channel}")
|
lines.append(f"i have (allegedly) joined {channel}")
|
||||||
return lines
|
return lines
|
||||||
|
|
||||||
def kick(self, channel, arguments):
|
def kick(self, channel, arguments):
|
||||||
if not arguments:
|
if not arguments:
|
||||||
self.part_channel(channel)
|
self.part_channel(channel)
|
||||||
return
|
return
|
||||||
for channel in arguments:
|
for channel in arguments:
|
||||||
self.part_channel(channel)
|
self.part_channel(channel)
|
||||||
|
|
||||||
def check_time(self, channel):
|
def check_time(self, channel):
|
||||||
try:
|
try:
|
||||||
this_time = self.state["times"][channel]
|
this_time = self.state["times"][channel]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
this_time = self.state["times"][channel] = time()
|
this_time = self.state["times"][channel] = time()
|
||||||
self.write_state()
|
self.write_state()
|
||||||
return this_time
|
return this_time
|
||||||
|
|
||||||
def set_time(self, channel, this_time):
|
def set_time(self, channel, this_time):
|
||||||
self.state["times"][channel] = this_time
|
self.state["times"][channel] = this_time
|
||||||
|
|
||||||
def counter(self, channel):
|
def counter(self, channel):
|
||||||
try:
|
try:
|
||||||
self.state["counts"][channel] += 1
|
self.state["counts"][channel] += 1
|
||||||
value = self.state["counts"][channel]
|
value = self.state["counts"][channel]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
value = self.state["counts"][channel] = 1
|
value = self.state["counts"][channel] = 1
|
||||||
self.write_state()
|
self.write_state()
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def reset_count(self, channel):
|
def reset_count(self, channel):
|
||||||
self.state["counts"][channel] = 0
|
self.state["counts"][channel] = 0
|
||||||
self.write_state()
|
self.write_state()
|
||||||
|
|
||||||
def command_loop(self):
|
def command_loop(self):
|
||||||
while True:
|
while True:
|
||||||
char = self.s.recv(1)
|
char = self.s.recv(1)
|
||||||
if not char:
|
if not char:
|
||||||
exit(f"{self.nick}: no response from IRC server")
|
exit(f"{self.nick}: no response from IRC server")
|
||||||
line = b""
|
line = b""
|
||||||
while char != b"\n":
|
while char != b"\n":
|
||||||
if char != b"\r":
|
if char != b"\r":
|
||||||
line += char
|
line += char
|
||||||
char = self.s.recv(1)
|
char = self.s.recv(1)
|
||||||
line = line.decode("UTF-8").strip()
|
line = line.decode("UTF-8").strip()
|
||||||
if line.startswith("PING"):
|
if line.startswith("PING"):
|
||||||
pong = "PONG " + line[5:]
|
pong = "PONG " + line[5:]
|
||||||
self.send_raw_line(pong)
|
self.send_raw_line(pong)
|
||||||
continue
|
continue
|
||||||
privmsg_channel_search = privmsg_channel_re.search(line)
|
privmsg_channel_search = privmsg_channel_re.search(line)
|
||||||
if not privmsg_channel_search:
|
if not privmsg_channel_search:
|
||||||
continue
|
continue
|
||||||
channel = privmsg_channel_search.group(1)
|
channel = privmsg_channel_search.group(1)
|
||||||
nick_search = nick_re.search(line)
|
nick_search = nick_re.search(line)
|
||||||
if nick_search:
|
if nick_search:
|
||||||
nick = nick_search.group(1)
|
nick = nick_search.group(1)
|
||||||
else:
|
else:
|
||||||
nick = None
|
nick = None
|
||||||
if nick and not channel.startswith("#"):
|
if nick and not channel.startswith("#"):
|
||||||
channel = nick
|
channel = nick
|
||||||
try:
|
try:
|
||||||
message_body = line[line.index(" :") + 2:]
|
message_body = line[line.index(" :") + 2:]
|
||||||
except (IndexError, ValueError):
|
except (IndexError, ValueError):
|
||||||
message_body = ""
|
message_body = ""
|
||||||
if message_body:
|
if message_body:
|
||||||
if message_body.startswith("!rollcall") or message_body.startswith("!help"):
|
if message_body.startswith("!rollcall") or message_body.startswith("!help"):
|
||||||
self.send(channel, helptext)
|
self.send(channel, helptext)
|
||||||
continue
|
continue
|
||||||
elif message_body.startswith("!chatterbot"):
|
elif message_body.startswith("!chatterbot"):
|
||||||
arguments = message_body.strip().lower()[11:]
|
arguments = message_body.strip().lower()[11:]
|
||||||
if not arguments:
|
if not arguments:
|
||||||
self.send(channel, helptext)
|
self.send(channel, helptext)
|
||||||
continue
|
continue
|
||||||
arguments = arguments.split()
|
arguments = arguments.split()
|
||||||
for command, callback in self.commands:
|
for command, callback in self.commands:
|
||||||
if command not in arguments:
|
if command not in arguments:
|
||||||
continue
|
continue
|
||||||
self.send(channel, callback(channel, arguments[1:]))
|
self.send(channel, callback(channel, arguments[1:]))
|
||||||
else:
|
else:
|
||||||
if channel in ("#tildetown", "#bots"):
|
if channel in ("#tildetown", "#bots"):
|
||||||
continue
|
continue
|
||||||
# i have not figured out this part yet
|
# i have not figured out this part yet
|
||||||
|
|
||||||
# channel_time = self.check_time(channel)
|
# channel_time = self.check_time(channel)
|
||||||
# now = time()
|
# now = time()
|
||||||
# count = self.counter(channel)
|
# count = self.counter(channel)
|
||||||
# delta = now - channel_time
|
# delta = now - channel_time
|
||||||
# if delta > timeout:
|
# if delta > timeout:
|
||||||
# if count < messages_within_timeout:
|
# if count < messages_within_timeout:
|
||||||
# self.send("#bots", f"i hear activity in {channel}...")
|
# self.send("#bots", f"i hear activity in {channel}...")
|
||||||
# self.reset_count(channel)
|
# self.reset_count(channel)
|
||||||
|
|
||||||
# elif and channel_time :
|
# elif and channel_time :
|
||||||
# self.reset_count
|
# self.reset_count
|
||||||
# self.set_time(channel, now)
|
# self.set_time(channel, now)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
bot = IRCBot()
|
bot = IRCBot()
|
||||||
try:
|
try:
|
||||||
bot.command_loop()
|
bot.command_loop()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user