Guest User

Untitled

a guest
Jun 17th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. from discord.ext import commands
  2. import json
  3.  
  4. bot = commands.Bot('!')
  5.  
  6. amounts = {}
  7.  
  8. @bot.event
  9. async def on_ready():
  10. global amounts
  11. try:
  12. with open('amounts.json') as f:
  13. amounts = json.load(f)
  14. except FileNotFoundError:
  15. print("Could not load amounts.json")
  16. amounts = {}
  17.  
  18. @bot.command(pass_context=True)
  19. async def balance(ctx):
  20. id = ctx.author.id
  21. if id in amounts:
  22. await bot.say("You have {} in the bank".format(amounts[ctx.author.id]))
  23. else:
  24. await bot.say("You do not have an account")
  25.  
  26. @bot.command(pass_context=True)
  27. async def register(ctx):
  28. id = ctx.author.id
  29. if id not in amounts:
  30. amounts[id] = 100
  31. await bot.say("You are now registered")
  32. else:
  33. await bot.say("You already have an account")
  34.  
  35. @bot.command(pass_context=True)
  36. async def transfer(ctx, amount: int, other: discord.Member):
  37. primary_id = ctx.author.id
  38. other_id = other.id
  39. if primary_id not in amounts:
  40. await bot.say("You do not have an account")
  41. elif other_id not in amounts:
  42. await bot.say("The other party does not have an account")
  43. elif amounts[primary_id] < amount:
  44. await bot.say("You cannot afford this transaction")
  45. else:
  46. amounts[primary_id] -= amount
  47. amounts[other_id] += amount
  48. await bot.say("Transaction complete")
  49.  
  50. @bot.command()
  51. async def save():
  52. with open('amounts.json', 'w+') as f:
  53. json.dump(amounts, f)
  54.  
  55. bot.run("TOKEN")
Add Comment
Please, Sign In to add comment