async def give(self, ctx, amount: float, target: discord.Member): """Give gil to another user, with a 1 percent tax deduction..""" if amount < 0: await ctx.send(CMD_GIVE_INVALID_AMOUNT) return if target == ctx.author: await ctx.send( "**{0.mention}, you can't transfer money to yourself.**") credit = amount * (1 - TRANSFER_TAX) debit = amount target_account = EconomyAccount.get_economy_account( target, self.bot.db_session) author_account = EconomyAccount.get_economy_account( ctx.author, self.bot.db_session) if not author_account.has_balance(debit): await ctx.send( CMD_GIVE_INSUFFICIENT_AMOUNT.format( ctx.author, target_account.get_balance())) return target_account.add_credit(self.bot.db_session, credit, "T:{}".format(ctx.author.id)) author_account.add_debit(self.bot.db_session, debit, "T:{}".format(target.id)) await ctx.send(CMD_GIVE_SUCCESS.format(ctx.author, credit, target))
async def setfarmname(self, ctx, name): """Show target's farm details.""" if len(name) > 32: await ctx.send(MSG_FARM_RENAME_NAME_TOO_LONG.format(ctx.author)) _farm = ORMFarm.get_farm(ctx.author, self.bot.db_session) _farm.name = name _account = EconomyAccount.get_economy_account(ctx.author, self.bot.db_session) if not _account.has_balance(FARM_NAME_CHANGE_PRICE, raw=True): await ctx.send( MSG_INSUFFICIENT_FUNDS_EXTRA.format( ctx.author, _account.get_balance(), FARM_NAME_CHANGE_PRICE / 10000)) return _account.add_debit(self.bot.db_session, FARM_NAME_CHANGE_PRICE, name="FARM NAME CHANGE", raw=True) self.bot.db_session.add(_farm) self.bot.db_session.commit() await ctx.send( MSG_FARM_RENAME_SUCCESS.format(ctx.author, name, _account.get_balance()))
async def farmsell(self, ctx, plant_name): """Sell all of your target crop.""" _plant = Plant.get_plant(self.bot.db_session, plant_name) if _plant is None: await ctx.send(MSG_PLANT_NOT_FOUND.format(ctx.author)) return _farm = ORMFarm.get_farm(ctx.author, self.bot.db_session) _account = EconomyAccount.get_economy_account(ctx.author, self.bot.db_session) total_amount = 0 for i in range(len(_farm.harvests))[::-1]: if _farm.harvests[i].plant.id == _plant.id: total_amount += _farm.harvests[i].amount del _farm.harvests[i] if total_amount == 0: await ctx.send(MSG_SELL_NONE.format(ctx.author, _plant.name)) return plant_sell_price = _plant.get_sell_price(raw=True) raw_credit = total_amount * plant_sell_price _account.add_credit(self.bot.db_session, raw_credit, name="S:{0.id}={1}".format(_plant, total_amount), raw=True) _plant.decrement_demand(self.bot.db_session, total_amount) _farm.decrease_storage(self.bot.db_session, total_amount) await ctx.send( MSG_SELL_SUCCESS.format(ctx.author, total_amount, _plant, raw_credit / 10000, _account.get_balance(), plant_sell_price / 10000))
async def plotbuy(self, ctx, up_count: int = 1): """Buy new N plots. (N <= 1M)""" up_count = max(1, up_count) up_count = min(1000000, up_count) _farm = ORMFarm.get_farm(ctx.author, self.bot.db_session) _account = EconomyAccount.get_economy_account(ctx.author, self.bot.db_session) plot_count = _farm.get_plot_count() to_max_plots = FARM_PLOTS_MAX - plot_count if to_max_plots == 0: await ctx.send( "**{0.mention}, you have reached maximum plot count.**".format( ctx.author)) return up_count = min(up_count, to_max_plots) price = _farm.get_next_plot_price(raw=True, up_count=up_count) if _account.has_balance(price, raw=True): _farm.add_plot(self.bot.db_session, up_count=up_count) _account.add_debit(self.bot.db_session, price, name="PLOTBUY", raw=True) await ctx.send( MSG_PLOT_BUY_SUCCESS.format(ctx.author, _account.get_balance(), up_count)) else: await ctx.send( MSG_INSUFFICIENT_FUNDS_EXTRA.format(ctx.author, _account.get_balance(), price / 10000))
async def gil(self, ctx, target: discord.Member = None): """Returns target account's balance.""" if target is None: target = ctx.author # Get current economy account account = EconomyAccount.get_economy_account(target, self.bot.db_session) await ctx.send(CMD_GIL.format(target, account.get_balance()))
async def registerall(self, ctx): """(Owner) Gives all users in guild economy accounts.""" registered = 0 for member in ctx.guild.members: k = EconomyAccount.get_economy_account(member, self.bot.db_session, create_if_not_exists=False) if k is None: k = EconomyAccount.create_economy_account( member, self.bot.db_session, member.bot or member.system, commit_on_execution=False) registered += 1 self.bot.db_session.commit() logging.info( "Registered {0} new accounts in the Economy database.".format( registered))
async def purgeaccounts(self, ctx): """(Owner) Purge all accounts with transactions less than 5.""" all_accounts = EconomyAccount.get_all_economy_accounts( self.bot.db_session) for _account in all_accounts: if len(_account.transactions) <= 5: logging.info("Deleting {}...".format(_account)) self.bot.db_session.delete(_account) await ctx.send("**Purging finished.**")
async def admingiveall(self, ctx, amount: float): """(Owner) Grant everyone gil.""" if amount < 0: await ctx.send(CMD_GIVE_INVALID_AMOUNT) return all_accounts = EconomyAccount.get_all_economy_accounts( self.bot.db_session) for _account in all_accounts: _account.add_credit(self.bot.db_session, amount, "Admin grant.") await ctx.send("**Compensation finished.**")
async def admingive(self, ctx, amount: float, target: discord.Member = None): """(Owner) Grant target gil.""" if amount < 0: await ctx.send(CMD_GIVE_INVALID_AMOUNT) return if target is None: target = ctx.author target_account = EconomyAccount.get_economy_account( target, self.bot.db_session) target_account.add_credit(self.bot.db_session, amount, "Admin grant.") await ctx.send(CMD_ADMIN_GIVE.format(target, amount))
async def reconsolidateall(self, ctx, target: discord.Member = None): """(Owner) Reconsolidate the whole database's balances.""" if target is None: all_accounts = EconomyAccount.get_all_economy_accounts( self.bot.db_session) inconsistent_accounts = 0 for account in all_accounts: # We disable committing here to optimize SQL query execution time result = account.reconsolidate_balance( self.bot.db_session, commit_on_execution=False) if not result: inconsistent_accounts += 1 self.bot.db_session.commit() await ctx.send( CMD_RECONSOLIDATE_MASS.format(len(all_accounts), inconsistent_accounts)) else: target_account = EconomyAccount.get_economy_account( target, self.bot.db_session) result = target_account.reconsolidate_balance(self.bot.db_session) if result: await ctx.send(CMD_RECONSOLIDATE_TRUE.format(target)) else: await ctx.send(CMD_RECONSOLIDATE_FALSE.format(target))
async def giltop(self, ctx): """Returns top 10 global accounts according to gil-on-hand.""" top_accounts = EconomyAccount.get_top_economy_accounts( self.bot.db_session, number=10) embed = discord.Embed(title="Top 10 Wealthiest Users", color=0xffd700) rank = 1 for account in top_accounts: user = self.bot.get_user(account.user_id) user_name = "#{1} **{0.name}#{0.discriminator}**".format( user, rank) gil_amount = "💵 {0:,.2f} gil".format(account.get_balance()) embed.add_field(name=user_name, value=gil_amount, inline=False) rank += 1 await ctx.send(embed=embed)
async def farmplant(self, ctx, plant_name, plant_count=1): """Plant a crop on a number of your available plots.""" _plant = Plant.get_plant(self.bot.db_session, plant_name) if _plant is None: await ctx.send(MSG_PLANT_NOT_FOUND.format(ctx.author)) return _account = EconomyAccount.get_economy_account(ctx.author, self.bot.db_session) total_price = _plant.buy_price * plant_count if not _account.has_balance(total_price, raw=True): await ctx.send( MSG_INSUFFICIENT_FUNDS.format(ctx.author, _account.get_balance())) return _farm = ORMFarm.get_farm(ctx.author, self.bot.db_session) _plots = _farm.get_available_plots(self.bot.db_session) if len(_plots) == None: await ctx.send(MSG_PLOT_NOT_FOUND.format(ctx.author)) return if len(_plots) < plant_count: await ctx.send( MSG_PLANT_NO_PLOTS.format(ctx.author, len(_plots), plant_count)) return _account.add_debit( self.bot.db_session, total_price, name="B:{0.id}={0.buy_price}".format(_plant), raw=True, ) for _plot in _plots[:plant_count]: _plot.plant_to_plot(_plant, self.bot.db_session, commit_on_execution=False) self.bot.db_session.commit() await ctx.send( MSG_PLOT_PLANT.format(ctx.author, _plant, plant_count, total_price / 10000, _account.get_balance()))
async def silobuy(self, ctx, up_count: int = 1): """Buy new N silos (increases your storage by 100). (N <= 1M)""" up_count = max(1, up_count) up_count = min(1000000, up_count) _farm = ORMFarm.get_farm(ctx.author, self.bot.db_session) _account = EconomyAccount.get_economy_account(ctx.author, self.bot.db_session) price = _farm.get_next_storage_upgrade_price(raw=True, up_count=up_count) if _account.has_balance(price, raw=True): _farm.upgrade_storage(self.bot.db_session, up_count) _account.add_debit(self.bot.db_session, price, name="SILOBUY", raw=True) await ctx.send( MSG_SILO_BUY_SUCCESS.format(ctx.author, _account.get_balance(), up_count)) else: await ctx.send( MSG_INSUFFICIENT_FUNDS_EXTRA.format(ctx.author, _account.get_balance(), price / 10000))