Refactored to include auto-join channels in config file, now using \r\n message splitting
This commit is contained in:
parent
b56de77a3f
commit
57419aa78a
35
app.py
35
app.py
@ -15,27 +15,9 @@ parser.add_argument(
|
||||
default="settings.json",
|
||||
help="Load config file"
|
||||
)
|
||||
parser.add_argument(
|
||||
"-d",
|
||||
"--debug",
|
||||
dest="debug",
|
||||
action="store_true",
|
||||
help="Runs in debug mode"
|
||||
)
|
||||
arguments = parser.parse_args()
|
||||
|
||||
debug = arguments.debug
|
||||
kingme = [] if debug else ["#chaos"]
|
||||
channels = ["#bots", "#insane"]
|
||||
if not debug:
|
||||
channels.extend([
|
||||
"#meta",
|
||||
"#team",
|
||||
"#chaos",
|
||||
"#tildeverse"
|
||||
])
|
||||
|
||||
bot = Bot("127.0.0.1", 6667, channels)
|
||||
bot = Bot("127.0.0.1", 6667)
|
||||
responses = Responses(bot)
|
||||
tasks = Tasks(bot)
|
||||
|
||||
@ -69,6 +51,7 @@ def handle_mode(channel, mode):
|
||||
|
||||
def handle_invite(channel, name):
|
||||
changed = False
|
||||
kingme = bot.settings.get("extras", dict()).get("kingme", [])
|
||||
|
||||
if channel in kingme:
|
||||
try_to_king_me(channel)
|
||||
@ -102,6 +85,19 @@ def handle_message(name, source, response):
|
||||
if response == "!debug":
|
||||
print("::", bot.memories)
|
||||
|
||||
def handle_crashed():
|
||||
bot.crashed = False
|
||||
bot.stop()
|
||||
bot.tasks = tasks
|
||||
bot.start(arguments.config, dirname(realpath(__file__)), {
|
||||
"pm": handle_pm,
|
||||
"mode": handle_mode,
|
||||
"invite": handle_invite,
|
||||
"kick": handle_kick,
|
||||
"crashed": handle_crashed,
|
||||
"message": handle_message
|
||||
})
|
||||
|
||||
if __name__ == "__main__":
|
||||
bot.tasks = tasks
|
||||
bot.start(arguments.config, dirname(realpath(__file__)), {
|
||||
@ -109,5 +105,6 @@ if __name__ == "__main__":
|
||||
"mode": handle_mode,
|
||||
"invite": handle_invite,
|
||||
"kick": handle_kick,
|
||||
"crashed": handle_crashed,
|
||||
"message": handle_message
|
||||
})
|
37
bot/core.py
37
bot/core.py
@ -13,13 +13,14 @@ logging.basicConfig(
|
||||
)
|
||||
|
||||
class Bot:
|
||||
def __init__(self, server, port, channels):
|
||||
def __init__(self, server, port):
|
||||
self.ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self.logger = logging.getLogger("")
|
||||
self.server = server
|
||||
self.port = port
|
||||
self.channels = channels
|
||||
self.channels = []
|
||||
self.running = True
|
||||
self.crashed = False
|
||||
|
||||
self.settings = dict()
|
||||
self.places = list()
|
||||
@ -28,7 +29,7 @@ class Bot:
|
||||
self.botnick = ""
|
||||
|
||||
self.recv_size = 2048
|
||||
self.splitter = "\n\r"
|
||||
self.splitter = "\r\n"
|
||||
|
||||
def send(self, message, *args):
|
||||
response = message.format(*args) + "\n"
|
||||
@ -66,7 +67,7 @@ class Bot:
|
||||
magic_string = "End of /NAMES list."
|
||||
while magic_string not in message:
|
||||
message = self.ircsock.recv(self.recv_size).decode()
|
||||
message = message.strip(self.splitter)
|
||||
# message = message.strip(self.splitter)
|
||||
print(message)
|
||||
self.logger.debug(message)
|
||||
|
||||
@ -76,8 +77,8 @@ class Bot:
|
||||
print("DEBUG: Skipping adding users from {}".format(chan))
|
||||
return
|
||||
|
||||
raw_users = user_listing[1].split(" \r\n")[0].split(" ")
|
||||
# prefix_filter = lambda u: u[1:] if "~" in u or "@" in u or "+" in u else u
|
||||
splitter = " {}".format(self.splitter)
|
||||
raw_users = user_listing[1].split(splitter)[0].split(" ")
|
||||
users = list(filter(self.parse_name, raw_users))
|
||||
remember = self.memories["users"]
|
||||
for user in users:
|
||||
@ -181,7 +182,8 @@ class Bot:
|
||||
def load_settings(self, location):
|
||||
set_vars = [
|
||||
"author",
|
||||
"botnick"
|
||||
"botnick",
|
||||
"channels"
|
||||
]
|
||||
|
||||
path = "{}/{}".format(self.location, location)
|
||||
@ -231,7 +233,6 @@ class Bot:
|
||||
while magic_string not in message:
|
||||
message = self.ircsock.recv(self.recv_size).decode()
|
||||
message = message.strip(self.splitter)
|
||||
print(message)
|
||||
self.logger.debug(message)
|
||||
if not registered and magic_phrase["has_registered"] in message:
|
||||
registered = True
|
||||
@ -254,16 +255,20 @@ class Bot:
|
||||
if getattr(self.tasks, "run", None) is not None:
|
||||
self.tasks.run()
|
||||
|
||||
while self.running:
|
||||
message = self.ircsock.recv(self.recv_size).decode()
|
||||
while self.running or not self.crashed:
|
||||
message = ""
|
||||
while self.splitter not in message:
|
||||
message = self.ircsock.recv(self.recv_size).decode()
|
||||
|
||||
message = message.strip(self.splitter)
|
||||
print(message)
|
||||
self.logger.debug("{}".format(message))
|
||||
|
||||
if "new_nick' commands." in message:
|
||||
if "ERROR" in message or ":Closing link:" in message:
|
||||
self.logger.warning(message)
|
||||
self.send_message(self.author, "ERROR: {}".format(message))
|
||||
|
||||
self.logger.debug(message)
|
||||
self.crashed = True
|
||||
if "crashed" in callback:
|
||||
callback["crashed"]()
|
||||
break
|
||||
|
||||
if "raw" in callback:
|
||||
callback["raw"](message)
|
||||
@ -303,4 +308,4 @@ class Bot:
|
||||
elif "message" in callback:
|
||||
callback["message"](name, source, response)
|
||||
elif "unhandled" in callback:
|
||||
callback["unhandled"](message)
|
||||
callback["unhandled"](message)
|
@ -4,5 +4,6 @@
|
||||
"email": "",
|
||||
"confirm": "",
|
||||
"author": "",
|
||||
"channels": [],
|
||||
"extras": {}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user