def donate(reddit, msg, tx_queue, failover_time): user = models.User(msg.author.name) if user.is_registered(): split_message = msg.body.lower().strip().split() donate_index = split_message.index('+donate') amount = split_message[donate_index + 1] if utils.check_amount_valid(amount) and split_message[donate_index + 2] == 'doge': crypto.tip_user(user.username.address, models.User(config.bot_name).address, amount, tx_queue, failover_time) history.add_to_history(msg.author.name, msg.author.name, config.bot_name, amount, "donate") else: bot_logger.logger.info(lang.message_invalid_amount) reddit.redditor(user.username).message('invalid amount', lang.message_invalid_amount) else: bot_logger.logger.info('user %s not registered (command : donate) ' % user.username) msg.reply( Template(lang.message_need_register + lang.message_footer).render(username=user.username))
def pending_tips(rpc, msg): # check if user have pending tips pending_tips = user_function.get_user_pending_tip(msg.author.name) if pending_tips != False: for tip in pending_tips: # check if it's not too old & replay tipping limit_date = datetime.datetime.now() - datetime.timedelta(days=3) if (datetime.datetime.strptime(tip['time'], '%Y-%m-%dT%H:%M:%SZ') < limit_date): print "replay tipping - %s send %s for %s " % ( tip['sender'], tip['amount'], msg.author.name) crypto.tip_user(rpc, tip['sender'], msg.author.name, tip['amount']) user_function.remove_pending_tip(msg.author.name)
def move_funds(self, tx_queue, failover_time): if self.use is True: amount = self.user.get_balance() if crypto.tip_user(self.user.address, self.address, amount, tx_queue, failover_time): # update user storage file UserStorage.add_address(self.user.username, self.address)
def replay_remove_pending_tip(reddit, tx_queue, failover_time): # check if user have pending tips list_tips = user_function.get_unregistered_tip() if list_tips: for arr_tip in list_tips: tip = models.Tip().create_from_array(arr_tip) bot_logger.logger.info("replay tipping check for %s" % str(tip.id)) # check if it's not too old & replay tipping if not tip.is_expired(): if tip.receiver.is_registered(): bot_logger.logger.info( "replay tipping %s - %s send %s to %s " % (str(tip.id), tip.sender.username, tip.amount, tip.receiver.username)) tip.tx_id = crypto.tip_user(tip.sender.address, tip.receiver.address, tip.amount, tx_queue, failover_time) if tip.tx_id: tip.finish = True user_function.remove_pending_tip(tip.id) if tip.message_fullname is not None: msg_id = re.sub(r't\d+_(?P<id>\w+)', r'\g<id>', tip.message_fullname) msg = Comment(reddit, msg_id) msg.reply( Template(lang.message_tip).render( sender=tip.sender.username, receiver=tip.receiver.username, amount=str(tip.amount), value_usd=str(tip.get_value_usd()), txid=tip.tx_id)) else: tip.status = "waiting registration of receiver" bot_logger.logger.info( "replay check for %s - user %s not registered " % (str(tip.id), tip.receiver.username)) else: tip.status = "receiver not registered in time" tip.finish = "" bot_logger.logger.info( "delete old tipping - %s send %s to %s " % (tip.sender.username, tip.amount, tip.receiver.username)) user_function.remove_pending_tip(tip.id) # update tip status history.update_tip(tip.sender.username, tip) history.update_tip(tip.receiver.username, tip) else: bot_logger.logger.info("no pending tipping")
def withdraw_user(msg, failover_time): split_message = msg.body.strip().split() user = models.User(msg.author.name) if user.is_registered(): if utils.check_amount_valid( split_message[1]) and split_message[4] != user.address: amount = float(split_message[1]) amount = round(amount - 0.5) user_balance = user.get_balance() if amount >= float(user_balance): bot_logger.logger.info( 'user %s not have enough to withdraw this amount (%s), balance = %s' % (user.username, amount, user_balance)) msg.reply( Template(lang.message_balance_low_withdraw).render( username=user.username, user_balance=str(user_balance), amount=str(amount)) + lang.message_footer) else: receiver_address = split_message[4] tip_id = random.randint(0, 99999999) models.HistoryStorage.add_to_history(user.username, user.username, receiver_address, amount, "withdraw", "", tip_id) send = crypto.tip_user(user.address, receiver_address, amount, None, failover_time) if send: models.HistoryStorage.update_withdraw( user.username, True, send, tip_id) value_usd = utils.get_coin_value(amount) msg.reply( Template(lang.message_withdraw + lang.message_footer).render( username=user.username, receiver_address=receiver_address, amount=str(amount), value_usd=str(value_usd))) elif split_message[4] == user.address: msg.reply(lang.message_withdraw_to_self + lang.message_footer) else: bot_logger.logger.info(lang.message_invalid_amount) msg.reply(lang.message_invalid_amount + lang.message_footer) else: bot_logger.logger.info('user %s not registered (command : withdraw) ' % user.username) msg.reply( Template(lang.message_need_register + lang.message_footer).render(username=msg.author.name))
def withdraw_user(msg, failover_time): rpc = crypto.get_rpc() split_message = msg.body.strip().split() user = models.User(msg.author.name) if user.is_registered(): if utils.check_amount_valid( split_message[1]) and split_message[4] != user.address: amount = float(split_message[1]) amount = round(amount - 0.5) user_balance = user.get_balance_confirmed() user_spendable_balance = crypto.get_user_spendable_balance( msg.author.name) if amount >= float(user_balance) + float(user_spendable_balance): bot_logger.logger.info( 'user %s not have enough to withdraw this amount (%s), balance = %s' % (msg.author.name, amount, user_balance)) msg.reply( Template(lang.message_balance_low_withdraw).render( username=msg.author.name, user_balance=str(user_balance), amount=str(amount)) + lang.message_footer) else: receiver_address = split_message[4] history.add_to_history(msg.author.name, user.username, receiver_address, amount, "withdraw") send = crypto.tip_user(user.address, receiver_address, amount, None, failover_time) if send: history.add_to_history(msg.author.name, user.username, receiver_address, amount, "withdraw", True, send) value_usd = utils.get_coin_value(amount) msg.reply( Template(lang.message_withdraw + lang.message_footer).render( username=msg.author.name, receiver_address=receiver_address, amount=str(amount), value_usd=str(value_usd))) elif split_message[4] == user.address: msg.reply(lang.message_withdraw_to_self + lang.message_footer) else: bot_logger.logger.info(lang.message_invalid_amount) msg.reply(lang.message_invalid_amount + lang.message_footer) else: msg.reply( Template(lang.message_need_register + lang.message_footer).render(username=msg.author.name))
def tip_user(rpc, msg): print('An user mention detected ') split_message = msg.body.strip().split() tip_index = split_message.index('+/u/sodogetiptest') if split_message[tip_index] == '+/u/sodogetiptest' and split_message[ tip_index + 2] == 'doge': amount = split_message[tip_index + 1] if utils.check_amount_valid(amount): parent_comment = msg.parent() if user_function.user_exist(msg.author.name): # check we have enough user_balance = crypto.get_user_balance(rpc, msg.author.name) if int(amount) >= user_balance: print( 'user %s not have enough to tip this amount (%s), balance = %s' % (msg.author.name, amount, user_balance)) msg.reply('+/u/%s your balance is too low for this tip ' % msg.author.name) else: # check user have address before tip if user_function.user_exist(parent_comment.author.name): if crypto.tip_user(rpc, msg.author.name, parent_comment.author.name, amount): user_function.add_to_history( msg.author.name, msg.author.name, parent_comment.author.name, amount, "tip") print '%s tip %s to %s' % ( msg.author.name, str(amount), parent_comment.author.name) msg.reply('+/u/%s tip %s to %s' % (msg.author.name, str(amount), parent_comment.author.name)) else: user_function.save_unregistered_tip( msg.author.name, parent_comment.author.name, amount) user_function.add_to_history( msg.author.name, msg.author.name, parent_comment.author.name, amount, "tip", False) print('user %s not registered' % parent_comment.author.name) msg.reply( '+/u/%s need %s before can be tipped (tip saved during 3 day)' % (parent_comment.author.name, linkRegister)) else: msg.reply('You need %s before' % linkRegister) else: print('You must use valid amount') msg.reply('You must use valid amount')
def replay_remove_pending_tip(rpc, reddit, tx_queue, failover_time): # check if it's not too old & replay tipping limit_date = datetime.datetime.now() - datetime.timedelta(days=3) # check if user have pending tips list_tips = user_function.get_unregistered_tip() if list_tips: for tip in list_tips: bot_logger.logger.info("replay tipping check for %s" % str(tip['id'])) if (datetime.datetime.strptime(tip['time'], '%Y-%m-%dT%H:%M:%S.%f') > limit_date): if (user_function.user_exist(tip['receiver'])): bot_logger.logger.info( "replay tipping %s - %s send %s to %s " % (str(tip['id']), tip['sender'], tip['amount'], tip['receiver'])) txid = crypto.tip_user(rpc, tip['sender'], tip['receiver'], tip['amount'], tx_queue, failover_time) user_function.remove_pending_tip(tip['id']) value_usd = utils.get_coin_value(tip['amount']) if 'message_fullname' in tip.keys(): msg_id = re.sub(r't\d+_(?P<id>\w+)', r'\g<id>', tip['message_fullname']) msg = Comment(reddit, msg_id) msg.reply( Template(lang.message_tip).render( sender=tip['sender'], receiver=tip['receiver'], amount=str(tip['amount']), value_usd=str(value_usd), txid=txid)) else: bot_logger.logger.info( "replay check for %s - user %s not registered " % (str(tip['id']), tip['receiver'])) else: bot_logger.logger.info( "delete old tipping - %s send %s for %s " % (tip['sender'], tip['amount'], tip['receiver'])) user_function.remove_pending_tip(tip['id']) else: bot_logger.logger.info("no pending tipping")
def tip_user(reddit, msg, tx_queue, failover_time): bot_logger.logger.info('An user mention detected ') bot_logger.logger.debug("failover_time : %s " % (str(failover_time.value))) # create an Tip tip = models.Tip() # update sender tip.set_sender(msg.author.name) # check user who use command is registered if tip.sender.is_registered() is not True: bot_logger.logger.info('user %s not registered (sender) ' % msg.author.name) msg.reply( Template(lang.message_need_register + lang.message_footer).render(username=msg.author.name)) return False # parse message tip.parse_message(msg.body) # set reddit message id tip.message_fullname = msg.fullname # check amount of tip if not utils.check_amount_valid(tip.amount): # invalid amount bot_logger.logger.info(lang.message_invalid_amount) reddit.redditor(msg.author.name).message('invalid amount', lang.message_invalid_amount) return False if tip.currency is None: bot_logger.logger.info(lang.message_invalid_currency) reddit.redditor(msg.author.name).message('invalid currency', lang.message_invalid_currency) return False # update receiver tip.set_receiver(msg.parent().author.name) # check user not tip self if tip.sender.username == tip.receiver.username: reddit.redditor(tip.sender.username).message( 'cannot tip self', Template(lang.message_recipient_self).render( username=tip.sender.username)) return False # check we have enough user_spendable_balance = crypto.balance_user(msg, failover_time) bot_logger.logger.debug('user_spendable_balance = %s' % user_spendable_balance) # in failover we need to use only user_balance if tip.amount >= float(user_spendable_balance): user_pending_balance = tip.sender.get_balance_unconfirmed() # not enough for tip if tip.amount < float(user_pending_balance): reddit.redditor(tip.sender.username).message( 'pending tip', Template(lang.message_balance_pending_tip).render( username=tip.sender.username)) else: bot_logger.logger.info( 'user %s not have enough to tip this amount (%s), balance = %s' % (tip.sender.username, str( tip.amount), str(user_spendable_balance))) reddit.redditor(tip.sender.username).message( 'low balance', Template(lang.message_balance_low_tip).render( username=tip.sender.username)) else: # add tip to history of sender & receiver history.add_to_history_tip(tip.sender.username, "tip send", tip) history.add_to_history_tip(tip.receiver.username, "tip receive", tip) # check user who receive tip have an account if tip.receiver.is_registered(): tip.tx_id = crypto.tip_user(tip.sender.address, tip.receiver.address, tip.amount, tx_queue, failover_time) if tip.tx_id: tip.finish = True tip.status = 'ok' bot_logger.logger.info( '%s tip %s to %s' % (msg.author.name, str(tip.amount), tip.receiver.username)) # if user have 'verify' in this command he will have confirmation if tip.verify: msg.reply( Template(lang.message_tip).render( sender=msg.author.name, receiver=tip.receiver.username, amount=str(int(tip.amount)), value_usd=str(tip.get_value_usd()), txid=tip.tx_id)) else: bot_logger.logger.info('user %s not registered (receiver)' % tip.receiver.username) tip.status = "waiting registration of receiver" # save tip user_function.save_unregistered_tip(tip) # send message to sender of tip reddit.redditor(tip.sender.username).message( 'tipped user not registered', Template(lang.message_recipient_register).render( username=tip.receiver.username)) # send message to receiver reddit.redditor(tip.receiver.username).message( Template(lang.message_recipient_need_register_title).render( amount=str(tip.amount)), Template(lang.message_recipient_need_register_message).render( username=tip.receiver.username, sender=msg.author.name, amount=str(tip.amount), value_usd=str(tip.get_value_usd()))) # update tip status history.update_tip(tip.sender.username, tip) history.update_tip(tip.receiver.username, tip)
def tip_user(rpc, reddit, msg, tx_queue, failover_time): bot_logger.logger.info('An user mention detected ') bot_logger.logger.debug("failover_time : %s " % (str(failover_time.value))) split_message = msg.body.lower().strip().split() tip_index = split_message.index(str('+/u/' + config.bot_name)) if split_message[tip_index] == str( '+/u/' + config.bot_name) and split_message[tip_index + 2] == 'doge': amount = float(split_message[tip_index + 1]) amount = round(amount - 0.5) if utils.check_amount_valid(amount): parent_comment = msg.parent() if user_function.user_exist(msg.author.name) and ( msg.author.name != parent_comment.author.name): # check we have enough user_balance = crypto.get_user_confirmed_balance( rpc, msg.author.name) user_pending_balance = crypto.get_user_unconfirmed_balance( rpc, msg.author.name) user_spendable_balance = crypto.balance_user( rpc, msg, failover_time) bot_logger.logger.debug('user_spendable_balance = %s' % user_spendable_balance) # in failover we need to use only user_balance if amount >= float(user_spendable_balance): # not enough for tip if amount < float(user_pending_balance): reddit.redditor(msg.author.name).message( 'pending tip', Template(lang.message_balance_pending_tip).render( username=msg.author.name)) else: bot_logger.logger.info( 'user %s not have enough to tip this amount (%s), balance = %s' % (msg.author.name, str(amount), str(user_balance))) reddit.redditor(msg.author.name).message( 'low balance', Template(lang.message_balance_low_tip).render( username=msg.author.name)) else: value_usd = utils.get_coin_value(amount) # check user have address before tip if user_function.user_exist(parent_comment.author.name): txid = crypto.tip_user(rpc, msg.author.name, parent_comment.author.name, amount, tx_queue, failover_time) if txid: user_function.add_to_history( msg.author.name, msg.author.name, parent_comment.author.name, amount, "tip send", txid) user_function.add_to_history( parent_comment.author.name, msg.author.name, parent_comment.author.name, amount, "tip receive", txid) bot_logger.logger.info( '%s tip %s to %s' % (msg.author.name, str(amount), parent_comment.author.name)) # if user have 'verify' in this command he will have confirmation if split_message.count( 'verify') or int(amount) >= 1000: msg.reply( Template(lang.message_tip).render( sender=msg.author.name, receiver=parent_comment.author.name, amount=str(int(amount)), value_usd=str(value_usd), txid=txid)) else: user_function.save_unregistered_tip( msg.author.name, parent_comment.author.name, amount, msg.fullname) user_function.add_to_history( msg.author.name, msg.author.name, parent_comment.author.name, amount, "tip send", False) user_function.add_to_history( parent_comment.author.name, msg.author.name, parent_comment.author.name, amount, "tip receive", False) bot_logger.logger.info('user %s not registered' % parent_comment.author.name) reddit.redditor(msg.author.name).message( 'tipped user not registered', Template(lang.message_recipient_register).render( username=parent_comment.author.name)) reddit.redditor(parent_comment.author.name).message( Template(lang.message_recipient_need_register_title ).render(amount=str(amount)), Template( lang.message_recipient_need_register_message). render(username=parent_comment.author.name, sender=msg.author.name, amount=str(amount), value_usd=str(value_usd))) elif user_function.user_exist( msg.author.name) and (msg.author.name == parent_comment.author.name): reddit.redditor(msg.author.name).message( 'cannot tip self', Template(lang.message_recipient_self).render( username=msg.author.name)) else: reddit.redditor(msg.author.name).message( 'tipped user not registered', Template(lang.message_need_register).render( username=msg.author.name)) else: bot_logger.logger.info(lang.message_invalid_amount) reddit.redditor(msg.author.name).message( 'invalid amount', lang.message_invalid_amount)
def gold(reddit, msg, tx_queue, failover_time): user = models.User(msg.author.name) if user.is_registered(): gold_month = number_gold_credit() if msg.body.strip() == 'buy': # Number of month quantity = 1 # check if we have enough credits if not gold_month >= quantity: # store in db want an gold, when bot have new credits a PM can be send db = TinyDB(config.DATA_PATH + 'reddit_gold_empty.json') db.insert({ "user": user.username, "quantity": quantity, 'time': datetime.datetime.now().isoformat(), }) db.close() msg.reply(Template(lang.message_gold_no_more).render(username=user.username)) return False # check user confirmed balance is ok if user.get_balance_confirmed() >= config.price_reddit_gold: msg.reply(Template(lang.message_gold_no_enough_pivx).render(username=user.username)) return False # send amount of one month of gold to address tx_id = crypto.tip_user(user.address, config.gold_address, config.price_reddit_gold, tx_queue, failover_time) if tx_id: # send gold reddit Redditor(reddit, user.username).gild(months=quantity) # update gold reddit table store_user_buy(user, quantity, tx_id) # update user history models.HistoryStorage.add_to_history(user, sender=user.username, receiver="Reddit", amount=config.price_reddit_gold, action="buy reddit gold") # send succes message msg.reply(Template(lang.message_buy_gold_success).render(username=user.username)) else: # send error message msg.reply(Template(lang.message_buy_gold_error).render(username=user.username)) elif msg.body.strip() == 'remind': # store in db want an gold, when bot have new credits a PM can be send db = TinyDB(config.DATA_PATH + 'reddit_gold_remind.json') db.insert({ "user": user.username, "remind": "True", 'time': datetime.datetime.now().isoformat(), }) db.close() else: # send info on reddit gold msg.reply(Template(lang.message_buy_gold).render(username=user.username, gold_credit=gold_month, price=config.price_reddit_gold)) else: bot_logger.logger.info('user %s not registered (command : donate) ' % user.username) msg.reply(Template(lang.message_need_register + lang.message_footer).render(username=user.username))