Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. import discord
  2. from discord.ext import commands
  3. import json
  4. import string
  5. import os.path
  6.  
  7. token = 'NjIzOTg2Njg5NDYzMDI1Njk0.XYKbAQ.5u-IY584CrUi3HReM7xe3MXZZDQ'
  8.  
  9. bot = commands.Bot(command_prefix='!')
  10.  
  11.  
  12. @bot.command(name='charge', help='Total amount is charged to each person. Built in state of the art thief protection')
  13. async def charge(ctx, amount: float):
  14. if amount <= 0:
  15. ctx.message.add_reaction('<:no:426147595786649613>')
  16. await ctx.send('Positive values only')
  17. return
  18. for mention in ctx.message.mentions:
  19. transfer(ctx.author.name, mention.name, amount)
  20. await ctx.message.add_reaction('👍')
  21.  
  22.  
  23.  
  24. @bot.command(name='split', help='Amount is split between each person. Built in state of the art thief protection')
  25. async def split(ctx, amount: float):
  26. if amount <= 0:
  27. ctx.message.add_reaction('<:no:426147595786649613>')
  28. await ctx.send('Positive values only')
  29. return
  30. for mention in ctx.message.mentions:
  31. transfer(ctx.author.name, mention.name, amount/len(ctx.message.mentions))
  32. await ctx.message.add_reaction('👍')
  33.  
  34.  
  35. @bot.command(name='credit', help='Pays the amount to that person')
  36. async def credit(ctx, amount: float):
  37. if amount <= 0:
  38. ctx.message.add_reaction('<:no:426147595786649613>')
  39. await ctx.send('Positive values only')
  40. return
  41. for mention in ctx.message.mentions:
  42. transfer(mention.name, ctx.author.name, amount)
  43. await ctx.message.add_reaction('👍')
  44.  
  45.  
  46. @bot.command(name='balance')
  47. async def balance(ctx):
  48. filepath = "%s.json" % ctx.author.name
  49. if os.path.exists(filepath):
  50. await ctx.send("No transactions found for %s" % ctx.author.name)
  51. else:
  52. with open("%s.json" % ctx.author.name) as json_file:
  53. data = json.load(json_file)
  54. await ctx.send("Balance for %s " % ctx.author.name + json.dumps(data))
  55.  
  56.  
  57. @bot.event
  58. async def on_ready():
  59. channel = bot.get_channel(620834829700825108)
  60. await channel.send('You gotta spend maney to make maney')
  61. print('Bot online')
  62.  
  63.  
  64. def transfer(nameToCredit: string, nameToCharge: string, amount: float):
  65. nameToCreditJsonFilePath = "%s.json" % nameToCredit
  66. nameToChargeJsonFilePath = "%s.json" % nameToCharge
  67.  
  68. if os.path.exists(nameToCreditJsonFilePath):
  69. with open(nameToCreditJsonFilePath) as read_file:
  70. data = json.load(read_file)
  71. data[nameToCharge] -= amount
  72. with open(nameToCreditJsonFilePath, 'w') as write_file:
  73. json.dump(data, write_file)
  74. else:
  75. with open(nameToCreditJsonFilePath, 'w+') as write_file:
  76. data = {nameToCharge: amount * -1}
  77. json.dump(data, write_file)
  78.  
  79. if os.path.exists(nameToChargeJsonFilePath):
  80. with open(nameToChargeJsonFilePath) as read_file:
  81. data = json.load(read_file)
  82. data[nameToCredit] += amount
  83. with open(nameToChargeJsonFilePath, 'w') as write_file:
  84. json.dump(data, write_file)
  85. else:
  86. with open(nameToChargeJsonFilePath, 'w+') as write_file:
  87. data = {nameToCredit: amount}
  88. json.dump(data, write_file)
  89.  
  90.  
  91. bot.run(token)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement