Greenadine

EchoBot economy.py

Nov 12th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.51 KB | None | 0 0
  1. from discord.ext import commands
  2. import json
  3.  
  4. class Economy(commands.Cog):
  5.  
  6.     def __init__(self, bot):
  7.         self.bot = bot
  8.         self.users = self.load()
  9.  
  10.     # e!balance - Check Gold balance
  11.     @commands.command(pass_context=True)
  12.     async def balance(self, ctx):
  13.         """Check your current Gold balance."""
  14.         self.user_register(ctx.message.author)
  15.  
  16.         await ctx.send("{} You have {} Gold.".format(ctx.message.author.mention, self.user_balance(ctx.message.author)))
  17.  
  18.     # Economy functions #
  19.  
  20.     # Returns whether user is already in data dictionary.
  21.     def user_has_data(self, user):
  22.         return user.id in self.users
  23.  
  24.     # Add user id to dictionary if missing.
  25.     def user_register(self, user):
  26.         if not self.user_has_data(user):
  27.             self.users[user.id] = {'id': user.id, 'name': user.name, 'balance': 100}
  28.             self.save()
  29.  
  30.     def user_balance(self, user):
  31.         if self.user_has_data(user):
  32.             return self.users[user.id]['balance']
  33.         else:
  34.             return None
  35.  
  36.     # Data functions #
  37.  
  38.     # Save all user data to file.
  39.     def save(self):
  40.         file = open('users.json', 'w+')
  41.         json.dump(self.users, file, indent=4)
  42.  
  43.         file.close()
  44.  
  45.     # Load user data from file into dictionary.
  46.     def load(self):
  47.         file = open('users.json', 'r')
  48.  
  49.         data = json.load(file)
  50.         file.close()
  51.  
  52.         return data
  53.  
  54. def setup(bot):
  55.     bot.add_cog(Economy(bot))
  56.     print("Loaded economy system.")
Add Comment
Please, Sign In to add comment