def restart_avaiable_karma(self): with DB.atomic() as transaction: try: Karma.update(avaiable_karma = DEFAULT_AVAIABLE_KARMA) \ .execute() except Exception as e: transaction.rollback() raise e return True
def reset_server_karma(self, guildId): with DB.atomic() as transaction: try: Karma.update(karma = 0).where(Karma.guild == guildId) \ .execute() except Exception as e: transaction.rollback() raise e return True
def cmd_core_stat(message): try: if is_private(message) or not sender_is_admin(message.chat.id, message.from_user.id): return None typing(message) karma_objects = Karma.objects(chat=message.chat.id) transactions = 0 users = [] canceled = 0 canceled_amount = 0 amount = 0 for karma in karma_objects: transactions += 1 amount += karma.amount if karma.to_user not in users: users.append(karma.to_user) if karma.from_user not in users: users.append(karma.from_user) if karma.rollback: canceled += 1 canceled_amount += karma.amount bot.reply_to( message, 'Транзакций: {} (отменено: {}, активно: {})\n' 'Пользователей: {}\n' 'Карма в чате: {} (отменено {}, активно {})\n'.format( transactions, canceled, transactions - canceled, len(users), amount, canceled_amount, amount - canceled_amount)) except: crash_message(message)
def cmd_top(message): try: typing(message) karma_records = Karma.objects(chat=message.chat.id) users = {} for karma in karma_records: if karma.rollback: continue if karma.from_user and karma.transfer: users[karma.from_user] = users.get(karma.from_user, 0) - karma.amount if karma.to_user: users[karma.to_user] = users.get(karma.to_user, 0) + karma.amount text = ['Статистика:'] if bot_id in users: users.pop(bot_id) for index, (user, karma) in enumerate(sorted(users.items(), key=lambda x: x[1], reverse=True), start=1): if index > 10: break text.append('{}) {}: {}'.format( index, get_username_or_name(bot.get_chat(user)), karma)) bot.send_message(message.chat.id, '\n'.join(text), disable_notification=True) except: crash_message(message)
def query_settings(callbackquery): try: key, data = parse_inline_data(callbackquery.data) karma = Karma.get_transaction(data[0]) if karma: if not sender_is_admin(karma.chat, callbackquery.from_user.id): return bot.answer_callback_query(callbackquery.id, 'You shall not pass!') karma.cancel() if karma.rollback: bot.answer_callback_query( callbackquery.id, 'Transaction ' + data[0] + ' is canceled!') text = '#karma #WARNING #Transaction\nCanceled: #{}\nby {} ({})'.format( data[0], get_username_or_name(callbackquery.from_user), callbackquery.from_user.id) bot.send_message(LOGGING_CHAT, text) notify_chat_admins(karma.chat, text) else: bot.answer_callback_query( callbackquery.id, 'Transaction ' + data[0] + ' is activated!') text = '#karma #WARNING #Transaction\nRestored: #{}\nby {} ({})'.format( data[0], get_username_or_name(callbackquery.from_user), callbackquery.from_user.id) bot.send_message(LOGGING_CHAT, text) notify_chat_admins(karma.chat, text) bot.answer_callback_query(callbackquery.id, 'Wrong transaction!') except: with CrashReport(*sys.exc_info()) as c: log.warning(c.formatted_traceback) c.save()
def get_top_karma_server(self, guildId, pageSize, page): karma = Karma.select() \ .where(Karma.guild == guildId) \ .order_by(Karma.karma.desc()) \ .paginate(page, paginate_by=pageSize) \ .execute() return karma
def karma_transaction(chat=0, from_user=0, to_user=0, amount=0, description='', transfer=True): if amount == 0: return False if type(chat) is telebot.types.Chat: chat_id = chat.id else: chat_id = chat if type(from_user) is telebot.types.User: from_user_id = from_user.id else: from_user_id = from_user if type(to_user) is telebot.types.User: to_user_id = to_user.id else: to_user_id = to_user if from_user and transfer: update_cached_user_karma(from_user_id, chat_id, -amount) if to_user: update_cached_user_karma(to_user_id, chat_id, amount) karma = Karma(chat=chat_id, from_user=from_user_id, to_user=to_user_id, amount=amount, description=description, transfer=transfer) karma.save() log_transaction(karma.pk, chat=chat, from_user=from_user, to_user=to_user, amount=amount, description=description, transfer=transfer)
def update_karma(name, op): karma = db.session.query(Karma).filter_by( name=name).with_lockmode('update').first() if not karma: karma = Karma(name) db.session.add(karma) if op == "--": karma.value -= 1 elif op == "++": karma.value += 1 db.session.commit() return str(karma)
def reset_chat_karma(chat): chat_karma = Karma.objects(chat=chat.id) counter = 0 for karma in chat_karma: if not karma.rollback: karma.revoke() counter += 1 # reset_cache() reset_cached_chat_karma(chat) generate_karma_cache() log.warning('Reset karma for chat: {} (<code>{}</code>)\n' '<code>{}</code> transactions'.format( chat.title or get_username_or_name(chat), chat.id, counter)) return counter
def _fetch_karma(self, clientId, guildId): karma = Karma.select() \ .where((Karma.client == clientId) & (Karma.guildId == guildId)) \ .first() return karma
def get_user_karma_all(self, clientId): karma = Karma.select(fn.SUM(Karma.karma).alias("karmaSum")) \ .where((Karma.client == clientId)) \ .first() return karma
def get_user_karma(self, guildId, clientId): karma = Karma.select() \ .where((Karma.guild == guildId) & (Karma.client == clientId)) \ .first() return karma
def get_user_avaiable_karma(self, clientId, guildId): karma = Karma.select(Karma.avaiable_karma) \ .where((Karma.client_id == clientId) & (Karma.guild == guildId)) \ .first() return karma.avaiable_karma
def check_user_has_karma(self, userId, guildId): result = Karma.select().where((Karma.guild == guildId) & (Karma.client == userId)) \ .exists() return result
def create_karma(self, userId, guildId): Karma.create(guild=guildId, client=userId)