From 2fc3d183c8654cc017b7a50f59f0363a2b9721d7 Mon Sep 17 00:00:00 2001 From: Marcel Schneider Date: Tue, 16 Oct 2018 19:53:10 +0200 Subject: [PATCH] Add some more killing features --- .gitignore | 1 + db.py | 34 ++++++++++++++++++++++++++++++++++ schneiderbot.py | 42 ++++++++++++++++++++++++++++++++---------- 3 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 db.py mode change 100644 => 100755 schneiderbot.py diff --git a/.gitignore b/.gitignore index 422f72b..641275b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ token +deaths.db # ---> Python # Byte-compiled / optimized / DLL files diff --git a/db.py b/db.py new file mode 100644 index 0000000..0f7ffdd --- /dev/null +++ b/db.py @@ -0,0 +1,34 @@ +"""DB interface""" +import sqlite3 + +class Db(): + """ Abstract interface for db actions """ + def __init__(self): + self.conn = sqlite3.connect("deaths.db") + table_exist = self.conn.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='death'") + if table_exist.fetchall() == []: + self.conn.execute("CREATE TABLE death(name)") + + def __del__(self): + self.conn.commit() + + def is_dead(self, user): + """Reports whether a user is already dead""" + print(str(user)) + res = self.conn.execute("select * from death where name = ?", [user]) + return res.fetchall() != [] + + def kill(self, user): + """Kills a user""" + self.conn.execute("insert into death(name) values(?)", [user]) + + def revive(self, user): + """Revives a user""" + self.conn.execute("delete from death where name = ?", [user]) + + def get_dead_bodies(self): + """List the dead""" + names = [] + for name in self.conn.execute("select * from death").fetchall(): + names.append(name[0]) + return ", ".join(names) diff --git a/schneiderbot.py b/schneiderbot.py old mode 100644 new mode 100755 index 4606403..eba0222 --- a/schneiderbot.py +++ b/schneiderbot.py @@ -3,6 +3,7 @@ from telegram.ext import Updater, CommandHandler, MessageHandler, Filters from telegram import ParseMode +from db import Db import logging import os import random @@ -85,7 +86,7 @@ with open("stoll.txt", "r") as tmp_file: with open("manta.txt", "r") as tmp_file: MANTA = tmp_file.readlines() - + with open("goodlife.txt", "r") as tmp_file: GOOD_LIFE = tmp_file.readlines() @@ -128,22 +129,43 @@ def sendDog(bot, update): def kill(bot, update, sudocall = False): """kill me pls""" + db = Db() + user = update.message.from_user.first_name + if db.is_dead(user): + if sudocall: + update.message.reply_text("%s killed again" % user) + return + update.message.reply_text("%s is already dead" % user) + return + message = update.message.from_user.first_name + " died" if sudocall: message += " for real" + db.kill(user) update.message.reply_text(message) def revive(bot, update, sudocall = False): """unkill me pls""" - message = update.message.from_user.first_name + " is living again" + db = Db() + user = update.message.from_user.first_name + if not db.is_dead(user): + update.message.reply_text("Maybe %s should have a litte 'accident' before" % user) + return if sudocall: - message += " for real" - update.message.reply_text(message) + update.message.reply_text("%s is living again, for real" % user) + db.revive(user) + return + update.message.reply_text("You fool! You cannot revive a dead person!") SUDOCMDS = { - "kill" : kill, - "revive" : revive - } + "kill" : kill, + "revive" : revive +} + +def graveyard(bot, update): + """List the dead""" + db = Db() + update.message.reply_text("Here are the dead people:\n%s" % db.get_dead_bodies()) def sudo(bot, update, args): """for real""" @@ -170,12 +192,12 @@ def manta(bot, update): """Boah ey, geile Karre!""" rand = random.SystemRandom() update.message.reply_text(MANTA[rand.randrange(0, len(MANTA), 1)]) - + def goodlife(bot, update): """GOOD LIFE MY A...""" rand = random.SystemRandom() update.message.reply_text(GOOD_LIFE[rand.randrange(0, len(GOOD_LIFE), 1)]) - + def shrug(bot, update): """SHRUG""" update.message.reply_text("¯\_(ツ)_/¯") @@ -267,6 +289,7 @@ def main(): dp.add_handler(CommandHandler("kill", kill)) dp.add_handler(CommandHandler("revive", revive)) dp.add_handler(CommandHandler("simon", simon)) + dp.add_handler(CommandHandler("graveyard", graveyard)) # log all errors @@ -283,4 +306,3 @@ def main(): if __name__ == '__main__': main() -