async def send( guild, author, from_wallet, to_wallet, amount ):

    if not methods.can_access_wallet(guild, author.id, f'<@{from_wallet["id"]}>'):
        return (False, "cannot access wallet")
    currency=""
    if "-" in amount:
        currency=f'-{amount.split("-")[1]}'
        amount =amount.split("-")[0]
    percent = False
    if "%" in amount:
        percent = True
        amount =amount.split("%")[0]
    try:
        amount = int(amount)
    except:
        return (False,"invalid amount" )
    guild_collection =db[str(guild.id)]
    print("from wallet", from_wallet)
    print("balance is ",f'balance{currency}')
    print(from_wallet[f'balance{currency}'])
    if f'balance{currency}' not in from_wallet:
        return (False, "you do not have this currency")
    if percent:
        amount = math.floor(from_wallet[f'balance{currency}']*(amount/100))
    if(from_wallet[f'balance{currency}'] > amount):
        guild_collection.update_one(
            {"id":  from_wallet["id"] },
            { "$inc":{f'balance{currency}':-amount} }
        )
        guild_collection.update_one(
            {"id":  to_wallet["id"] },
            { "$inc":{f'balance{currency}':amount} }
        )
        log_money(guild,f'{author.mention} sent {amount} from <@{from_wallet["id"] }> to <@{to_wallet["id"]}>')
        return (True, "successful")
    else:
        return (False, f'insuffiecent funds for transfer.')
Exemplo n.º 2
0
 async def fulfill_trade(self, ctx, wallet, message):
     guild = ctx.guild
     person = ctx.author
     found_wallet = methods.get_wallet(guild, wallet)
     if not found_wallet[0]:
         return await ctx.send(embed=simple_embed(False, "cant find wallet")
                               )
     if not methods.can_access_wallet(guild, person.id, wallet):
         return await ctx.send(
             embed=simple_embed(False, "cannot access wallet"))
     try:
         message = int(message)
     except:
         return await ctx.send(embed=simple_embed(False, "invalid number"))
     guild_collection = db[str(guild.id)]
     found_offer = guild_collection.find_one({
         "type": "trade",
         "message_id": message
     })
     if found_offer is None:
         return await ctx.send(embed=simple_embed(False, "can't find offer")
                               )
     if "uses" in found_offer:
         if found_offer["uses"] <= 0:
             guild_collection.delete_one(
                 {"message_id": found_offer["message_id"]})
             return await ctx.send(
                 embed=simple_embed(False, "offer has been used up"))
     if "offer_time" in found_offer:
         if time.time() > found_offer["offer_time"]:
             guild_collection.delete_one(
                 {"message_id": found_offer["message_id"]})
             return await ctx.send(
                 embed=simple_embed(False, "offer has run out of time"))
     if "people_restrictions" in found_offer:
         if person.id not in methods.whois(
                 found_offer["people_restrictions"], guild):
             return await ctx.send(embed=simple_embed(
                 False,
                 "you cannot accept this offer because a restriction has bee"
             ))
     receiver = methods.find_create(found_wallet[1].id, ctx.guild)
     if f'balance{found_offer["cost_currency"]}' not in receiver:
         return await ctx.send(
             embed=simple_embed(False, "you have no money"))
     if receiver[f'balance{found_offer["cost_currency"]}'] < found_offer[
             "cost_amount"]:
         return await ctx.send(
             embed=simple_embed(False, "you don't have enough money"))
     sender = guild_collection.find_one({"id": found_offer["person"]})
     if "wallet" in found_offer:
         if f'balance{found_offer["offer_currency"]}' not in sender:
             guild_collection.delete_one(
                 {"message_id": found_offer["message_id"]})
             return await ctx.send(embed=simple_embed(
                 False,
                 "offer deleted, person does not have enough for trade"))
         if sender[f'balance{found_offer["offer_currency"]}'] < found_offer[
                 "offer_amount"]:
             guild_collection.delete_one(
                 {"message_id": found_offer["message_id"]})
             return await ctx.send(embed=simple_embed(
                 False,
                 "offer deleted, person does not have enough for trade"))
     guild_collection.update_one({"id": receiver["id"]}, {
         "$inc": {
             f'balance{found_offer["offer_currency"]}':
             -int(found_offer["offer_amount"])
         }
     })
     guild_collection.update_one({"id": receiver["id"]}, {
         "$inc": {
             f'balance{found_offer["cost_currency"]}':
             int(found_offer["cost_amount"])
         }
     })
     if " wallet" in found_offer:
         guild_collection.update_one({"id": sender["id"]}, {
             "$inc": {
                 f'balance{found_offer["offer_currency"]}':
                 int(found_offer["offer_amount"])
             }
         })
         guild_collection.update_one({"id": sender["id"]}, {
             "$inc": {
                 f'balance{found_offer["cost_currency"]}':
                 -int(found_offer["cost_amount"])
             }
         })
     if re.match(r'<@&?\d{18}>', found_offer["offer_currency"]) is not None:
         try:
             loop = asyncio.get_event_loop()
             id_role = re.findall(r'\d{18}',
                                  found_offer["offer_currency"])[0]
             print(id_role)
             id_role = int(id_role)
             role = discord.utils.get(guild.roles, id=id_role)
             print(role)
             loop.create_task(person.add_roles(role))
         except:
             return await ctx.send(
                 embed=simple_embed(False, "bad permissions"))
     if "uses" in found_offer:
         guild_collection.update_one(
             {"message_id": found_offer["message_id"]},
             {"$inc": {
                 "uses": -1
             }})
     return await ctx.send(embed=simple_embed(True, "success"))
Exemplo n.º 3
0
 async def insert_trade(self, ctx, wallet, offer, cost, *options):
     person = ctx.author
     guild = ctx.guild
     message = ctx.message
     if wallet != "admins":
         found_wallet = methods.get_wallet(guild, wallet)
         if not found_wallet[0]:
             return await ctx.send(embed=simple_embed(False, "bad wallet"))
         if not methods.can_access_wallet(guild, person.id, wallet):
             return await ctx.send(
                 embed=simple_embed(False, "bad wallet 2"))
     offer_currency = ""
     offer_amount = offer
     if "-" in offer:
         offer_currency = f'-{offer.split("-")[1]}'
         offer_amount = offer.split("-")[0]
         if not methods.valid_item(offer_currency[1:]):
             return await ctx.send(
                 embed=simple_embed(False, "invalid item name"))
     cost_currency = ""
     cost_amount = cost
     if "-" in cost:
         cost_currency = f'-{cost.split("-")[1]}'
         cost_amount = cost.split("-")[0]
         if not methods.valid_item(cost_currency[1:]):
             return await ctx.send(
                 embed=simple_embed(False, "invalid item name"))
     if re.match(r'<@&?\d{18}>', offer) is not None:
         offer_currency = offer
         offer_amount = "1"
     print(offer_amount, cost_amount)
     if not offer_amount.isdigit() or not cost_amount.isdigit():
         return await ctx.send(embed=simple_embed(False, "invalid amount"))
     offer_amount = int(offer_amount)
     cost_amount = int(cost_amount)
     for index, option in enumerate(options):
         if "use" in option:
             if not option.split("use")[0].isdigit():
                 return await ctx.send(
                     embed=simple_embed(False, "invalid uses"))
             uses = option.split("use")[0]
         if "time" in option:
             if not option.split("time")[0].isdigit():
                 return await ctx.send(
                     embed=simple_embed(False, "invalid time"))
             offer_time = int(option.split("time")[0]) + time.time()
         if option == "whois":
             people_restrictions = options[index:]
     guild_collection = db[str(guild.id)]
     offer_schema = {
         "type": "trade",
         "person": person.id,
         "message_id": message.id,
         "offer_currency": offer_currency,
         "offer_amount": int(offer_amount),
         "cost_currency": cost_currency,
         "cost_amount": int(cost_amount)
     }
     if "found_wallet" in locals():
         offer_schema["wallet"] = found_wallet[1].id
     if "uses" in locals():
         offer_schema["uses"] = int(uses)
     if "offer_time" in locals():
         offer_schema["offer_time"] = int(offer_time)
     if "people_restrictions" in locals():
         offer_schema["people_restrictions"] = people_restrictions
     guild_collection.insert_one(offer_schema)
     return await ctx.send(embed=simple_embed(
         True,
         f'succesful. In order to accept this trade, type "$accept {message.id} (ping wallet)", or you may react to the original message with ✅, in which case the money will be deducted from your personal account.'
     ))
Exemplo n.º 4
0
def send(person_id, guild, from_wallet, to_wallet, amount):
    # #print("send")
    #print("to_wallet is",to_wallet)
    if not methods.can_access_wallet(guild, person_id, from_wallet):
        return (False, "cannot access wallet")
    currency = ""
    if "-" in amount:
        currency = f'-{amount.split("-")[1]}'
        amount = amount.split("-")[0]
    percent = False
    if "%" in amount:
        percent = True
        amount = amount.split("%")[0]
    try:
        amount = int(amount)
    except:
        return (False, "invalid amount")
    guild_collection = db[str(guild.id)]
    from_wallet_id = methods.get_wallet(guild, from_wallet)
    to_wallet_id = methods.get_wallet(guild, to_wallet)
    #print(to_wallet_id,from_wallet_id)
    if (from_wallet_id[0] and to_wallet_id[0]):
        sender_account = guild_collection.find_one(
            {"id": from_wallet_id[1].id})
        reciever_account = guild_collection.find_one(
            {"id": to_wallet_id[1].id})
        if (sender_account is None):
            c_result = create(guild, from_wallet)
            if not c_result[0]:
                return (False, c_result[1])
            sender_account = c_result[2]
        if (reciever_account is None):
            c_result = create(guild, to_wallet)
            if not c_result[0]:
                return (False, c_result[1])
            reciever_account = c_result[2]

        if f'balance{currency}' not in sender_account:
            return (False, "you do not have this currency")
        if percent:
            amount = math.floor(sender_account[f'balance{currency}'] *
                                (amount / 100))
        if (sender_account[f'balance{currency}'] > amount):
            guild_collection.update_one(
                {"id": sender_account["id"]},
                {"$inc": {
                    f'balance{currency}': -amount
                }})
            guild_collection.update_one(
                {"id": reciever_account["id"]},
                {"$inc": {
                    f'balance{currency}': amount
                }})
            log_money(
                guild,
                f'<@{person_id}> sent {amount} from {from_wallet_id[1].mention } to {to_wallet_id[1].mention}'
            )
            return (
                True,
                f'transfer successful. you send {amount}, making your balance '
                + str(sender_account[f'balance{currency}']) + f' {currency}')

        else:
            return (False, f'insuffiecent funds for transfer.')
    else:
        return (False, "cannot find wallet")

    pass