restructure bash + buxfixes
parent
292c6e80b0
commit
2fbc6754e9
286
modules/bash.py
286
modules/bash.py
|
@ -42,101 +42,74 @@ def setup(self):
|
||||||
# Start thread to check for new messages
|
# Start thread to check for new messages
|
||||||
Thread(target = insert_into_db_caller, args=(self,)).start()
|
Thread(target = insert_into_db_caller, args=(self,)).start()
|
||||||
|
|
||||||
def bash(phenny, input):
|
def insert_into_db(phenny, sqlite_data):
|
||||||
"""'.bash' - queries a quote selection to add"""
|
"""inserts message to to temp db"""
|
||||||
usage = "Usage: .bash <nick_1> <# msgs to start at> <nick_2> <# msgs to end at> (Specifies a range of messages)"
|
|
||||||
|
|
||||||
input_all = input.group(2)
|
if not bash.conn:
|
||||||
if not input_all:
|
bash.conn = sqlite3.connect(phenny.bash_db)
|
||||||
phenny.say(usage)
|
|
||||||
return
|
|
||||||
|
|
||||||
input_args = input_all.split()
|
c = bash.conn.cursor()
|
||||||
if len(input_args) < 4:
|
|
||||||
phenny.say(usage)
|
|
||||||
return
|
|
||||||
|
|
||||||
channel = input.sender
|
c.execute('''INSERT INTO quotes
|
||||||
nick1 = input_args[0]
|
(channel, nick, quote, time)
|
||||||
nick1_start_str = input_args[1]
|
VALUES(
|
||||||
nick2 = input_args[2]
|
:channel,
|
||||||
nick2_end_str = input_args[3]
|
:nick,
|
||||||
|
:quote,
|
||||||
|
CURRENT_TIMESTAMP
|
||||||
|
);''', sqlite_data)
|
||||||
|
|
||||||
# Check Input Type
|
c.execute('''INSERT OR REPLACE INTO stats
|
||||||
if not nick1_start_str.isdigit() or not nick2_end_str.isdigit():
|
(channel, nick, lines)
|
||||||
phenny.say("Error: 2nd & 4th argument must be integers")
|
VALUES(
|
||||||
phenny.say(usage)
|
:channel,
|
||||||
return
|
:nick,
|
||||||
|
COALESCE((SELECT lines FROM stats WHERE channel=:channel AND nick=:nick) + 1, 1)
|
||||||
|
);''', sqlite_data)
|
||||||
|
|
||||||
nick1_start = int(nick1_start_str)
|
|
||||||
nick2_end = int(nick2_end_str)
|
|
||||||
|
|
||||||
# Connect to db
|
c.close()
|
||||||
fn = phenny.nick + '-' + phenny.config.host + '.bash.db'
|
bash.conn.commit()
|
||||||
bash_db = os.path.join(os.path.expanduser('~/.phenny'), fn)
|
|
||||||
bash_conn = sqlite3.connect(bash_db)
|
|
||||||
c = bash_conn.cursor()
|
|
||||||
|
|
||||||
# Check input for validity
|
c = bash.conn.cursor()
|
||||||
c.execute('''SELECT lines FROM stats WHERE (channel=? OR channel='NULL') AND nick=?''', (channel, nick1))
|
|
||||||
|
c.execute('''SELECT id FROM quotes ORDER BY id DESC LIMIT 1''')
|
||||||
|
last_id = c.fetchall()[0][0] - 99
|
||||||
|
|
||||||
|
c.execute('''SELECT channel, nick FROM quotes WHERE id < ?''', (last_id,))
|
||||||
rows = c.fetchall()
|
rows = c.fetchall()
|
||||||
nick1_total = 0
|
|
||||||
for row in rows:
|
for row in rows:
|
||||||
nick1_total = nick1_total + row[0]
|
channel = row[0]
|
||||||
|
nick = row[1]
|
||||||
|
|
||||||
c.execute('''SELECT lines FROM stats WHERE (channel=? OR channel='NULL') AND nick=?''', (channel, nick2))
|
c.execute('''SELECT lines FROM stats WHERE channel=? AND nick=?''', (channel, nick))
|
||||||
rows = c.fetchall()
|
lines = 0
|
||||||
nick2_total = 0
|
lines = c.fetchall()[0][0]
|
||||||
for row in rows:
|
|
||||||
nick2_total = nick2_total + row[0]
|
|
||||||
|
|
||||||
if nick1_total < nick1_start:
|
if lines - 1 == 0:
|
||||||
phenny.say("Error: {} has not done {} actions (EVERYTHING included EXCEPT {} replies)" \
|
c.execute('''DELETE FROM stats WHERE channel=? AND nick=?''', (channel, nick))
|
||||||
.format(nick1, nick1_start, phenny.nick))
|
else:
|
||||||
return
|
c.execute('''REPLACE INTO stats
|
||||||
|
(channel, nick, lines)
|
||||||
|
VALUES(
|
||||||
|
?,
|
||||||
|
?,
|
||||||
|
(SELECT lines FROM stats WHERE channel=? AND nick=?) - 1
|
||||||
|
);''', (channel, nick, channel, nick))
|
||||||
|
|
||||||
if nick2_total < nick2_end:
|
c.execute('''DELETE FROM quotes WHERE id < ?''', (last_id,))
|
||||||
phenny.say("Error: {} has not done {} actions (EVERYTHING included EXCEPT {} replies)" \
|
|
||||||
.format(nick2, nick2_end, phenny.nick))
|
|
||||||
return
|
|
||||||
|
|
||||||
# Fetch quote ids
|
c.close()
|
||||||
c.execute('''SELECT id, channel, nick FROM quotes WHERE (channel=? OR channel='ALL') AND nick=?''', (channel, nick1))
|
bash.conn.commit()
|
||||||
rows = c.fetchall()
|
|
||||||
nick1_id = -1
|
|
||||||
for row, i in zip(reversed(rows), range(nick1_start)):
|
|
||||||
if i == nick1_start - 1:
|
|
||||||
nick1_id = row[0]
|
|
||||||
|
|
||||||
c.execute('''SELECT id, channel, nick FROM quotes WHERE (channel=? OR channel='ALL') AND nick=?''', (channel, nick2))
|
def insert_into_db_caller(phenny):
|
||||||
rows = c.fetchall()
|
"""Checks for a new quote once a second
|
||||||
nick2_id = -1
|
"""
|
||||||
for row, i in zip(reversed(rows), range(nick2_end)):
|
while True:
|
||||||
if i == nick2_end - 1:
|
global queue
|
||||||
nick2_id = row[0]
|
if len(queue) > 0:
|
||||||
|
insert_into_db(phenny, queue.pop(0))
|
||||||
if nick2_id < nick1_id:
|
time.sleep(1)
|
||||||
phenny.say("Error, try again. 2nd nick number (Newer) must come after 1st nick number")
|
|
||||||
phenny.say(usage)
|
|
||||||
return
|
|
||||||
|
|
||||||
# Fetch quotes within range of ids
|
|
||||||
c.execute('''SELECT quote FROM quotes WHERE (channel=? OR channel='ALL') AND id >= ? AND id <= ?''', (channel, nick1_id, nick2_id))
|
|
||||||
final_lines = []
|
|
||||||
for line in c.fetchall():
|
|
||||||
final_lines.append(line[0])
|
|
||||||
final_lines = ''.join(final_lines)
|
|
||||||
|
|
||||||
|
|
||||||
quote_json = {
|
|
||||||
'body': final_lines,
|
|
||||||
'tags': ','.join(['bone', nick1, nick2]),
|
|
||||||
'key': "SUPERSECRETAPIKEY"
|
|
||||||
}
|
|
||||||
|
|
||||||
web.post('https://bash.vtluug.org/quotes', {}, {}, True, json=quote_json)
|
|
||||||
|
|
||||||
phenny.say('Check https://bash.vtluug.org/quotes to see your quote!')
|
|
||||||
|
|
||||||
def logger(phenny, input):
|
def logger(phenny, input):
|
||||||
"""logs everyting to specific format, except we only keep last 100 lines
|
"""logs everyting to specific format, except we only keep last 100 lines
|
||||||
|
@ -147,7 +120,7 @@ def logger(phenny, input):
|
||||||
if input.event not in allowed_actions:
|
if input.event not in allowed_actions:
|
||||||
return
|
return
|
||||||
|
|
||||||
message=""
|
message=''
|
||||||
if input.event == 'PRIVMSG':
|
if input.event == 'PRIVMSG':
|
||||||
channel = input.sender
|
channel = input.sender
|
||||||
nick = input.nick
|
nick = input.nick
|
||||||
|
@ -176,7 +149,8 @@ def logger(phenny, input):
|
||||||
channel = input.sender
|
channel = input.sender
|
||||||
nick = input.nick
|
nick = input.nick
|
||||||
quote = "<-- {nick} has kicked {nick_kick} [{kick_msg}] from {channel}" \
|
quote = "<-- {nick} has kicked {nick_kick} [{kick_msg}] from {channel}" \
|
||||||
.format(nick=nick, nick_kick=input.args[1], kick_msg=input.group(1), channel=channel)
|
.format(nick=nick, nick_kick=input.args[1],
|
||||||
|
kick_msg=input.group(1), channel=channel)
|
||||||
elif input.event == 'NICK':
|
elif input.event == 'NICK':
|
||||||
channel = 'ALL'
|
channel = 'ALL'
|
||||||
nick = input.nick
|
nick = input.nick
|
||||||
|
@ -206,72 +180,112 @@ def logger(phenny, input):
|
||||||
global queue
|
global queue
|
||||||
queue.append(sqlite_data)
|
queue.append(sqlite_data)
|
||||||
|
|
||||||
def insert_into_db_caller(phenny):
|
def bash(phenny, input):
|
||||||
while True:
|
"""'.bash' - queries a quote selection to add"""
|
||||||
global queue
|
usage = ('Usage: .bash <nick_1> <# msgs to start at> <nick_2>'
|
||||||
if len(queue) > 0:
|
' <# msgs to end at> (Specifies a range of messages)')
|
||||||
insert_into_db(phenny, queue.pop(0))
|
|
||||||
time.sleep(1)
|
|
||||||
|
|
||||||
def insert_into_db(phenny, sqlite_data):
|
input_all = input.group(2)
|
||||||
"""inserts message to to temp db"""
|
if not input_all:
|
||||||
|
phenny.say(usage)
|
||||||
|
return
|
||||||
|
|
||||||
if not bash.conn:
|
input_args = input_all.split()
|
||||||
bash.conn = sqlite3.connect(phenny.bash_db)
|
if len(input_args) < 4:
|
||||||
|
phenny.say(usage)
|
||||||
|
return
|
||||||
|
|
||||||
c = bash.conn.cursor()
|
channel = input.sender
|
||||||
|
nick1 = input_args[0]
|
||||||
|
nick1_start_str = input_args[1]
|
||||||
|
nick2 = input_args[2]
|
||||||
|
nick2_end_str = input_args[3]
|
||||||
|
|
||||||
c.execute('''insert into quotes
|
# Check Input Type
|
||||||
(channel, nick, quote, time)
|
if not nick1_start_str.isdigit() or not nick2_end_str.isdigit():
|
||||||
values(
|
phenny.say('Error: 2nd & 4th argument must be integers')
|
||||||
:channel,
|
phenny.say(usage)
|
||||||
:nick,
|
return
|
||||||
:quote,
|
|
||||||
CURRENT_TIMESTAMP
|
|
||||||
);''', sqlite_data)
|
|
||||||
|
|
||||||
c.execute('''insert or replace into stats
|
nick1_start = int(nick1_start_str)
|
||||||
(channel, nick, lines)
|
nick2_end = int(nick2_end_str)
|
||||||
values(
|
|
||||||
:channel,
|
|
||||||
:nick,
|
|
||||||
coalesce((select lines from stats where channel=:channel and nick=:nick) + 1, 1)
|
|
||||||
);''', sqlite_data)
|
|
||||||
|
|
||||||
|
# Connect to db
|
||||||
|
fn = phenny.nick + '-' + phenny.config.host + '.bash.db'
|
||||||
|
bash_db = os.path.join(os.path.expanduser('~/.phenny'), fn)
|
||||||
|
bash_conn = sqlite3.connect(bash_db)
|
||||||
|
c = bash_conn.cursor()
|
||||||
|
|
||||||
c.close()
|
# Check input for validity
|
||||||
bash.conn.commit()
|
c.execute('''SELECT lines FROM stats
|
||||||
|
WHERE (channel=? OR channel='NULL')
|
||||||
c = bash.conn.cursor()
|
AND nick=?''', (channel, nick1))
|
||||||
|
|
||||||
c.execute('''select id from quotes order by id desc limit 1''')
|
|
||||||
last_id = c.fetchall()[0][0] - 99
|
|
||||||
|
|
||||||
c.execute('''select channel, nick from quotes where id < ?''', (last_id,))
|
|
||||||
rows = c.fetchall()
|
rows = c.fetchall()
|
||||||
|
nick1_total = 0
|
||||||
for row in rows:
|
for row in rows:
|
||||||
channel = row[0]
|
nick1_total = nick1_total + row[0]
|
||||||
nick = row[1]
|
|
||||||
|
|
||||||
c.execute('''select lines from stats where channel=? and nick=?''', (channel, nick))
|
c.execute('''SELECT lines FROM stats
|
||||||
lines = 0
|
WHERE (channel=? OR channel='NULL')
|
||||||
lines = c.fetchall()[0][0]
|
AND nick=?''', (channel, nick2))
|
||||||
|
rows = c.fetchall()
|
||||||
|
nick2_total = 0
|
||||||
|
for row in rows:
|
||||||
|
nick2_total = nick2_total + row[0]
|
||||||
|
|
||||||
if lines - 1 == 0:
|
if nick1_total < nick1_start:
|
||||||
c.execute('''delete from stats where channel=? and nick=?''', (channel, nick))
|
phenny.say("Error: {} has not done {} actions ({} replies not included)" \
|
||||||
else:
|
.format(nick1, nick1_start, phenny.nick))
|
||||||
c.execute('''replace into stats
|
return
|
||||||
(channel, nick, lines)
|
|
||||||
values(
|
|
||||||
?,
|
|
||||||
?,
|
|
||||||
(select lines from stats where channel=? and nick=?) - 1
|
|
||||||
);''', (channel, nick, channel, nick))
|
|
||||||
|
|
||||||
c.execute('''delete from quotes where id < ?''', (last_id,))
|
if nick2_total < nick2_end:
|
||||||
|
phenny.say("Error: {} has not done {} actions ({} replies not included)" \
|
||||||
|
.format(nick2, nick2_end, phenny.nick))
|
||||||
|
return
|
||||||
|
|
||||||
c.close()
|
# Fetch quote ids
|
||||||
bash.conn.commit()
|
c.execute('''SELECT id, channel, nick FROM quotes
|
||||||
|
WHERE (channel=? OR channel='ALL')
|
||||||
|
AND nick=?''', (channel, nick1))
|
||||||
|
rows = c.fetchall()
|
||||||
|
nick1_id = -1
|
||||||
|
for row, i in zip(reversed(rows), range(nick1_start)):
|
||||||
|
if i == nick1_start - 1:
|
||||||
|
nick1_id = row[0]
|
||||||
|
|
||||||
|
c.execute('''SELECT id, channel, nick FROM quotes
|
||||||
|
WHERE (channel=? OR channel='ALL')
|
||||||
|
AND nick=?''', (channel, nick2))
|
||||||
|
rows = c.fetchall()
|
||||||
|
nick2_id = -1
|
||||||
|
for row, i in zip(reversed(rows), range(nick2_end)):
|
||||||
|
if i == nick2_end - 1:
|
||||||
|
nick2_id = row[0]
|
||||||
|
|
||||||
|
if nick2_id < nick1_id:
|
||||||
|
phenny.say('Error, try again. 2nd message must occur after first message.')
|
||||||
|
phenny.say(usage)
|
||||||
|
return
|
||||||
|
|
||||||
|
# Fetch quotes within range of ids
|
||||||
|
c.execute('''SELECT quote FROM quotes
|
||||||
|
WHERE (channel=? OR channel='ALL')
|
||||||
|
AND id >= ? AND id <= ?''', (channel, nick1_id, nick2_id))
|
||||||
|
final_lines = []
|
||||||
|
for line in c.fetchall():
|
||||||
|
final_lines.append(line[0])
|
||||||
|
final_lines = ''.join(final_lines)
|
||||||
|
|
||||||
|
|
||||||
|
quote_json = {
|
||||||
|
'body': final_lines,
|
||||||
|
'tags': ','.join([phenny.nick, nick1, nick2]),
|
||||||
|
'key': phenny.config.bash_api_key
|
||||||
|
}
|
||||||
|
|
||||||
|
web.post('https://bash.vtluug.org/quotes', {}, {}, True, json=quote_json)
|
||||||
|
|
||||||
|
phenny.say('Check https://bash.vtluug.org/quotes to see your quote!')
|
||||||
|
|
||||||
bash.conn = None
|
bash.conn = None
|
||||||
bash.rule = (['bash', 'vtbash', 'quote'], r'(.*)')
|
bash.rule = (['bash', 'vtbash', 'quote'], r'(.*)')
|
||||||
|
|
6
phenny
6
phenny
|
@ -46,10 +46,14 @@ def create_default_config(fn):
|
||||||
# leave the api key blank to not use them and be sure to add the 'linx' module to the ignore list.
|
# leave the api key blank to not use them and be sure to add the 'linx' module to the ignore list.
|
||||||
linx_api_key = ""
|
linx_api_key = ""
|
||||||
|
|
||||||
|
# bash-enabled features
|
||||||
|
# designed to be used with https://github.com/vtluug/pyqdb
|
||||||
|
bash_api_key = ''
|
||||||
|
|
||||||
# These are people who will be able to use admin.py's functions...
|
# These are people who will be able to use admin.py's functions...
|
||||||
admins = [owner, 'someoneyoutrust']
|
admins = [owner, 'someoneyoutrust']
|
||||||
# But admin.py is disabled by default, as follows:
|
# But admin.py is disabled by default, as follows:
|
||||||
exclude = ['admin', 'linx', 'foodforus']
|
exclude = ['admin', 'linx', 'foodforus', 'bash']
|
||||||
|
|
||||||
ignore = ['']
|
ignore = ['']
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue