Added responses system, added actions system to react to response triggers
This commit is contained in:
parent
31e4d6da67
commit
27ac672d6d
14
actions/__init__.py
Normal file
14
actions/__init__.py
Normal file
@ -0,0 +1,14 @@
|
||||
from actions.botlist import botlist
|
||||
|
||||
actions = [
|
||||
{
|
||||
"type": "response",
|
||||
"pattern": "!botlist",
|
||||
"callback": botlist
|
||||
},
|
||||
{
|
||||
"type": "response",
|
||||
"pattern": "!rollcall",
|
||||
"callback": botlist
|
||||
}
|
||||
]
|
12
actions/botlist.py
Normal file
12
actions/botlist.py
Normal file
@ -0,0 +1,12 @@
|
||||
def botlist(self, name, source, response):
|
||||
name = self.bot.botnick
|
||||
author = self.bot.author
|
||||
email = self.bot.settings["email"]
|
||||
about = "the meta chat bot"
|
||||
commands = ", ".join([
|
||||
"!botlist",
|
||||
"!rollcall"
|
||||
])
|
||||
args = (name, author, email, about, commands)
|
||||
message = "{} | {} <{}> | {} | {}".format(*args)
|
||||
self.bot.send_message(source, message)
|
53
app.py
53
app.py
@ -1,25 +1,58 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from bot import Bot
|
||||
from bot import Bot, Tasks, Responses
|
||||
from actions import actions
|
||||
|
||||
kickers = list()
|
||||
inviters = dict()
|
||||
kingme = [
|
||||
"#chaos"
|
||||
]
|
||||
|
||||
bot = Bot("127.0.0.1", 6667, "BabiliBot|py", ["#bots"])
|
||||
responses = Responses(bot)
|
||||
|
||||
for action in actions:
|
||||
if "type" in action and "pattern" in action and "callback" in action:
|
||||
responses.add_trigger(
|
||||
action["type"],
|
||||
action["pattern"],
|
||||
action["callback"]
|
||||
)
|
||||
|
||||
def try_to_king_me(bot, channel):
|
||||
bot.send_message("ChanServ", "REGISTER {}", channel)
|
||||
bot.send_message("ChanServ", "SET Successor {} {}", channel, bot.botnick)
|
||||
bot.send_message("ChanServ", "SET Founder {} {}", channel, bot.author)
|
||||
|
||||
def handle_pm(name, response):
|
||||
print("PM: {} - {}".format(name, response))
|
||||
|
||||
def handle_message(name, source, response):
|
||||
#bot.send_message(source, "Got response")
|
||||
print("MSG: {} {} - {}".format(name, source, response))
|
||||
|
||||
def handle_mode(channel, mode):
|
||||
if mode == "-r":
|
||||
bot.send_message("ChanServ", "REGISTER {}", channel)
|
||||
bot.send_message("ChanServ", "SET Successor {} {}", chan, bot.botnick)
|
||||
bot.send_message("ChanServ", "SET Founder {} {}", chan, bot.author)
|
||||
try_to_king_me(bot, channel)
|
||||
|
||||
def handle_invite(channel, name):
|
||||
if channel in kingme:
|
||||
try_to_king_me(bot, channel)
|
||||
|
||||
invites.append({
|
||||
"channel": channel,
|
||||
"name": name
|
||||
})
|
||||
|
||||
def handle_kick(kicker):
|
||||
kickers.append(kicker)
|
||||
|
||||
def handle_message(name, source, response):
|
||||
print("MSG: {} {} - {}".format(name, source, response))
|
||||
responses.parse(name, source, response)
|
||||
|
||||
if __name__ == "__main__":
|
||||
bot.start("settings.json", {
|
||||
"pm": handle_pm,
|
||||
"message": handle_message,
|
||||
"mode": handle_mode
|
||||
"mode": handle_mode,
|
||||
"invite": handle_invite,
|
||||
"kick": handle_kick,
|
||||
"message": handle_message
|
||||
})
|
@ -1 +1,3 @@
|
||||
from bot.core import Bot
|
||||
from bot.core import Bot
|
||||
from bot.tasks import Tasks
|
||||
from bot.responses import Responses
|
30
bot/core.py
30
bot/core.py
@ -11,12 +11,9 @@ class Bot:
|
||||
self.channels = channels
|
||||
self.running = True
|
||||
|
||||
self.settings = dict()
|
||||
self.users = dict()
|
||||
self.kickers = list()
|
||||
self.invites = list()
|
||||
|
||||
self.author = ""
|
||||
self.kingme = []
|
||||
|
||||
self.recv_size = 2048
|
||||
self.splitter = "\n\r"
|
||||
@ -42,9 +39,6 @@ class Bot:
|
||||
message = message.strip(self.splitter)
|
||||
print(message)
|
||||
|
||||
if chan in self.kingme:
|
||||
self.try_to_king_me(chan)
|
||||
|
||||
user_list = "= {} :".format(chan)
|
||||
raw_users = message.split(user_list)[1].split(" \r\n")[0].split(" ")
|
||||
prefix_filter = lambda u: u[1:] if "~" in u or "@" in u else u
|
||||
@ -85,32 +79,21 @@ class Bot:
|
||||
name = self.get_name(before)
|
||||
channel = after.split(":", 1)[1]
|
||||
self.join(channel)
|
||||
self.invites.append({
|
||||
"name": name,
|
||||
"channel": channel
|
||||
})
|
||||
return channel, name
|
||||
|
||||
def handle_kick(self, message):
|
||||
regex = "KICK #\S+ {} :".format(self.botnick)
|
||||
before, kicker = re.split(regex, message)
|
||||
self.kickers.append(kicker)
|
||||
return kicker
|
||||
|
||||
def try_to_king_me(self, chan):
|
||||
self.send_message("ChanServ", "REGISTER {}", chan)
|
||||
self.send_message("ChanServ", "SET Successor {} {}", chan, self.botnick)
|
||||
self.send_message("ChanServ", "SET Founder {} {}", chan, self.author)
|
||||
|
||||
def load_settings(self, location):
|
||||
set_vars = [
|
||||
"author"
|
||||
]
|
||||
|
||||
with open(location, "r") as f:
|
||||
self.settings = json.loads(f.read())
|
||||
|
||||
set_vars = [
|
||||
"author",
|
||||
"kingme"
|
||||
]
|
||||
|
||||
for name, attr in self.settings.items():
|
||||
if name in set_vars:
|
||||
setattr(self, name, attr)
|
||||
@ -196,6 +179,5 @@ class Bot:
|
||||
callback["pm"](name, response)
|
||||
elif "message" in callback:
|
||||
callback["message"](name, source, response)
|
||||
else:
|
||||
if "unhandled" in callback:
|
||||
elif "unhandled" in callback:
|
||||
callback["unhandled"](message)
|
||||
|
30
bot/responses.py
Normal file
30
bot/responses.py
Normal file
@ -0,0 +1,30 @@
|
||||
import re
|
||||
|
||||
class Responses:
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
self.triggers = {
|
||||
"name": dict(),
|
||||
"source": dict(),
|
||||
"response": dict()
|
||||
}
|
||||
|
||||
def add_trigger(self, trigger_type, pattern, callback):
|
||||
if trigger_type in self.triggers:
|
||||
self.triggers[trigger_type][pattern] = callback
|
||||
|
||||
def parse(self, name, source, response):
|
||||
check = {
|
||||
"name": name,
|
||||
"source": source,
|
||||
"response": response
|
||||
}
|
||||
|
||||
for trigger in list(self.triggers.keys()):
|
||||
for pattern, callback in self.triggers[trigger].items():
|
||||
if pattern[0] == "!" and pattern == response:
|
||||
callback(self, name, source, response)
|
||||
else:
|
||||
regex = re.compile(pattern)
|
||||
if regex.match(check[trigger]) is not None:
|
||||
callback(self, name, source, response)
|
3
bot/tasks.py
Normal file
3
bot/tasks.py
Normal file
@ -0,0 +1,3 @@
|
||||
class Tasks:
|
||||
def __init__(self):
|
||||
pass
|
Loading…
Reference in New Issue
Block a user