2018-10-17 16:52:21 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import logging
|
2018-10-22 12:01:49 +02:00
|
|
|
import telegram
|
2018-10-24 15:48:02 +02:00
|
|
|
import sqlite3
|
2018-10-31 23:34:01 +01:00
|
|
|
import questable
|
|
|
|
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
|
2018-10-17 16:52:21 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
import config
|
|
|
|
except ImportError:
|
|
|
|
print("Missing Config. Exiting.")
|
|
|
|
exit()
|
|
|
|
|
|
|
|
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - \
|
|
|
|
%(message)s', level=logging.INFO)
|
|
|
|
|
|
|
|
|
|
|
|
def start(bot, update):
|
|
|
|
chat_id = update.message.chat_id
|
2018-10-22 12:01:49 +02:00
|
|
|
name = str(update.message.from_user.first_name)
|
|
|
|
if update.message.from_user.last_name:
|
|
|
|
name += " " + str(update.message.from_user.last_name)
|
2018-10-17 16:52:21 +02:00
|
|
|
text = f"Hello {name}!\n" + \
|
|
|
|
"Welcome to Questable. To get started, check /help."
|
2018-10-22 12:01:49 +02:00
|
|
|
custom_keyboard = [
|
2018-10-31 23:34:01 +01:00
|
|
|
['Add Quest', 'Add Side Quest'],
|
|
|
|
['List Quests', 'List Side Quests']
|
2018-10-22 12:01:49 +02:00
|
|
|
]
|
|
|
|
reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard)
|
|
|
|
bot.send_message(chat_id=chat_id, text=text, reply_markup=reply_markup)
|
2018-10-17 16:52:21 +02:00
|
|
|
|
|
|
|
|
2018-10-31 23:34:01 +01:00
|
|
|
def add_quest(bot, update, player, type="quest"):
|
|
|
|
# Get largest id and set qid to 1 more than that
|
|
|
|
if type == "quest":
|
|
|
|
x = player.get_quests(None)
|
|
|
|
elif type == "side_quest":
|
|
|
|
x = player.get_side_quests(None)
|
|
|
|
else:
|
|
|
|
raise ValueError('Not quest or side_quest')
|
|
|
|
if len(x) == 0:
|
|
|
|
qid = 1
|
|
|
|
else:
|
|
|
|
x.sort(key=lambda i: i.QID, reverse=True)
|
|
|
|
qid = x[0].QID + 1
|
|
|
|
|
|
|
|
# Add quest / sub_quest
|
|
|
|
if type == 'quest':
|
|
|
|
questable.add_quest(player.DB, player.CHAT_ID, qid)
|
|
|
|
player.set_state('aq', qid)
|
|
|
|
if type == 'side_quest':
|
|
|
|
questable.add_side_quest(player.DB, player.CHAT_ID, qid)
|
|
|
|
player.set_state('asq', qid)
|
|
|
|
|
|
|
|
# Send message
|
|
|
|
chat_id = update.message.chat_id
|
|
|
|
text = ("What shall the name of " +
|
|
|
|
{"quest": "Quest", "side_quest": "Side Quest"}[type] + " be?")
|
|
|
|
reply_markup = telegram.ReplyKeyboardRemove()
|
|
|
|
bot.send_message(chat_id=chat_id, text=text, reply_markup=reply_markup)
|
|
|
|
|
|
|
|
|
|
|
|
def message_handling(bot, update, db):
|
|
|
|
text = update.message.text
|
|
|
|
player = questable.player(db, update.message.chat_id)
|
|
|
|
state = player.get_state()
|
|
|
|
if state["state"] == "none":
|
|
|
|
if text == "Add Quest":
|
|
|
|
add_quest(bot, update, player)
|
|
|
|
elif text == "Add Side Quest":
|
|
|
|
add_quest(bot, update, player, "side_quest")
|
|
|
|
|
|
|
|
|
|
|
|
db = sqlite3.connect("questable.db", check_same_thread=False)
|
2018-10-24 15:48:02 +02:00
|
|
|
cursor = db.cursor()
|
|
|
|
# Set up tables
|
|
|
|
queries = [
|
2018-10-27 11:57:13 +02:00
|
|
|
("CREATE TABLE IF NOT EXISTS quests(chat_id int NOT NULL, qid int NOT"
|
2018-10-31 23:34:01 +01:00
|
|
|
" NULL, name varchar(255), difficulty int, importance int, date int"
|
|
|
|
", state int DEFAULT 0, UNIQUE(chat_id, qid));"),
|
2018-10-24 15:48:02 +02:00
|
|
|
|
2018-10-31 23:34:01 +01:00
|
|
|
("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));"),
|
2018-10-24 15:48:02 +02:00
|
|
|
|
2018-10-27 11:57:13 +02:00
|
|
|
("CREATE TABLE IF NOT EXISTS points(chat_id int PRIMARY KEY, points "
|
2018-10-24 15:48:02 +02:00
|
|
|
"int);"),
|
|
|
|
|
2018-10-27 11:57:13 +02:00
|
|
|
("CREATE TABLE IF NOT EXISTS state(chat_id int PRIMARY KEY, state "
|
2018-10-31 13:46:26 +01:00
|
|
|
"varchar(10), extra varchar(10));"),
|
2018-10-24 15:48:02 +02:00
|
|
|
]
|
|
|
|
for query in queries:
|
|
|
|
cursor.execute(query)
|
|
|
|
db.commit()
|
|
|
|
|
2018-10-17 16:52:21 +02:00
|
|
|
updater = Updater(token=config.api_key)
|
|
|
|
dispatcher = updater.dispatcher
|
|
|
|
|
|
|
|
start_handler = CommandHandler('start', start)
|
|
|
|
dispatcher.add_handler(start_handler)
|
|
|
|
|
2018-10-31 23:34:01 +01:00
|
|
|
handler = MessageHandler(Filters.text, lambda x, y: message_handling(x, y, db))
|
|
|
|
dispatcher.add_handler(handler)
|
|
|
|
|
2018-10-17 16:52:21 +02:00
|
|
|
updater.start_polling()
|