Пример #1
0
async def _tip(ctx,
               amount,
               sender: discord.User = None,
               receiver: discord.User = None):
    """ Tips a user <amount> of coin """

    err_embed = discord.Embed(title=":x:Error:x:",
                              colour=discord.Colour(0xf44242))
    good_embed = discord.Embed(title="You were tipped!",
                               colour=discord.Colour(0xD4AF37))
    request_desc = "Register with `{}registerwallet <youraddress>` to get started! To create a wallet head to https://turtlecoin.lol/wallet/".format(
        config['prefix'])
    request_embed = discord.Embed(title="{} wants to tip you".format(
        ctx.message.author.name),
                                  description=request_desc)

    if not sender:  # regular tip
        sender = ctx.message.author

    if not receiver:
        tipees = ctx.message.mentions
    else:
        tipees = [
            receiver,
        ]

    try:
        amount = int(round(float(amount) * config['units']))
    except:
        await client.say("Amount must be a number > {}".format(
            10 / config['units']))
        return False

    if amount <= 10:
        err_embed.description = "`amount` must be greater than {}".format(
            10 / config['units'])
        await client.say(embed=err_embed)
        return False

    fee = get_fee(amount)
    self_exists = session.query(Wallet).filter(
        Wallet.userid == sender.id).first()

    if not self_exists:
        err_embed.description = "You haven't registered a wallet!"
        err_embed.add_field(
            name="Help",
            value="Use `{}registerwallet <addr>` before trying to tip!".format(
                config['prefix']))
        await client.send_message(sender, embed=err_embed)
        return False

    pid = gen_paymentid(self_exists.address)
    balance = session.query(TipJar).filter(TipJar.paymentid == pid).first()
    if not balance:
        t = TipJar(pid, sender.id, 0)
        session.add(t)
        session.commit()
        err_embed.description = "You are now registered, please `{}deposit` to tip".format(
            config['prefix'])
        await client.send_message(sender, embed=err_embed)
        return False

    if balance.amount < 0:
        balance.amount = 0
        session.commit()
        err_embed.description = "Your balance was negative!"
        await client.send_message(sender, embed=err_embed)

        madk = discord.utils.get(client.get_all_members(),
                                 id='200823661928644617')
        err_embed.title = "{} had a negative balance!!".format(sender.name)
        err_embed.description = "PID: {}".format(pid)

        await client.send_message(madk, embed=err_embed)
        return False

    if ((len(tipees) * (amount)) + fee) > balance.amount:
        err_embed.description = "Your balance is too low! Amount + Fee = `{}` {}".format(
            ((len(tipees) * (amount)) + fee) / config['units'],
            config['symbol'])
        await client.add_reaction(ctx.message, "\u274C")
        await client.send_message(sender, embed=err_embed)
        return False

    destinations = []
    actual_users = []
    failed = 0
    for user in tipees:
        user_exists = session.query(Wallet).filter(
            Wallet.userid == user.id).first()
        if user_exists:
            destinations.append({
                'amount': amount,
                'address': user_exists.address
            })
            if user_exists.userid != sender.id:  # multitip shouldn't tip self.
                actual_users.append(user)
        else:
            failed = failed + 1
            await client.add_reaction(ctx.message, EMOJI_SOS)
            try:
                await client.send_message(user, embed=request_embed)
            except:
                continue

    if len(destinations) == 0:
        await client.add_reaction(ctx.message, EMOJI_SOS)
        return False

    transfer = build_transfer(amount, destinations, balance)
    print(transfer)
    result = rpc.sendTransaction(transfer)
    print(result)

    await client.add_reaction(ctx.message, EMOJI_MONEYBAGS)

    balance.amount -= ((len(actual_users) * amount) + fee)
    tx = Transaction(result['transactionHash'],
                     (len(actual_users) * amount) + fee, balance.paymentid)
    session.add(tx)
    session.commit()
    good_embed.title = "Tip Sent!"
    good_embed.description = (
        "Sent `{0:,.2f}` {1} to {2} users! With Transaction Hash ```{3}```".
        format(amount / config['units'], config['symbol'], len(actual_users),
               result['transactionHash']))
    good_embed.url = (
        "https://blocks.turtle.link/?hash={}#blockchain_transaction".format(
            result['transactionHash']))

    good_embed.add_field(name="New Balance",
                         value="`{:0,.2f}` {}".format(
                             balance.amount / config['units'],
                             config['symbol']))
    good_embed.add_field(
        name="Transfer Info",
        value="Successfully sent to {0} users. {1} failed.".format(
            len(actual_users), failed))
    try:
        await client.send_message(sender, embed=good_embed)
    except:
        pass

    for user in actual_users:
        good_embed = discord.Embed(title="You were tipped!",
                                   colour=discord.Colour(0xD4AF37))
        good_embed.description = (
            "{0} sent you `{1:,.2f}` {2} with Transaction Hash ```{3}```".
            format(sender.mention, amount / config['units'], config['symbol'],
                   result['transactionHash']))
        good_embed.url = (
            "https://blocks.turtle.link/?hash={}#blockchain_transaction".
            format(result['transactionHash']))
        try:
            await client.send_message(user, embed=good_embed)
        except:
            continue
    return True
Пример #2
0
async def tip(ctx, amount, user: discord.User = None):
    """ Tips a user <amount> TRTL """
    if not user:
        await client.say("Usage: !tip <amount> @username")

    err_embed = discord.Embed(title=":x:Error:x:",
                              colour=discord.Colour(0xf44242))
    request_desc = "Register with `!registerwallet TRTLyourwallet` to get started!"
    request_embed = discord.Embed(title="{} wants to tip you".format(
        ctx.message.author.mention),
                                  description=request_desc)
    good_embed = discord.Embed(title="You were tipped!",
                               colour=discord.Colour(0xD4AF37))

    try:
        amount = int(round(float(amount) * 100))
    except:
        if user:
            await client.say("Usage: !tip <amount> @username")
        else:
            await client.say("Usage: !tip <amount> @username")
    if amount <= 1:
        err_embed.description = "`amount` must be greater than 1"
        await client.say(ember=err_embed)
        return
    user_exists = session.query(Wallet).filter(
        Wallet.userid == user.id).first()
    self_exists = session.query(Wallet).filter(
        Wallet.userid == ctx.message.author.id).first()
    if user.id == ctx.message.author.id:
        err_embed.description = "You cannot tip yourself!"
        await client.say(embed=err_embed)
        return
    if self_exists and amount < 50000000 and user_exists:
        pid = gen_paymentid(self_exists.address)
        balance = session.query(TipJar).filter(TipJar.paymentid == pid).first()
        print(balance)
        if not balance:
            t = TipJar(pid, ctx.message.author.id, 0)
            session.add(t)
            session.commit()
            err_embed.description = "You are now registered, please `!deposit` to tip"
            await client.send_message(ctx.message.author, embed=err_embed)

        else:
            fee = get_fee(amount)
            if balance.amount < 0:
                balance.amount = 0
                session.commit()
                err_embed.description = "Your balance was negative!"
                await client.send_message(ctx.message.author,
                                          "It has been reset to zero")
                return
            if amount + fee > balance.amount:
                err_embed.description = "Your balance is too low! Amount + Fee = `{}` TRTLs".format(
                    (amount + fee) / 100)
                await client.send_message(
                    ctx.message.author,
                    "You have `{0:,.2f}` TRTLs".format(balance.amount / 100))
                return
            else:
                transfer = build_transfer(user_exists.address, amount,
                                          self_exists.address, balance)
                print(transfer)
                result = rpc.sendTransaction(transfer)
                print(result)
                if (balance.amount - amount + fee) < 0:
                    print("ERROR! Balance corrupted")
                    balance.amount = 0
                    return
                try:
                    session.commit()
                except:
                    session.rollback()
                    raise
                await client.say("Sent `{0:,.2f}` TRTLs".format(amount / 100))
                good_embed.description = "{} sent you `{}` TRTLs with Transaction Hash ```{}```".format(
                    ctx.message.author.mention, amount / 100,
                    result['transactionHash'])
                good_embed.url = "https://blocks.turtle.link/?hash={}#blockchain_transaction".format(
                    result['transactionHash'])
                await client.send_message(user, embed=good_embed)

                balance.amount -= amount + fee
                tx = Transaction(result['transactionHash'])
                session.add(tx)
                session.commit()
                return
    elif amount > int(rpc.getBalance()['availableBalance']):
        err_embed.description = "Too many coins are locked, please wait."
    elif amount > 50000000:
        err_embed.description = "Transactions must be under 500k TRTLs!"
    elif not user_exists:
        err_embed.description = "{} hasn't registered to be tipped!".format(
            user.name)
        await client.send_message(user, embed=request_embed)
    else:
        err_embed.description = "You haven't registered a wallet!"
        err_embed.add_field(
            name="Help",
            value="Use `!registerwallet <addr>` before trying to tip!")
    await client.say(embed=err_embed)