Example #1
0
    def enable_user_detection(self, bot, update, args):
        """Start or stop detect_users_handler, pass arg 0 or 1 to activate"""
        mode = args[0]
        if mode == "1":
            self.members = botutils.load_members()
            self.dispatcher.add_handler(self.user_handler)
            message = "User detection enabled"
        else:
            self.dispatcher.remove_handler(self.user_handler)
            message = "User detection disabled"

        update.message.reply_text(message, parse_mode="HTML")
        return True
    def send_scores(self, bot, update):
        """Send game ranking for each players"""
        scores = gm.get_scores()
        members = bu.load_members()
        message = ""
        for i, score in enumerate(reversed(scores)):
            if i == 0:
                message += "\U0001F947"  # First place medal
            elif i == 1:
                message += "\U0001F948"  # Second place medal
            elif i == 2:
                message += "\U0001F949"  # Third place medal
            first_name = bu.get_member_by_id(score["id"], members=members)["first_name"]
            message += "<a href='tg://user?id={}'>{}</a> - {} victoires\n".format(score["id"], first_name, score["won"])

        bot.sendMessage(chat_id=config.chat_id, text=message, parse_mode="HTML", disable_notification=True)
 def update_DB(self):
     # TODO : update values (is_active and names...) not just inserting
     conn = sqlite3.connect(self.database)
     c = conn.cursor()
     
     # get all users and insert via id
     members = bu.load_members()
     db_member_ids = c.execute("SELECT id FROM person").fetchall()
     for member in members:
         if not member["id"] in db_member_ids:
             try:
                 c.execute("""INSERT INTO person(id, first_name, GD_dir_id) 
                    VALUES (?,?,?);""", (member["id"], member["first_name"], member["GD_dir_id"]))
             except sqlite3.Error as e:
                 logging.info("An SQL error [{}] occurred while inserting person {}.".format(e, member))
     # Update is_active status for old members
     # TODO : get all members id and change status of every other person to is_active=0
     
     # get all formats from local file and try to insert
     formats = self.load_formats()
     db_format_ids = c.execute("SELECT id FROM format").fetchall()
     for format in formats:
         if not format["id"] in db_format_ids:
             try:
                 c.execute("""INSERT INTO format(id, name, GD_banlist_file_id) 
                    VALUES (?,?,?);""", (format["id"], format["name"], format["GD_banlist_file_id"]))
             except sqlite3.Error as e:
                 logging.info("An SQL error [{}] occurred while inserting format {}.".format(e, format))
     
     # get all decks from Google Drive and insert via file_id
     db_gd_deck_ids = c.execute("SELECT GD_file_id FROM deck").fetchall()
     for member in members:
         decks = [] # TODO: Load list of file for each deck in member GD directory
         for deck in decks:
             if not deck["id"] in db_gd_deck_ids:
                 try:
                     c.execute("""INSERT INTO deck(personn_id, name, GD_file_id) 
                        VALUES (?,?,?);""", (member["id"], deck["title"], deck["id"]))
                 except sqlite3.Error as e:
                     logging.info("An SQL error [{}] occurred while inserting format {}.".format(e, format))
         # Update is_active status for old decks
         # TODO : get all current decks id and change status of every other decks to is_active=0
     
     conn.commit()   
     conn.close()
     return True
 def __init__(self):
     """Load members and unfinished transactions"""
     self.members = bu.load_members()
     self.transactions = self.load_transactions()
 def update_members(self):
     """Update list of members if new ones are added on the go"""
     members = bu.load_members()
     self.members = members
     return members