Add send_status, /me. Drop date from tables.
This commit is contained in:
parent
7fca63a2ab
commit
0b74dcabf0
39
bot.py
39
bot.py
|
@ -138,12 +138,38 @@ def add_imp(bot, update, player, type, qid):
|
|||
x.update_db()
|
||||
|
||||
text = "Quest Added!"
|
||||
bot.send_message(chat_id=chat_id, text=text)
|
||||
send_status(bot, update, player)
|
||||
|
||||
|
||||
def send_status(bot, update, player):
|
||||
name = str(update.message.from_user.first_name)
|
||||
if update.message.from_user.last_name:
|
||||
name += " " + str(update.message.from_user.last_name)
|
||||
points = player.get_points()
|
||||
total_quests = len(player.get_quests(None))
|
||||
completed_quests = len(player.get_quests(1))
|
||||
total_side_quests = len(player.get_side_quests(None))
|
||||
completed_side_quests = len(player.get_side_quests(1))
|
||||
|
||||
text = (f'<b>{name}</b>\n\n'
|
||||
f'🔥XP: {points}\n'
|
||||
f'⭐️Quests: {completed_quests}/{total_quests}\n'
|
||||
f'💠Side Quests: {completed_side_quests}/{total_side_quests}\n')
|
||||
chat_id = update.message.chat_id
|
||||
custom_keyboard = [
|
||||
['Add Quest', 'Add Side Quest'],
|
||||
['List Quests', 'List Side Quests']
|
||||
]
|
||||
reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard)
|
||||
bot.send_message(chat_id=chat_id, text=text, reply_markup=reply_markup)
|
||||
bot.send_message(chat_id=chat_id, text=text, reply_markup=reply_markup,
|
||||
parse_mode="HTML")
|
||||
|
||||
|
||||
def me_handler(bot, update, db):
|
||||
chat_id = update.message.chat_id
|
||||
player = questable.player(db, chat_id)
|
||||
send_status(bot, update, player)
|
||||
|
||||
|
||||
def message_handling(bot, update, db):
|
||||
|
@ -183,12 +209,12 @@ cursor = db.cursor()
|
|||
# Set up tables
|
||||
queries = [
|
||||
("CREATE TABLE IF NOT EXISTS quests(chat_id int NOT NULL, qid int NOT"
|
||||
" NULL, name varchar(255), difficulty int, importance int, date int"
|
||||
", state int DEFAULT 0, UNIQUE(chat_id, qid));"),
|
||||
" NULL, name varchar(255), difficulty int, importance int, "
|
||||
"state int DEFAULT 0, UNIQUE(chat_id, qid));"),
|
||||
|
||||
("CREATE TABLE IF NOT EXISTS side_quests(chat_id int NOT NULL, qid int "
|
||||
"NOT NULL, name varchar(255), difficulty int, importance int, date "
|
||||
"int, state int DEFAULT 0, UNIQUE(chat_id, qid));"),
|
||||
"NOT NULL, name varchar(255), difficulty int, importance int, "
|
||||
"state int DEFAULT 0, UNIQUE(chat_id, qid));"),
|
||||
|
||||
("CREATE TABLE IF NOT EXISTS points(chat_id int PRIMARY KEY, points "
|
||||
"int);"),
|
||||
|
@ -206,6 +232,9 @@ dispatcher = updater.dispatcher
|
|||
start_handler = CommandHandler('start', start)
|
||||
dispatcher.add_handler(start_handler)
|
||||
|
||||
start_handler = CommandHandler('me', lambda x, y: me_handler(x, y, db))
|
||||
dispatcher.add_handler(start_handler)
|
||||
|
||||
handler = MessageHandler(Filters.text, lambda x, y: message_handling(x, y, db))
|
||||
dispatcher.add_handler(handler)
|
||||
|
||||
|
|
36
questable.py
36
questable.py
|
@ -1,11 +1,8 @@
|
|||
from datetime import datetime
|
||||
|
||||
|
||||
class base_quest():
|
||||
TABLE = None
|
||||
|
||||
def __init__(self, db, chat_id, qid, name=None, imp=None, diff=None,
|
||||
state=None, date=None):
|
||||
state=None):
|
||||
self.DB = db
|
||||
self.CHAT_ID = chat_id
|
||||
self.name = name
|
||||
|
@ -13,37 +10,30 @@ class base_quest():
|
|||
self.imp = imp
|
||||
self.diff = diff
|
||||
self.state = state
|
||||
if date:
|
||||
if isinstance(date, datetime):
|
||||
self.date = date
|
||||
else:
|
||||
self.date = datetime.fromtimestamp(date)
|
||||
else:
|
||||
self.date = None
|
||||
|
||||
def add_to_db(self):
|
||||
cursor = self.DB.cursor()
|
||||
query = (f'INSERT INTO {self.TABLE}(chat_id, qid, name, importance'
|
||||
', difficulty, date, state) values(?, ?, ?, ?, ?, ?, ?)')
|
||||
', difficulty, state) values(?, ?, ?, ?, ?, ?)')
|
||||
cursor.execute(query, (self.CHAT_ID, self.QID, self.name, self.imp,
|
||||
self.diff, self.date, self.state))
|
||||
self.diff, self.state))
|
||||
self.DB.commit()
|
||||
|
||||
def update_db(self):
|
||||
cursor = self.DB.cursor()
|
||||
query = (f'UPDATE {self.TABLE} SET name=?, importance=?, difficulty=?,'
|
||||
' date=?, state=? WHERE chat_id=? AND qid=?')
|
||||
cursor.execute(query, (self.name, self.imp, self.diff, self.date,
|
||||
' state=? WHERE chat_id=? AND qid=?')
|
||||
cursor.execute(query, (self.name, self.imp, self.diff,
|
||||
self.state, self.CHAT_ID, self.QID))
|
||||
self.DB.commit()
|
||||
|
||||
def get_from_db(self):
|
||||
cursor = self.DB.cursor()
|
||||
query = (f'SELECT name, importance, difficulty, date, state FROM '
|
||||
query = (f'SELECT name, importance, difficulty, state FROM '
|
||||
f'{self.TABLE} where chat_id=? AND qid=?')
|
||||
cursor.execute(query, (self.CHAT_ID, self.QID))
|
||||
output = cursor.fetchone()
|
||||
self.name, self.imp, self.diff, self.date, self.state = output
|
||||
self.name, self.imp, self.diff, self.state = output
|
||||
|
||||
|
||||
class quest(base_quest):
|
||||
|
@ -61,8 +51,8 @@ def get_quest(db, chat_id, qid):
|
|||
|
||||
|
||||
def add_quest(db, chat_id, qid, name=None, imp=None, diff=None,
|
||||
state=None, date=None):
|
||||
q = quest(db, chat_id, qid, name, imp, diff, state, date)
|
||||
state=None):
|
||||
q = quest(db, chat_id, qid, name, imp, diff, state)
|
||||
q.add_to_db()
|
||||
return q
|
||||
|
||||
|
@ -74,8 +64,8 @@ def get_side_quest(db, chat_id, qid):
|
|||
|
||||
|
||||
def add_side_quest(db, chat_id, qid, name=None, imp=None, diff=None,
|
||||
state=None, date=None):
|
||||
q = side_quest(db, chat_id, qid, name, imp, diff, state, date)
|
||||
state=None):
|
||||
q = side_quest(db, chat_id, qid, name, imp, diff, state)
|
||||
q.add_to_db()
|
||||
return q
|
||||
|
||||
|
@ -127,7 +117,7 @@ class player():
|
|||
|
||||
def get_quests(self, state=0):
|
||||
cursor = self.DB.cursor()
|
||||
query = ('SELECT chat_id, qid, name, importance, difficulty, date, '
|
||||
query = ('SELECT chat_id, qid, name, importance, difficulty, '
|
||||
'state FROM quests WHERE chat_id = ?')
|
||||
if state is not None:
|
||||
query += ' AND state = ?'
|
||||
|
@ -142,7 +132,7 @@ class player():
|
|||
|
||||
def get_side_quests(self, state=0):
|
||||
cursor = self.DB.cursor()
|
||||
query = ('SELECT chat_id, qid, name, importance, difficulty, date, '
|
||||
query = ('SELECT chat_id, qid, name, importance, difficulty, '
|
||||
'state FROM side_quests WHERE chat_id = ?')
|
||||
if state is not None:
|
||||
query += ' AND state = ?'
|
||||
|
|
Loading…
Reference in New Issue