· 4 years ago · Jan 30, 2021, 10:20 PM
1import discord
2from discord.ext import commands
3
4import sqlite3
5
6idb = sqlite3.connect('user.sqlite3')
7cursor = idb.cursor()
8cursor.execute('''
9CREATE TABLE IF NOT EXISTS user(
10
11 id BIGINT,
12 wallet BIGINT,
13 bank BIGINT,
14 level BIGINT,
15 job TEXT
16
17)
18''')
19
20def get_user(user):
21 if user == None:
22 return user
23
24 return {
25 "id": user[0],
26 "wallet": user[1],
27 "bank": user[2],
28 "level": user[3],
29 "job": user[4]
30 }
31
32def add_wallet(user_id, amount, ctx):
33 with sqlite3.connect("user.sqlite3") as conn:
34 c = conn.cursor()
35
36 # check if user is in the USERS table
37 c.execute("SELECT * FROM user WHERE ID = ?", (user_id,))
38 if c.fetchone() == None:
39 c.execute("INSERT INTO user VALUES (?, ?, ?, ?, ?)", (ctx.author.id, 0, 150, 1, "Lemonade Stand"))
40 conn.commit()
41
42 c.execute("UPDATE user SET WALLET = WALLET + ? WHERE ID = ?", (amount, user_id))
43 conn.commit()
44
45def add_bank(user_id, amount, ctx):
46 with sqlite3.connect("user.sqlite3") as conn:
47 c = conn.cursor()
48
49 # check if user is in the USERS table
50 c.execute("SELECT * FROM user WHERE ID = ?", (user_id,))
51 if c.fetchone() == None:
52 c.execute("INSERT INTO user VALUES (?, ?, ?, ?, ?)", (ctx.author.id, 0, 150, 1, "Lemonade Stand"))
53 conn.commit()
54
55 c.execute("UPDATE user SET BANK = BANK + ? WHERE ID = ?", (amount, user_id))
56 conn.commit()
57
58def create_account(user_id, ctx):
59 with sqlite3.connect("user.sqlite3") as conn:
60 c = conn.cursor()
61
62 # check if user is in the USERS table
63 c.execute("SELECT * FROM user WHERE ID = ?", (user_id,))
64 if c.fetchone() == None:
65 c.execute("INSERT INTO user VALUES (?, ?, ?, ?, ?)", (user_id, 0, 150, 1, "Lemonade Stand"))
66 conn.commit()
67 c.execute("INSERT INTO user(id, wallet, bank, level, job) VALUES(?,?,?,?,?)", (user_id, 0, 150, 1, "Lemonade Stand"))
68 conn.commit()
69
70def update_job(user_id, job, ctx):
71 with sqlite3.connect("user.sqlite3") as conn:
72 c = conn.cursor()
73
74 # check if user is in the USERS table
75 c.execute("SELECT * FROM user WHERE ID = ?", (user_id,))
76 if c.fetchone() == None:
77 c.execute("INSERT INTO user VALUES (?, ?, ?, ?, ?)", (ctx.author.id, 0, 150, 1, "Lemonade Stand"))
78 conn.commit()
79
80 c.execute("UPDATE user SET JOB = ? WHERE ID = ?", (job, user_id))
81 conn.commit()
82
83def update_level(user_id, level, ctx):
84 with sqlite3.connect("user.sqlite3") as conn:
85 c = conn.cursor()
86
87 # check if user is in the USERS table
88 c.execute("SELECT * FROM user WHERE ID = ?", (user_id,))
89 if c.fetchone() == None:
90 c.execute("INSERT INTO user VALUES (?, ?, ?, ?, ?)", (ctx.author.id, 0, 150, 1, "Lemonade Stand"))
91 conn.commit()
92
93 c.execute("UPDATE user SET LEVEL = LEVEL + ? WHERE ID = ?", (level, user_id))
94 conn.commit()
95
96class Income(commands.Cog):
97
98 def __init__(self, client):
99 self.client = client
100
101 db = sqlite3.connect('user.sqlite3')
102
103 @commands.command()
104 async def stats(self, ctx, user : discord.Member=None):
105
106 await db
107 cursor = idb.cursor()
108 if user is None:
109 cursor.execute(f"SELECT id, wallet, bank, level, job FROM user WHERE id = '{ctx.author.id}'")
110 else:
111 cursor.execute(f"SELECT id, wallet, bank, level, job FROM user WHERE id = '{user.id}'")
112 result = cursor.fetchone()
113
114 if result == None:
115 msg = await ctx.send('Your a new player! Let me get your account started')
116 create_account(user.id, ctx)
117 await msg.edit(content="Your account has been made")
118 else:
119
120 if user is None:
121 user = ctx.author
122
123 job = get_user(result)["job"]
124 level = get_user(result)["level"]
125 wallet = get_user(result)["wallet"]
126 bank = get_user(result)["bank"]
127
128 embed = discord.Embed(title=f'{user}\'s Stats', color = discord.Color.green())
129 embed.add_field(name='Bank Balance', value=bank, inline=False)
130 embed.add_field(name='Wallet Balance', value=wallet, inline=False)
131 embed.add_field(name='Account Level', value=level, inline=False)
132 embed.add_field(name='Account Job', value=job, inline=False)
133
134 await ctx.send(embed=embed)
135
136
137
138 @commands.command()
139 async def work(self, ctx):
140
141 await db
142 cursor = idb.cursor()
143 cursor.execute(f"SELECT id, wallet, bank, level, job FROM user WHERE id = '{ctx.author.id}'")
144 result = cursor.fetchone()
145
146 if result == None:
147 msg = await ctx.send('Your a new player! Let me get your account started')
148 create_account(ctx.author.id, ctx)
149 await msg.edit(content="Your account has been made")
150 else:
151 with sqlite3.connect("user.sqlite3") as conn:
152 c = conn.cursor()
153 c.execute("SELECT * FROM user")
154 j = c.fetchone()
155 job = get_user(j)["job"]
156 wallet = get_user(j)["wallet"]
157 level = get_user(j)["level"]
158 if job == 'Lemonade Stand':
159 paycheck = 10*int(level)
160 elif job == 'Cashier':
161 paycheck = 30*int(level)
162 elif job == 'Chef':
163 paycheck = 50*int(level)
164 elif job == 'Author':
165 paycheck = 70*int(level)
166 elif job == 'Doctor':
167 paycheck = 90*int(level)
168 elif job == 'Lawyer':
169 paycheck = 110*int(level)
170
171 add_wallet(ctx.author.id, paycheck, ctx)
172 await ctx.send(f"You made {paycheck} and now have {wallet}")
173
174
175def setup(client):
176 client.add_cog(Income(client))