Add commands support
This commit is contained in:
parent
bf661d9577
commit
529bcbb003
3 changed files with 59 additions and 1 deletions
13
Db.py
13
Db.py
|
@ -23,11 +23,16 @@ class LogMessage(BaseModel):
|
|||
message_type = CharField()
|
||||
message = CharField()
|
||||
|
||||
class Quote(BaseModel):
|
||||
datetime = DateTimeField()
|
||||
author = CharField()
|
||||
message = CharField()
|
||||
|
||||
|
||||
def create_tables():
|
||||
database.connect()
|
||||
try:
|
||||
database.create_tables([LogMessage, Day, Channel])
|
||||
database.create_tables([Quote, LogMessage, Day, Channel])
|
||||
except OperationalError:
|
||||
pass # Database already exists
|
||||
|
||||
|
@ -55,6 +60,12 @@ def add_log_message(channel, nickname, message_type, message = None):
|
|||
message_type = message_type,
|
||||
message = message)
|
||||
|
||||
def add_quote(author, message):
|
||||
Quote.create(
|
||||
author = author,
|
||||
message = message,
|
||||
datetime = datetime.datetime.now().strftime("%H:%m:%S"))
|
||||
|
||||
def show_all_messages():
|
||||
for message in LogMessage.select():
|
||||
print("<%s> %s" % (message.nickname, message.message))
|
||||
|
|
44
commands.py
Normal file
44
commands.py
Normal file
|
@ -0,0 +1,44 @@
|
|||
from Db import *
|
||||
from irclib import nm_to_n
|
||||
import random
|
||||
|
||||
class Commands:
|
||||
def __init__(self):
|
||||
self.commands = {
|
||||
"quote": self.cmd_quote,
|
||||
"remember_quote": self.cmd_remember_quote
|
||||
}
|
||||
|
||||
def process(self, c, e):
|
||||
msg = e.arguments()[0]
|
||||
if msg.startswith("!"):
|
||||
cmd = msg.split("!")[1].split(" ")[0]
|
||||
if cmd in self.commands:
|
||||
msg = " ".join(msg.split(" ")[1:])
|
||||
self.commands[cmd](c, msg, e.target(), nm_to_n(e.source()))
|
||||
else:
|
||||
print "Unknown command", cmd
|
||||
|
||||
def cmd_quote(self, c, msg, target, source):
|
||||
replies = [
|
||||
"%s once said \"%s\"",
|
||||
"I head from %s that \"%s\"",
|
||||
"A wise man (haha, just kidding, it was actually %s) once said \"%s\""
|
||||
]
|
||||
reply = lambda msg: c.privmsg(target, msg)
|
||||
random_query = Quote.select().order_by(fn.Random())
|
||||
try:
|
||||
one_quote = random_query.get()
|
||||
reply(random.choice(replies) % (one_quote.author, one_quote.message))
|
||||
except: # No quotes
|
||||
reply("I don't know ay quotes :(")
|
||||
|
||||
def cmd_remember_quote(self, c, msg, target, source):
|
||||
reply = lambda msg: c.privmsg(target, msg)
|
||||
|
||||
if (len(msg) > 1):
|
||||
add_quote(msg.split(" ")[0], ' '.join(msg.split(" ")[1:]))
|
||||
reply("I'll try to remember that!")
|
||||
else:
|
||||
reply("I didn't get that :(")
|
||||
|
|
@ -59,6 +59,7 @@ from pullrequest import PullRequest
|
|||
from Db import *
|
||||
from flask import *
|
||||
import threading
|
||||
from commands import Commands
|
||||
|
||||
pat1 = re.compile(r"(^|[\n ])(([\w]+?://[\w\#$%&~.\-;:=,?@\[\]+]*)(/[\w\#$%&~/.\-;:=,?@\[\]+]*)?)", re.IGNORECASE | re.DOTALL)
|
||||
|
||||
|
@ -203,6 +204,7 @@ class Logbot(SingleServerIRCBot):
|
|||
self.chans = [x.lower() for x in channels]
|
||||
self.set_ftp()
|
||||
self.nick_pass = nick_pass
|
||||
self.commands = Commands()
|
||||
|
||||
print("Logbot %s" % __version__)
|
||||
print("Connecting to %s:%i..." % (server, port))
|
||||
|
@ -320,6 +322,7 @@ class Logbot(SingleServerIRCBot):
|
|||
def on_pubmsg(self, c, e):
|
||||
# if e.arguments()[0].startswith(NICK):
|
||||
# c.privmsg(e.target(), self.format["help"])
|
||||
self.commands.process(c, e)
|
||||
self.write_event("pubmsg", e)
|
||||
|
||||
def on_pubnotice(self, c, e):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue