Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from discord.ext import commands
- import json
- class Economy(commands.Cog):
- def __init__(self, bot):
- self.bot = bot
- self.users = self.load()
- # e!balance - Check Gold balance
- @commands.command(pass_context=True)
- async def balance(self, ctx):
- """Check your current Gold balance."""
- self.user_register(ctx.message.author)
- await ctx.send("{} You have {} Gold.".format(ctx.message.author.mention, self.user_balance(ctx.message.author)))
- # Economy functions #
- # Returns whether user is already in data dictionary.
- def user_has_data(self, user):
- return user.id in self.users
- # Add user id to dictionary if missing.
- def user_register(self, user):
- if not self.user_has_data(user):
- self.users[user.id] = {'id': user.id, 'name': user.name, 'balance': 100}
- self.save()
- def user_balance(self, user):
- if self.user_has_data(user):
- return self.users[user.id]['balance']
- else:
- return None
- # Data functions #
- # Save all user data to file.
- def save(self):
- file = open('users.json', 'w+')
- json.dump(self.users, file, indent=4)
- file.close()
- # Load user data from file into dictionary.
- def load(self):
- file = open('users.json', 'r')
- data = json.load(file)
- file.close()
- return data
- def setup(bot):
- bot.add_cog(Economy(bot))
- print("Loaded economy system.")
Add Comment
Please, Sign In to add comment