Browse Source

Add some more killing features

quotes
Schneider 6 years ago
parent
commit
2fc3d183c8
  1. 1
      .gitignore
  2. 34
      db.py
  3. 32
      schneiderbot.py

1
.gitignore

@ -1,4 +1,5 @@
token token
deaths.db
# ---> Python # ---> Python
# Byte-compiled / optimized / DLL files # Byte-compiled / optimized / DLL files

34
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)

32
schneiderbot.py

@ -3,6 +3,7 @@
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from telegram import ParseMode from telegram import ParseMode
from db import Db
import logging import logging
import os import os
import random import random
@ -128,22 +129,43 @@ def sendDog(bot, update):
def kill(bot, update, sudocall = False): def kill(bot, update, sudocall = False):
"""kill me pls""" """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" message = update.message.from_user.first_name + " died"
if sudocall: if sudocall:
message += " for real" message += " for real"
db.kill(user)
update.message.reply_text(message) update.message.reply_text(message)
def revive(bot, update, sudocall = False): def revive(bot, update, sudocall = False):
"""unkill me pls""" """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: 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 = { SUDOCMDS = {
"kill" : kill, "kill" : kill,
"revive" : revive "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): def sudo(bot, update, args):
"""for real""" """for real"""
@ -267,6 +289,7 @@ def main():
dp.add_handler(CommandHandler("kill", kill)) dp.add_handler(CommandHandler("kill", kill))
dp.add_handler(CommandHandler("revive", revive)) dp.add_handler(CommandHandler("revive", revive))
dp.add_handler(CommandHandler("simon", simon)) dp.add_handler(CommandHandler("simon", simon))
dp.add_handler(CommandHandler("graveyard", graveyard))
# log all errors # log all errors
@ -283,4 +306,3 @@ def main():
if __name__ == '__main__': if __name__ == '__main__':
main() main()
Loading…
Cancel
Save