-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
414 lines (306 loc) · 10.7 KB
/
main.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
from tempfile import TemporaryFile
import discord
from discord.ext import commands
import json
import os
import random
from dotenv import load_dotenv
load_dotenv()
my_token = os.getenv("TOKEN")
#os.chdir("C:/Users/richardji/Desktop/Economy")
intents = discord.Intents.all()
client = commands.Bot(command_prefix = "e!", intents = intents)
bot = commands.Bot(command_prefix = "e!", intents = intents)
mainshop = [{"name": "Coconut", "price":100, "description":"Yummy"},
{"name": "Cabbage", "price":1000, "description":"Becuz of Inflation"}]
@client.event
async def on_ready():
await client.change_presence(status = discord.Status.idle, activity = discord.Game('Economy Keeper'))
print("Bot is up and running!")
@client.command()
async def balance(ctx):
user = ctx.author
a = await open_account(ctx.author) #Open an account for the user
users = await get_bank_data()
wallet_amt = users[str(user.id)]["wallet"]
bank_amt = users[str(user.id)]["bank"]
em = discord.Embed(title = f"{ctx.author.name}'s balance", color = discord.Color.blue())
em.add_field(name = "Wallet Balance", value = wallet_amt)
em.add_field(name = "Bank Balance", value = bank_amt)
await ctx.send(embed = em)
@client.command()
async def shop(ctx):
em = discord.Embed(title = "Shop")
for item in mainshop:
name = item["name"]
price = item["price"]
description = item["description"]
em.add_field(name = name, value = f"${price} | {description}")
await ctx.send(embed = em)
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandOnCooldown):
msg = 'You cannot use this command right now, try again in {:.2f}s'.format(error.retry_after)
await ctx.send(msg)
@client.command()
@commands.cooldown(1, 300, commands.BucketType.user) #Rate, per, commands.BucketType
async def beg(ctx):
await open_account(ctx.author)
user = ctx.author
users = await get_bank_data()
earnings = random.randrange(100, 1000)
await ctx.send(f"Someone gave you {earnings} coins!!")
users[str(user.id)]["wallet"] += earnings
with open("mainbank.json", "w") as f:
json.dump(users,f)
@client.command()
async def buy(ctx, item, amount = 1):
await open_account(ctx.author)
result = await buy_this(ctx.author, item, amount)
if not result[0]:
if result[1] == 1:
await ctx.send("That Object isn't there!")
if result[1] == 2:
await ctx.send(f"You don't have enough money in your wallet to buy {item}!")
return
await ctx.send(f"you just bought {amount} {item}!")
async def buy_this(user, item_name, amount):
item_name = item_name.lower()
name_ = None
for item in mainshop:
name = item["name"].lower()
if name == item_name:
name_ = name
price = item["price"]
break
if name_ == None:
return [False, 1] #Return error code 1
cost = price * amount
users = await get_bank_data()
bal = await update_bank(user)
if bal[0] < cost:
return [False, 2] #Return error code 2
try:
index = 0
t = None #tracker
for thing in users[str(user.id)]["bag"]:
n = thing["item"]
if n == item_name:
old_amt = thing["amount"]
new_amt = old_amt + amount
users[str(user.id)]["bag"][index]["amount"] = new_amt
t = 1
break
index += 1
if t == None:
obj = {"item":item_name, "amount" : amount}
users[str(user.id)]["bag"].append(obj)
except:
obj = {"item": item_name, "amount": amount}
users[str(user.id)]["bag"] = [obj]
with open("mainbank.json", "w") as f:
json.dump(users,f)
await update_bank(user, cost * -1, "wallet")
return [True, "Worked!"]
@client.command()
async def bag(ctx):
await open_account(ctx.author)
user = ctx.author
users = await get_bank_data()
try:
bag = users[str(user.id)]["bag"]
except:
bag = []
em = discord.Embed(title = "Bag")
for item in bag:
name = item["item"]
amount = item["amount"]
em.add_field(name = name, value = amount)
await ctx.send(embed = em)
async def open_account(user):
users = await get_bank_data()
with open("mainbank.json", "r") as f:
users = json.load(f) #Give us data of users
if str(user.id) in users:
return False
else:
users[str(user.id)] = {}
users[str(user.id)]["wallet"] = 0
users[str(user.id)]["bank"] = 0
with open("mainbank.json", "w") as f:
json.dump(users,f) #Create new account if user does not already have one
return True
async def get_bank_data():
with open("mainbank.json", "r") as f:
users = json.load(f)
return users
async def update_bank(user, change = 0, mode = "wallet"):
users = await get_bank_data()
users[str(user.id)][mode] += change
with open("mainbank.json", "w") as f:
json.dump(users,f)
bal = [users[str(user.id)]["wallet"],users[str(user.id)]["bank"]]
return bal
@client.command()
async def withdraw(ctx, amount = None):
await open_account(ctx.author)
if amount == None:
await ctx.send("Please enter the amount")
return
bal = await update_bank(ctx.author)
amount = int(amount)
if amount > bal[1]:
await ctx.send("You Don't enough money!")
return
if amount < 0:
await ctx.send("Amount must be positive")
return
await update_bank(ctx.author, amount)
await update_bank(ctx.author, -1*amount, "bank")
await ctx.send(f"You Withdrew {amount} coins!")
@client.command() #Understand this code
async def sell(ctx, item, amount = 1):
await open_account(ctx.author)
res = await sell_this(ctx.author,item,amount)
if not res[0]:
if res[1] == 1:
await ctx.send("That object isn't there!")
if res[1] == 2:
await ctx.send(f"You don't have {amount} {item} in your bag!")
if res[1] == 3:
await ctx.send(f"You don't have {item} in your bag!")
return
await ctx.send(f"You just sold {amount} {item}.")
async def sell_this(user,item_name, amount, price = None):
item_name = item_name.lower()
name_ = None
for item in mainshop:
name = item["name"].lower()
if name == item_name:
name_ = name
if price == None:
price = 0.7*item["price"]
break
if name_ == None:
return [False, 1]
cost = price * amount
users = await get_bank_data()
bal = await update_bank(user)
try:
index = 9
t = None
for thing in users[str(user.id)]["bag"]:
n = thing["item"]
if n == item_name:
old_amt = thing["amount"]
new_amt = old_amt - amount
if new_amt < 0:
return [False, 2]
users[str(user.id)]["bag"][index]["smount"] = new_amt
t = 1
break
index += 1
if t == None:
return [False, 3]
except:
return [False, 3]
@client.command(aliases = ["lb"])
async def leaderboard(ctx, x = 3):
users = await get_bank_data()
leader_board = {}
total = []
for user in users:
name = int(user)
total_amount = users[user]["wallet"] + user[user]["bank"]
leader_board[total_amount] = name
total.append(total_amount)
total = sorted(total, reverse = True)
em = discord.Embed(title = f"Top {x} Richest People", description = "Calculated through the money in the bank and wallet", color = discord.Color(0xfa43ee))
index = 1
for amt in total:
id_ = leader_board[amt]
mem = client.get_user(id_)
name = mem.name
em.add_field(name = f"{index}. {name}", value = f"{amt}", inline = False)
if index == x:
break
else:
index += 1 #Until here
@client.command()
async def send(ctx,member:discord.Member, amount = None):
await open_account(ctx.author)
await open_account(member)
if amount == None:
await ctx.send("Please enter the amount")
return
bal = await update_bank(ctx.author)
if amount == "all":
amount = bal[0]
amount = int(amount)
if amount > bal[1]:
await ctx.send("You Don't enough money!")
return
if amount < 0:
await ctx.send("Amount must be positive")
return
await update_bank(ctx.author, -1*amount, "bank")
await update_bank(member, amount, "bank")
await ctx.send(f"You sent {member} {amount} coins!")
@client.command()
async def rob(ctx,member:discord.Member):
await open_account(ctx.author)
await open_account(member)
bal = await update_bank(member)
if bal[0] < 1000:
await ctx.send("It is not worth robbing!")
return
earnings = random.randrange(-1000, bal[0] - 204)
await update_bank(ctx.author, earnings)
await update_bank(member, -1*earnings)
if earnings < 0:
await ctx.send(f"You lost {-1*earnings} coins!")
else:
await ctx.send(f"You managed to steal {earnings} coins!")
@client.command()
async def slots(ctx, amount = None):
await open_account(ctx.author)
if amount == None:
await ctx.send("Please enter the amount")
return
bal = await update_bank(ctx.author)
amount = int(amount)
if amount > bal[0]:
await ctx.send("You Don't enough money!")
return
if amount < 0:
await ctx.send("Amount must be positive")
return
final = []
for i in range(3):
a = random.choice(["X", "O", "Q"])
final.append(a)
await ctx.send(str(final))
if final[0] == final[1] or final[0] == final[2] or final[2] == final[1]:
await update_bank(ctx.author, 2*amount)
await ctx.send(f"Congrats! You won {amount} coins")
else:
update_bank(ctx.author, -1*amount)
await ctx.send(f"Unlucky, you lost {amount} coins")
@client.command()
async def deposit(ctx, amount = None):
await open_account(ctx.author)
if amount == None:
await ctx.send("Please enter the amount")
return
bal = await update_bank(ctx.author)
amount = int(amount)
if amount > bal[0]:
await ctx.send("You Don't enough money!")
return
if amount < 0:
await ctx.send("Amount must be positive")
return
await update_bank(ctx.author, -1*amount)
await update_bank(ctx.author, amount, "bank")
await ctx.send(f"You Deposited {amount} coins!")
client.run(my_token)