예제 #1
0
 def save_vote(self, user_id, meme_id, mark):
     if not Vote.is_voted(user_id, meme_id):
         vote = Vote(user_id, meme_id, mark).save()
         user = User.get(user_id)
         user.add_points(mark)
         print(f'{user} marked {vote}', flush=True)
         return True
     return False
예제 #2
0
    def vote_handler(self, call: telebot.types.CallbackQuery):
        telegram_id = call.from_user.id
        chat_id = str(call.message.chat.id)
        msg_id = call.message.message_id
        mark = int(call.data)
        username = '******' + call.from_user.username if call.from_user.username else call.from_user.first_name

        user = User.get(chat_id, telegram_id)
        if not user:
            user = User(telegram_id, chat_id, username).save()

        meme = Meme.get_meme(chat_id, msg_id - 1)
        if meme and not Vote.is_voted(user.id, meme.id):
            Vote(user.id, meme.id, mark).save()
            print(user.username, 'voted', mark, flush=True)
            self.answer_callback_query(call.id, text='You voted')
        else:
            print(user.username, 'have tried to vote again', flush=True)
            self.answer_callback_query(call.id, text='You have already voted')