Add separation of killed persons by chat #5

Merged
schneider merged 1 commits from :master into master 6 years ago
  1. 20
      db.py
  2. 10
      schneiderbot.py

20
db.py

@ -7,28 +7,28 @@ class Db():
self.conn = sqlite3.connect("deaths.db") self.conn = sqlite3.connect("deaths.db")
table_exist = self.conn.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='death'") table_exist = self.conn.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='death'")
if table_exist.fetchall() == []: if table_exist.fetchall() == []:
self.conn.execute("CREATE TABLE death(name)")
self.conn.execute("CREATE TABLE death(name text, chat integer )")
self.conn.execute("create index chat_idx on death(chat)")
def __del__(self): def __del__(self):
self.conn.commit() self.conn.commit()
def is_dead(self, user):
def is_dead(self, user, chat):
"""Reports whether a user is already dead""" """Reports whether a user is already dead"""
print(str(user))
res = self.conn.execute("select * from death where name = ?", [user])
res = self.conn.execute("select * from death where chat = ? and name = ?", [chat, user])
return res.fetchall() != [] return res.fetchall() != []
def kill(self, user):
def kill(self, user, chat):
"""Kills a user""" """Kills a user"""
self.conn.execute("insert into death(name) values(?)", [user])
self.conn.execute("insert into death(name, chat) values(?, ?)", [user, chat])
def revive(self, user):
def revive(self, user, chat):
"""Revives a user""" """Revives a user"""
self.conn.execute("delete from death where name = ?", [user])
self.conn.execute("delete from death where chat = ? and name = ?", [chat, user])
def get_dead_bodies(self):
def get_dead_bodies(self, chat):
"""List the dead""" """List the dead"""
names = [] names = []
for name in self.conn.execute("select * from death").fetchall():
for name in self.conn.execute("select * from death where chat = ?", chat).fetchall():
names.append(name[0]) names.append(name[0])
return ", ".join(names) return ", ".join(names)

10
schneiderbot.py

@ -131,7 +131,7 @@ def kill(bot, update, sudocall = False):
"""kill me pls""" """kill me pls"""
db = Db() db = Db()
user = update.message.from_user.first_name user = update.message.from_user.first_name
if db.is_dead(user):
if db.is_dead(user, update.message.chat_id):
if sudocall: if sudocall:
update.message.reply_text("%s killed again" % user) update.message.reply_text("%s killed again" % user)
return return
@ -141,19 +141,19 @@ def kill(bot, update, sudocall = False):
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)
db.kill(user, update.message.chat_id)
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"""
db = Db() db = Db()
user = update.message.from_user.first_name user = update.message.from_user.first_name
if not db.is_dead(user):
if not db.is_dead(user, update.message.chat_id):
update.message.reply_text("Maybe %s should have a litte 'accident' before" % user) update.message.reply_text("Maybe %s should have a litte 'accident' before" % user)
return return
if sudocall: if sudocall:
update.message.reply_text("%s is living again, for real" % user) update.message.reply_text("%s is living again, for real" % user)
db.revive(user)
db.revive(user, update.message.chat_id)
return return
update.message.reply_text("You fool! You cannot revive a dead person!") update.message.reply_text("You fool! You cannot revive a dead person!")
@ -165,7 +165,7 @@ SUDOCMDS = {
def graveyard(bot, update): def graveyard(bot, update):
"""List the dead""" """List the dead"""
db = Db() db = Db()
update.message.reply_text("Here are the dead people:\n%s" % db.get_dead_bodies())
update.message.reply_text("Here are the dead people:\n%s" % db.get_dead_bodies(update.message.chat_id))
def sudo(bot, update, args): def sudo(bot, update, args):
"""for real""" """for real"""

Loading…
Cancel
Save