def trade_coin(user, input): message = "The trading format is 'trade 10 eth for 1000 gnt'" if len(input) < 6: return message else: from_num, from_str = get_num_coins(input[:3]) to_num, to_str = get_num_coins(input[3:]) from_coin = get_coin_from_input(from_str) to_coin = get_coin_from_input(to_str) w_from_coin = session.query(Wallet).filter((Wallet.user == user) &\ (Wallet.coin == from_coin)).first() w_to_coin = session.query(Wallet).filter((Wallet.user == user) &\ (Wallet.coin == to_coin)).first() if w_from_coin: if w_from_coin.amount > from_num: w_from_coin.amount -= from_num else: session.delete(w_from_coin) if w_to_coin: w_to_coin.amount += to_num else: print "adding {} {}".format(to_num, to_coin) new_wallet = Wallet(user, to_coin, to_num) session.add(new_wallet) session.commit() message = "You traded {} {} for {} {}".format( from_num, from_coin.symbol, to_num, to_coin.symbol) else: pass return message
def add_coin(user, input): num, coin_string = get_num_coins(input) coin = get_coin_from_input(coin_string) wallet = session.query(Wallet).filter((Wallet.user == user) &\ (Wallet.coin == coin)).first() # if the user has this coin simply add to it, otherwise set it up if wallet: wallet.amount += num session.commit() else: wallet = Wallet(user, coin, num) session.add(wallet) session.commit() return "{} {} added".format(round(num, 3), coin.symbol)
def get_or_create_user(user_data): t_id = user_data.id first_name = user_data.first_name last_name = user_data.last_name try: username = user_data.username except KeyError: username = None if session.query(exists().where(User.telegram_id == t_id)).scalar(): # get user user = session.query(User).filter(User.telegram_id == t_id).first() else: # create a new user user = User(t_id, first_name, last_name, username) session.add(user) session.commit() session.flush() return user
def remove_coin(user, input): num, coin_string = get_num_coins(input) coin = get_coin_from_input(coin_string) wallet = session.query(Wallet).filter((Wallet.user == user) &\ (Wallet.coin == coin)).first() if wallet: if num == "all": return_string = "You have sold all of your {}".format(coin.symbol) session.delete(wallet) session.commit() return return_string else: wallet.amount -= num session.commit() return "You have sold {} of your {}. {} {} remaining".format( round(num, 3), coin.symbol, round(wallet.amount, 3), coin.symbol) else: # dont remove someting thats not there return "You have no {} to remove you big dumb baby".format(coin.symbol)
def refresh(bot, update): logging.log(logging.INFO, "Deleting Coins From DB") session.query(Coin).delete() session.commit() update.message.reply_text('Bot is restarting...') Thread(target=stop_and_restart).start()
def set_bio(self, s): self.bio = s session.commit()