-
Notifications
You must be signed in to change notification settings - Fork 0
/
libcurrency.py
69 lines (61 loc) · 2.41 KB
/
libcurrency.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
import asyncio
import dataset
import discord
DATABASE = dataset.connect('sqlite:///data/bot/higgsbot.db')
class Token:
def __init__(self):
self.table = DATABASE['balance']
async def start(self, bot):
for member in bot.get_all_members():
id = member.id
if self.table.find_one(user=id) is None:
self.table.insert(dict(user=id, coins=3))
def check_balance(self, usr):
id = usr.id
if self.table.find_one(user=id) is not None:
user = self.table.find_one(user=id)
return user['coins']
else:
self.table.insert(dict(user=id, coins=3))
return 3
def set_balance(self, usr, b):
if b >= 0:
id = usr.id
if self.table.find_one(user=id) is not None:
self.table.update(dict(user=id, coins=b), ['user'])
return
else:
self.table.insert(dict(user=id, coins=b))
return
else:
raise Exception("Balance cannot be less than 0")
def remove_balance(self, usr, c):
id = usr.id
if self.table.find_one(user=id) is not None:
user = self.table.find_one(user=id)
if (user['coins'] - c) >= 0:
new_coins = user['coins'] - c
self.table.update(dict(user=id, coins=new_coins), ['user'])
return
else:
raise Exception("Balance insufficient")
else:
self.table.insert(dict(user=id, coins=c))
user = self.table.find_one(user=id)
if (user['coins'] - c) >= 0:
new_coins = user['coins'] - c
self.table.update(dict(user=id, coins=new_coins), ['user'])
return
else:
raise Exception("Balance insufficient")
def join(self, usr): # On joining of user add him to the table if he's not already there.
id = usr.id
if self.table.find_one(user=id) is None:
self.table.insert(dict(user=id, coins=3))
async def payment(self):
while True: # 10 minute loop to add CodeTokens.
await asyncio.sleep(600)
for user in self.table:
if user['coins'] < 10:
user['coins'] = user['coins'] + 1
self.table.update(dict(user=user['user'], coins=user['coins']), ['user'])