0

I can't wrap my head around how to let a user bet X amount of "XP"(i use dollars) inside of a command. I've posted the simple coinflip command below with what I think should be the logic, but I am not 100% sure if I'm on the right track. I was wondering how I could call get_dollars for the user when they bet a random amount of money. I'm guessing that I will need to create something such as betamount = enter authors bet amount but I'm drawing a blank on how to handle the random amount they may put rather than hardcoding a fixed amount that forces the user to use.

client = discord.Client()

try:
    with open("cash.json") as fp:
        cash = json.load(fp)
except Exception:
    cash = {}

def save_cash():
    with open("cash.json", "w+") as fp:
        json.dump(cash, fp, sort_keys=True, indent=4)

def get_dollars(user: discord.User):
    id = user.id
    if id in cash:
        return cash[id].get("dollars", 0)
    return 0

@client.event
async def on_message(message):
    betamount = ???
    if message.content.lower().startswith('!coinflip'):
        if get_dollars(message.author) < 0:
            await client.send_message(message.channel, "{} you don't have enough money to bet.".format(message.author.mention))
        else:
            choice = random.randint(0,1)
            if choice == 0
                await client.add_reaction(message, '⚫')
                await client.send_message(message.channel, "The coin handed on heads!)
                if 'heads' in message.content:
                    await client.send_message(message.channel, "You've won ${}".format(betamount))
                    add_dollars(message.author, betamount)
                else:
                    if 'tails' in message.content:
                        await client.send_message(message.channel, "You've lost ${}".format(betamount))
                        remove_dollars(message.author, betamount)
            elif choice == 1:
                await client.add_reaction(message, '⚪')
                await client.send_message(message.channel, "The coin handed on tails!")
                if 'tails' in message.content:
                    await client.send_message(message.channel, "You've won ${}".format(betamount))
                    add_dollars(message.author, betamount)
                else:
                    if 'heads' in message.content:
                        await client.send_message(message.channel, "You've lost ${}".format(betamount))
                        remove_dollars(message.author, betamount)
  • Are they supposed to call your command like `!flipcoin 2.50`? – Patrick Haugh Sep 01 '18 at 16:42
  • Correct, something like "!coinflip heads 23", of course ill add a check to see if the bet amount is larger than their total in get_dollars –  Sep 02 '18 at 05:16

2 Answers2

0

If you are using a command like !coinflip (heads/tails) (amount) then you could use x = message.content.split(" ") to split the message into a list. With this you could do something like outcome = x[1] and betamount = x[2].

I would also recommend that you then change if 'tails' in message.content: to if outcome == 'tails'. Otherwise a user could do something like !coinflip heads (amount) tails which would award them cash every time.

Mehvix
  • 296
  • 3
  • 12
0

If you're getting into commands with arguments, (espescially once those arguments become types like discord.Member), I would heavily recommend switching to using the discord.ext.commands extension. It makes writing commands much simpler, and it means you get to move code out of the on_message event. Here's how your command would look using commands:

from discord.ext import commands

bot = commands.Bot(command_prefix='!')

@bot.command(pass_context=True)
async def coinflip(ctx, guess: str, amount: float):
    guesses = ('heads', 'tails')
    guess = guess.lower()
    if guess not in guesses:
        await bot.say("Invalid guess.")
        return
    author = ctx.message.author
    balance = get_dollars(author)
    if balance < amount:
        await bot.say(f"You don't have that much money.  Your balance is ${balance:.2f}")
        return
    result = random.sample(guesses)
    if result == guess:
        await bot.say("You won!")
        add_dollars(author, amount)
    else:
        await bot.say("You lost!")
        remove_dollars(author, amount)

bot.run("TOKEN")

Also keep in mind that if you start using commands you have to add await bot.process_commands(message) to the bottom of your on_message event

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • I was afraid I was going to have to change everything to this format. I guess it will be worth it in the long wrong, thanks so much for the response! –  Sep 03 '18 at 07:14