Created
July 3, 2018 12:21
-
-
Save IAmJSD/f63925b8596fcd720cc677973b913812 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from discord.ext import commands | |
from shared import get_guild, get_wallet,\ | |
render_emoji, save_wallet | |
from ext_utils import FormattedEmbed | |
import discord | |
# Imports go here. | |
class Balance: | |
def __init__(self, dclient): | |
self.dclient = dclient | |
# Initialises the cog. | |
@commands.command( | |
help="Shows your/someone elses" | |
" balance of the guilds currency.", | |
usage="{user}" | |
) | |
async def balance( | |
self, ctx, user: discord.Member=None | |
): | |
yours = user is None | |
if not user: | |
user = ctx.message.author | |
guilddb = await get_guild( | |
ctx.guild.id | |
) | |
user_wallet = await get_wallet( | |
ctx.guild.id, | |
user.id | |
) | |
bal = user_wallet.get( | |
"balance", 0 | |
) | |
emoji = render_emoji( | |
self.dclient, guilddb) | |
if yours: | |
title = f"Balance - {bal}{emoji}" | |
desc = ("You can check your transactions" | |
f" with `{ctx.prefix}transactions`.") | |
else: | |
title = (f"{user.name}'s Balance" | |
f" - {bal}{emoji}") | |
desc = ("You can check your transactions" | |
f" with `{ctx.prefix}transactions`" | |
f" or your balance with `{ctx.prefix}" | |
"balance`.") | |
try: | |
await ctx.send( | |
embed=FormattedEmbed( | |
self.dclient, | |
ctx.message.author, | |
title=title, | |
description=desc, | |
colour=discord.Colour.green() | |
), | |
delete_after=15 | |
) | |
except discord.Forbidden: | |
pass | |
# Gets the users balance. | |
class Pay: | |
def __init__(self, dclient): | |
self.dclient = dclient | |
# Initialises the cog. | |
async def pay_user(self, user, recv, amount): | |
user_a = user.get("balance", 0) | |
recv_a = recv.get("balance", 0) | |
if amount > user_a: | |
return False | |
new_user_a = user_a - amount | |
new_recv_a = recv_a + amount | |
user['balance'] = new_user_a | |
recv['balance'] = new_recv_a | |
user['transactions'] = user.get( | |
"transactions", [] | |
) | |
recv['transactions'] = recv.get( | |
"transactions", [] | |
) | |
user['transactions'].append({ | |
"type": "PAY_OUT", | |
"to": recv['_id'].split("_")[1], | |
"amount": amount, | |
"before": user_a, | |
"after": new_user_a | |
}) | |
recv['transactions'].append({ | |
"type": "PAY_IN", | |
"from": user['_id'].split("_")[1], | |
"amount": amount, | |
"before": recv_a, | |
"after": new_recv_a | |
}) | |
await save_wallet(user) | |
await save_wallet(recv) | |
return True | |
# The logic behind paying users. | |
@commands.command( | |
help="Allows you to " | |
"pay a user a bit of the " | |
"currency you have in the " | |
"guild you are in.", | |
usage="[user] [amount]" | |
) | |
async def pay( | |
self, ctx, reciever: discord.Member, | |
amount: int | |
): | |
if amount == 0: | |
try: | |
return await ctx.send( | |
self.dclient, | |
ctx.message.author, | |
title="Payment Declined:", | |
description="You cannot pay" | |
" nothing.", | |
colour=discord.Colour.red() | |
) | |
except discord.Forbidden: | |
return | |
user_wallet = await get_wallet( | |
ctx.guild.id, | |
ctx.message.author.id | |
) | |
recv_wallet = await get_wallet( | |
ctx.guild.id, | |
reciever.id | |
) | |
sufficient_funds = await self.pay_user( | |
user_wallet, recv_wallet, amount | |
) | |
if not sufficient_funds: | |
try: | |
return await ctx.send( | |
embed=FormattedEmbed( | |
self.dclient, | |
ctx.message.author, | |
title=f"Insufficient Funds:", | |
description="You have insufficient" | |
" funds. You can see your balance" | |
f" with `{ctx.prefix}balance`.", | |
colour=discord.Colour.red() | |
), | |
delete_after=15 | |
) | |
except discord.Forbidden: | |
return | |
guilddb = await get_guild(ctx.guild.id) | |
emoji = render_emoji( | |
self.dclient, guilddb | |
) | |
try: | |
await ctx.send( | |
embed=FormattedEmbed( | |
self.dclient, | |
ctx.message.author, | |
title="User Paid:", | |
description="You successfully paid " | |
f"{reciever.mention} {amount}{emoji}." | |
" You can view your balance with " | |
f"`{ctx.prefix}balance` or view your " | |
f"transactions with `{ctx.prefix}" | |
"transactions`.", | |
colour=discord.Colour.green() | |
), | |
delete_after=15 | |
) | |
except discord.Forbidden: | |
pass | |
# The command that allows you to pay people. | |
class Give: | |
def __init__(self, dclient): | |
self.dclient = dclient | |
# Initialises the cog. | |
@commands.command( | |
help="Allows you to give " | |
"some of the servers currency." | |
" **This command is for " | |
"administrators only.**", | |
usage="[user] [amount]" | |
) | |
@commands.has_permissions( | |
administrator=True | |
) | |
async def give( | |
self, ctx, user: discord.Member, | |
amount: int | |
): | |
user_wallet = await get_wallet( | |
ctx.guild.id, | |
user.id | |
) | |
old_bal = user_wallet.get( | |
"balance", 0 | |
) | |
new_bal = old_bal + amount | |
user_wallet['transactions'] = user_wallet.get( | |
"transactions", [] | |
) | |
user_wallet['transactions'].append({ | |
"type": "PAY_IN", | |
"from": str(ctx.message.author.id), | |
"amount": amount, | |
"before": old_bal, | |
"after": new_bal | |
}) | |
user_wallet['balance'] = new_bal | |
await save_wallet(user_wallet) | |
guilddb = await get_guild(ctx.guild.id) | |
emoji = render_emoji( | |
self.dclient, guilddb | |
) | |
try: | |
await ctx.send( | |
embed=FormattedEmbed( | |
self.dclient, | |
ctx.message.author, | |
title="User Given Currency:", | |
description="You successfully gave " | |
f"{user.mention} {amount}{emoji}.", | |
colour=discord.Colour.green() | |
), | |
delete_after=15 | |
) | |
except discord.Forbidden: | |
pass | |
# Allows administrators to give currency. | |
class Take: | |
def __init__(self, dclient): | |
self.dclient = dclient | |
# Initialises the cog. | |
@commands.command( | |
help="Allows you to take " | |
"some of the servers currency." | |
" **This command is for " | |
"administrators only.**", | |
usage="[user] [amount]" | |
) | |
@commands.has_permissions( | |
administrator=True | |
) | |
async def take( | |
self, ctx, user: discord.Member, amount: int | |
): | |
user_wallet = await get_wallet( | |
ctx.guild.id, | |
user.id | |
) | |
bal = user_wallet.get("balance", 0) | |
if amount > bal: | |
try: | |
return await ctx.send( | |
embed=FormattedEmbed( | |
self.dclient, | |
ctx.message.author, | |
title=f"Insufficient Funds:", | |
description="They have less " | |
"funds than you are trying to" | |
f" take. Use `{ctx.prefix}balance " | |
f"@{user}` to see their balance.", | |
colour=discord.Colour.red() | |
), | |
delete_after=15 | |
) | |
except discord.Forbidden: | |
return | |
new_bal = bal - amount | |
user_wallet['balance'] = new_bal | |
user_wallet['transactions'] = user_wallet.get( | |
"transactions", [] | |
) | |
user_wallet['transactions'].append({ | |
"type": "PAY_OUT_TAKE", | |
"from": str(ctx.message.author.id), | |
"amount": amount, | |
"before": bal, | |
"after": new_bal | |
}) | |
await save_wallet(user_wallet) | |
guilddb = await get_guild(ctx.guild.id) | |
emoji = render_emoji( | |
self.dclient, guilddb | |
) | |
try: | |
await ctx.send( | |
embed=FormattedEmbed( | |
ctx.bot, | |
ctx.message.author, | |
title="Took Money:", | |
description="Successfully " | |
f"took {amount}{emoji} from " | |
f"{user.mention}.", | |
colour=discord.Colour.green() | |
), | |
delete_after=15 | |
) | |
except discord.Forbidden: | |
pass | |
# Allows you to take currency from someone. | |
class Transactions: | |
def __init__(self, dclient): | |
self.dclient = dclient | |
# Initialises the cog. | |
@commands.command( | |
help="Allows you to view " | |
"your transactions." | |
) | |
async def transactions(self, ctx): | |
wallet = await get_wallet( | |
ctx.guild.id, | |
ctx.message.author.id | |
) | |
transactions = wallet.get( | |
"transactions" | |
) | |
if not transactions: | |
try: | |
return await ctx.send( | |
embed=FormattedEmbed( | |
self.dclient, | |
ctx.message.author, | |
title=f"No Transactions:", | |
description="You have no " | |
"transactions to view.", | |
colour=discord.Colour.red() | |
), | |
delete_after=15 | |
) | |
except discord.Forbidden: | |
return | |
embeds = [FormattedEmbed( | |
ctx.bot, | |
ctx.message.author, | |
title="Transactions:", | |
description="Here are a list " | |
"of transactions you accumulated:", | |
colour=discord.Colour.green() | |
)] | |
guilddb = await get_guild( | |
ctx.guild.id | |
) | |
emoji = render_emoji( | |
self.dclient, guilddb | |
) | |
for transaction in transactions: | |
value = ( | |
f"Amount Before: {transaction['before']}" | |
f"{emoji}\nAmount After: {transaction['after']}" | |
f"{emoji}" | |
) | |
action = transaction['type'] | |
if action == "PAY_IN": | |
user = self.dclient.get_user( | |
int(transaction['from']) | |
) | |
if not user: | |
user = transaction['from'] | |
title = ( | |
f"Given {transaction['amount']}" | |
f"{emoji} by {user}:" | |
) | |
elif action == "PAY_OUT": | |
user = self.dclient.get_user( | |
transaction['to'] | |
) | |
if not user: | |
user = transaction['to'] | |
title = ( | |
f"Paid {transaction['amount']}" | |
f"{emoji} to {user}:" | |
) | |
elif action == "PAY_OUT_TAKE": | |
user = self.dclient.get_user( | |
int(transaction['from']) | |
) | |
if not user: | |
user = transaction['from'] | |
title = ( | |
f"{transaction['amount']}" | |
f"{emoji} was took by {user}:" | |
) | |
else: | |
continue | |
if len(embeds[-1].fields) == 25: | |
embeds.append( | |
FormattedEmbed( | |
ctx.bot, | |
ctx.message.author, | |
colour=discord.Colour.green() | |
) | |
) | |
embeds[-1].add_field( | |
name=title, | |
value=value | |
) | |
for em in embeds: | |
try: | |
await ctx.send( | |
embed=em | |
) | |
except discord.Forbidden: | |
return | |
# Gets all of a users transactions. | |
def setup(dclient): | |
dclient.add_cog(Balance(dclient)) | |
dclient.add_cog(Pay(dclient)) | |
dclient.add_cog(Give(dclient)) | |
dclient.add_cog(Take(dclient)) | |
dclient.add_cog(Transactions(dclient)) | |
# Sets up the plugin. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment