-
Notifications
You must be signed in to change notification settings - Fork 0
/
economy.py
391 lines (339 loc) · 15.2 KB
/
economy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import datetime
import json
from asyncio import sleep
import discord
import modules.utilities as utils
from discord.ext import commands
from modules.extension import Extension
from modules.utilities import logger as l
from modules.utilities import prefix
from modules.utilities import utilities as u
class economy(Extension):
"""
Economy Extension - ylcb-devs
"""
def __init__(self, bot: commands.Bot):
"""
economy(bot)
Args:
bot (`commands.Bot`): commands.Bot instance
"""
super().__init__(bot, "economy")
self.db = bot.get_cog("database")
def get_balance_from_d_id(self, discord_id: int) -> float:
"""
Returns a user's balance
Args:
discord_id (int): User's id
Returns:
float: Balance
"""
return self.db.cursor.execute("SELECT balance FROM Users WHERE discord_id=?", (discord_id,)).fetchone()[0]
def set_balance_from_d_id(self, discord_id: int, bal: int) -> float:
"""
Set a user's balance
Args:
discord_id (int): User's id
bal (int): New balance value
Returns:
float: Balance
"""
self.db.cursor.execute("UPDATE Users SET balance=? WHERE discord_id=?", (round(bal,2), discord_id))
self.db.db.commit()
return round(bal, 2)
def can_pay_amount(self, discord_id: int, amount: int) -> bool :
"""
Returns if a user can pay amount
Args:
discord_id (int): User's id
amount (int): Amount to pay
Returns:
bool: If user can pay amount
"""
snd_bal = self.get_balance_from_d_id(discord_id)
return snd_bal > amount
def get_transaction_history_from_id(self, discord_id: int) -> list :
"""
Return a user's transaction history
Args:
discord_id (int): User's id
Returns:
list: Transaction History
"""
th = self.db.cursor.execute("SELECT transaction_history FROM Users WHERE discord_id=?", (discord_id,)).fetchone()[0]
return json.loads(th.replace("\'","\""))
def set_transaction_history_from_id(self, discord_id: int, value: list) -> list :
"""
Set a user's transaction history
Args:
discord_id (int): User's id
value (list): New transaction history
Returns:
list: value
"""
self.db.cursor.execute("UPDATE Users SET transaction_history=? WHERE discord_id=?", (str(value), discord_id))
self.db.db.commit()
return value
def clear_transaction_history_from_id(self, discord_id: int) -> dict :
"""
Clear a user's transaction history
Args:
discord_id (int): User's id
Returns:
dict: Empty array
"""
return self.set_transaction_history_from_id(discord_id, [])
def push_transaction_history_from_id(self, discord_id: int, place: str, amount: float) -> dict :
"""
Push an entry to a user's transaction history
Args:
discord_id (int): User's id
place (str): Place money was spent
amount (float): Amount of money spent
Returns:
dict: New transaction history value
"""
thCur: list = self.get_transaction_history_from_id(discord_id)
if len(thCur) >= 3:
for x in range(len(thCur) - 2):
thCur.pop()
thCur.insert(0,{"place": place, "amount": amount})
self.set_transaction_history_from_id(discord_id, thCur)
return thCur
@commands.command(name="balance", aliases=["bal"], usage=f"{prefix}balance [user:user]", brief="Returns you or another user's balance")
async def balance(self, ctx, user: discord.Member = None):
"""
Returns you or another user's balance
Args:
user (`discord.Member`, optional): User to get balance for. Defaults to `None`.
"""
if not user: user = ctx.author
points = self.get_balance_from_d_id(user.id)
th: list = self.get_transaction_history_from_id(user.id)
description: str = ""
if th:
description = "Transaction History\nYour 3 recent transactions\n\n"
for entry in th:
entry: dict
if entry["amount"] > 0 : emoji = "🟩"
else: emoji = "🟥"
if len(entry["place"]) > 20:
entry["place"] = entry["place"][:20]
entry["place"][19] = "…"
description = description + f"{emoji} {entry['place'].ljust(20,'.')} ${str(entry['amount'])}\n"
embed_dict = {
"title":f"${str(points)}",
"description": description,
"type": "rich",
"timestamp": datetime.datetime.now().isoformat(),
"color": 0x00ff00,
"author": {
"name": u.discordify(str(user)),
"icon_url": str(user.avatar_url)
}
}
await ctx.send(embed=discord.Embed.from_dict(embed_dict))
@commands.command(name="leaderboard", aliases=["lb"], usage=f"{prefix}leaderboard", brief="Show server leaderboard")
async def leaderboard(self, ctx):
"""
Show server leaderboard
"""
everyone = self.db.cursor.execute("SELECT * FROM Users").fetchall()
lb: list = self.db.cursor.execute("SELECT * FROM Users ORDER BY balance DESC").fetchmany(5)
server_total: int = 0
fields: list = []
for entry in everyone: server_total += entry[4]
for i, entry in enumerate(lb):
entry_user = await self.bot.fetch_user(entry[2])
fields.append({"name": f"{i+1}. {u.discordify(str(entry_user))}", "value": f"({str(round(entry[4]/server_total*100, 1))}%) ${'{:,}'.format(entry[4])}"})
embed_dict = {
"title":f"Leaderboard (${server_total})",
"type": "rich",
"timestamp": datetime.datetime.now().isoformat(),
"color": 0x00cc99,
"fields": fields,
"author": {
"name": u.discordify(str(self.bot.user)),
"icon_url": str(self.bot.user.avatar_url)
}
}
await ctx.send(embed=discord.Embed.from_dict(embed_dict))
@commands.command(name="pay", usage=f"{prefix}pay <receiver:user> [amount:float=50] [message:str]", brief="Pay another user")
async def pay(self, ctx, reciever: discord.Member, amount: float = 50, *, message: str = None):
"""
Pay another user
Args:
reciever (`discord.Member`): Person to pay money to
amount (`float`, optional): Amount to pay. Defaults to `50`.
message (`str`, optional): Message to user. Defaults to `None`.
"""
if reciever == ctx.author:
await ctx.send(f"{ctx.author.mention}, you cannot send money to yourself")
return
amount = round(amount, 2)
if self.can_pay_amount(ctx.author.id, amount):
l.log(f"Check: {u.discordify(str(ctx.author))} | Amount:${amount} | Reciever:{str(reciever)} | Status: AWAITING APPROVAL", channel=l.DISCORD)
self.set_balance_from_d_id(ctx.author.id, self.get_balance_from_d_id(ctx.author.id)-amount)
embed_dict = {
"title":"Check [AWAITING APPROVAL]",
"type": "rich",
"timestamp": datetime.datetime.now().isoformat(),
"color": 0xff8800,
"fields": [
{"name": "Pay To:", "value": u.discordify(str(reciever)), "inline":True},
{"name": "Balance:", "value": "$"+str(amount), "inline":True},
{"name": "From:", "value": u.discordify(str(ctx.author)), "inline":True},
]
}
if message: embed_dict["fields"].append({"name": "Message:", "value": message})
embed = discord.Embed.from_dict(embed_dict)
msg: dicsord.Message = await ctx.send(f"Are you sure you want to pay this user ${amount}",embed=embed)
await msg.add_reaction("✅")
await msg.add_reaction("❎")
await sleep(0.3)
def check(payload: discord.RawReactionActionEvent): return payload.user_id == ctx.author.id and str(payload.emoji) in ["✅", "❎"] and payload.message_id == msg.id
payload = await self.bot.wait_for("raw_reaction_add", check=check)
if str(payload.emoji) == "✅":
embed_dict["title"] = "Check [PENDING]"
l.log(f"Check: {str(ctx.author)} | Amount:${amount} | Reciever:{str(reciever)} | Status: APPROVED,PENDING", channel=l.DISCORD)
elif str(payload.emoji) == "❎":
await msg.delete()
await ctx.message.delete()
self.set_balance_from_d_id(ctx.author.id, self.get_balance_from_d_id(ctx.author.id)+amount)
l.log(f"Check: {str(ctx.author)} | Amount:${amount} | Reciever:{str(reciever)} | Status: CANCELED", channel=l.DISCORD)
embed = discord.Embed.from_dict(embed_dict)
await msg.edit(content=reciever.mention,embed=embed)
await sleep(0.3)
def check(payload: discord.RawReactionActionEvent): return payload.member == reciever and str(payload.emoji) in ["✅", "❎"] and payload.message_id == msg.id
payload = await self.bot.wait_for("raw_reaction_add", check=check)
if str(payload.emoji) == "✅":
embed_dict["title"] = "Check [ACCEPTED]"
embed_dict["color"] = 0x00ff00
try:
self.set_balance_from_d_id(reciever.id, self.get_balance_from_d_id(reciever.id)-amount)
self.push_transaction_history_from_id(ctx.author.id, "Transfer", -1*amount)
except Exception as e: l.log(e, l.ERR, l.DISCORD)
else:self.set_balance_from_d_id(ctx.author.id, self.get_balance_from_d_id(ctx.author.id)+amount)
self.set_balance_from_d_id(reciever.id, self.get_balance_from_d_id(reciever.id)+amount)
self.push_transaction_history_from_id(reciever.id, "Transfer", amount)
l.log(f"Check: {str(ctx.author)} | Amount:${amount} | Reciever:{str(reciever)} | Status: ACCEPTED,PAID", channel=l.DISCORD)
elif str(payload.emoji) == "❎":
embed_dict["title"] = "Check [DECLINED]"
embed_dict["color"] = 0xff0000
l.log(f"Check: {str(ctx.author)} | Amount:${amount} | Reciever:{str(reciever)} | Status: DECLINED,REFUNDED", channel=l.DISCORD)
self.set_balance_from_d_id(ctx.author.id, self.get_balance_from_d_id(ctx.author.id)+amount)
embed_dict["timestamp"] = datetime.datetime.now().isoformat()
await msg.edit(content=None, embed=discord.Embed.from_dict(embed_dict))
try: await msg.clear_reactions()
except Exception as e: l.log(e, l.WRN, l.DISCORD)
else:
l.log(f"Check: {str(ctx.author)} | Amount:${amount} | Reciever:{str(reciever)} | Status: BANK DECLINED", channel=l.DISCORD)
await ctx.send(f"{str(ctx.author.mention)}, you only have ${self.get_balance_from_d_id(ctx.author.id)}")
@commands.command(name="request", aliases=["req"], usage=f"{prefix}request <sender:user> [amount:float=50] [message:str]", brief="Request money from another user")
async def request(self, ctx, sender: discord.Member, amount: float = 50, *, message: str = None):
"""
Request money from another user
Args:
sender (`discord.Member`): User you want money from
amount (`float`, optional): Amount to request. Defaults to `50`.
message (`str`, optional): Message to user. Defaults to `None`.
"""
if sender == ctx.author:
await ctx.send(f"{ctx.author.mention}, you cannot request money from yourself")
return
if self.can_pay_amount(sender.id, amount):
l.log(f"Money Request: {str(ctx.author)} | Amount:${amount} | Payer:{str(sender)} | Status: APPROVED,PENDING", channel=l.DISCORD)
embed_dict = {
"title":"Money Request [PENDING]",
"type": "rich",
"timestamp": datetime.datetime.now().isoformat(),
"color": 0xff8800,
"fields": [
{"name": "Pay To:", "value": u.discordify(str(ctx.author)), "inline":True},
{"name": "Balance:", "value": "$"+str(amount), "inline":True},
{"name": "From:", "value": u.discordify(str(sender)), "inline":True},
]
}
if message: embed_dict["fields"].append({"name": "Message:", "value": message})
embed = discord.Embed.from_dict(embed_dict)
msg: dicsord.Message = await ctx.send(sender.mention,embed=embed)
await msg.add_reaction("✅")
await msg.add_reaction("❎")
await sleep(0.3)
def check(payload: discord.RawReactionActionEvent): return payload.member == sender and str(payload.emoji) in ["✅", "❎"] and payload.message_id == msg.id
payload = await self.bot.wait_for("raw_reaction_add", check=check)
if str(payload.emoji) == "✅":
embed_dict["title"] = "Money Request [ACCEPTED]"
embed_dict["color"] = 0x00ff00
try:
self.set_balance_from_d_id(sender.id, self.get_balance_from_d_id(sender.id)-amount)
self.push_transaction_history_from_id(sender.id, "Transfer", amount*-1)
except Exception as e: l.log(e, l.ERR, l.DISCORD)
else: self.set_balance_from_d_id(ctx.author.id, self.get_balance_from_d_id(ctx.author.id)+amount)
self.push_transaction_history_from_id(ctx.author.id, "Transfer", amount)
l.log(f"Money Request: {str(ctx.author)} | Amount:${amount} | Payer:{str(sender)} | Status: ACCEPTED,PAID", channel=l.DISCORD)
elif str(payload.emoji) == "❎":
embed_dict["title"] = "Money Request [DECLINED]"
embed_dict["color"] = 0xff0000
l.log(f"Money Request: {str(ctx.author)} | Amount:${amount} | Payer:{str(sender)} | Status: DECLINED,REFUNDED", channel=l.DISCORD)
self.set_balance_from_d_id(sender.id, self.get_balance_from_d_id(sender.id)+amount)
embed_dict["timestamp"] = datetime.datetime.now().isoformat()
await msg.edit(content=None, embed=discord.Embed.from_dict(embed_dict))
try: await msg.clear_reactions()
except Exception as e: l.log(e, l.WRN, l.DISCORD)
else:
l.log(f"Money Request: {str(ctx.author)} | Amount:${amount} | Payer:{str(sender)} | Status: DECLINED", channel=l.DISCORD)
await ctx.send(f"{ctx.author.mention}, that user has insufficient funds!")
@commands.command(name="go_broke", aliases=["0"], usage=f"{prefix}go_broke", brief="Go broke")
async def go_broke(self, ctx):
"""
Go broke
"""
try:
self.set_balance_from_d_id(ctx.author.id, 0)
self.clear_transaction_history_from_id(ctx.author.id)
except Exception as e: l.log(e, l.ERR, l.DISCORD)
else: await ctx.send(f"{ctx.author.mention}, congrats you're broke...")
# admin commands
@commands.command(name="set_balance", hidden=True)
@u.is_admin()
async def set_balance(self, ctx, user: discord.Member, amount: float = 0):
"""Set the given user's balance to amount"""
try:
self.set_balance_from_d_id(user.id, amount)
self.clear_transaction_history_from_id(user.id)
self.push_transaction_history_from_id(user.id, "Admin Set Amount", amount)
except Exception as e: l.log(e, l.ERR, l.DISCORD)
else: await ctx.send(f"{ctx.author.mention}, {user.mention}'s balance is now ${amount}")
@set_balance.error
async def set_balance_error(self, ctx, error):
if isinstance(error, commands.CheckFailure):
await ctx.send(f"{ctx.author.mention}, this command can only be used by admins")
@commands.command(name="add_balance", hidden=True)
@u.is_admin()
async def add_balance(self, ctx, user: discord.Member, amount: float):
"""Add an amount to the given user's balance"""
try:
self.set_balance_from_d_id(user.id, self.get_balance_from_d_id(user.id) + amount)
self.push_transaction_history_from_id(ctx.author.id, "Admin Added Amount", amount)
except Exception as e: l.log(e, l.ERR, l.DISCORD)
else: await ctx.send(f"{ctx.author.mention}, {user.mention}'s balance is now ${self.get_balance_from_d_id(user.id)}")
@add_balance.error
async def add_balance_error(self, ctx, error):
if isinstance(error, commands.CheckFailure):
await ctx.send(f"{ctx.author.mention}, this command can only be used by admins")
@commands.command(name="sub_balance", hidden=True)
@u.is_admin()
async def sub_balance(self, ctx, user: discord.Member, amount: float):
"""Subtract an amount from the given user's balance"""
try:
self.set_balance_from_d_id(user.id, self.get_balance_from_d_id(user.id) - amount)
self.push_transaction_history_from_id(ctx.author.id, "Admin Removed Amount", amount*-1)
except Exception as e: l.log(e, l.ERR, l.DISCORD)
else: await ctx.send(f"{ctx.author.mention}, {user.mention}'s balance is now ${self.get_balance_from_d_id(user.id)}")
@sub_balance.error
async def sub_balance_error(self, ctx, error):
if isinstance(error, commands.CheckFailure):
await ctx.send(f"{ctx.author.mention}, this command can only be used by admins")
def setup(bot):
bot.add_cog(economy(bot))