/
functions.py
304 lines (200 loc) · 7.32 KB
/
functions.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
"""
/functions.py
Author(s):
Utku Tombul (Calabresi)
Changelog:
28/02/2018:
- Initial file commit.
07/12/2018:
- Added get_bet, create_bet, get_user_bet, create_user_bet.
"""
import asyncio
import json
from datetime import datetime, timedelta
from config import Config
from extensions import client, logger, session, Base
from models import User, UserRole, UserBet, UserCurrency, Bet, Currency, System
from random import randint
from requests import get
def get_current_date(tz=None):
"""
Returns current datetime.
"""
return datetime.now(tz)
def get_user(**arguments):
"""
Creates a query with the given arguments for user selection. Returns the query.
"""
query = session.query(User)
if "id" in arguments:
query = query.filter_by(id=int(arguments["id"]))
if "discord_id" in arguments:
query = query.filter_by(discord_id=arguments["discord_id"])
return query
def create_user(discord_id):
"""
Creates a new user in the database. Returns the created user or None if failed.
"""
user = User(discord_id=discord_id,
creation_date=get_current_date())
session.add(user)
session.commit()
return user
def give_role_to_user(user_id, role_id):
"""
Gives a role to the user in database and Discord. Returns the given role or None if failed.
"""
role = UserRole(user_id=user_id, role_id=role_id)
session.add(role)
session.commit()
return role
def get_user_currency(**arguments):
"""
Creates a query with the given arguments for user currency selection. Returns the query.
"""
query = session.query(UserCurrency)
if "id" in arguments:
query = query.filter_by(id=int(arguments["id"]))
if "user_id" in arguments:
query = query.filter_by(user_id=int(arguments["user_id"]))
if "short_code" in arguments:
query = query.filter_by(short_code=str(arguments["short_code"]))
if "amount" in arguments:
query = query.filter_by(amount=float(arguments["amount"]))
return query
def create_user_currency(user_id, short_code):
"""
Creates a currency for the user. Returns the created currency or None if not exists.
"""
currency = UserCurrency(user_id=user_id, short_code=short_code, amount=0)
session.add(currency)
session.commit()
return currency
def add_currency_to_user(user_id, short_code, amount):
"""
Adds specified amount of currency to the user. Returns updated currency.
"""
currency = get_user_currency(user_id=user_id, short_code=short_code).first()
if not currency:
currency = create_user_currency(user_id, short_code)
currency.amount += amount
session.commit()
return currency
def remove_currency_from_user(user_id, short_code, amount):
"""
Removes specified amount of currency to the user. Returns updated currency.
"""
currency = get_user_currency(
user_id=user_id, short_code=short_code).first()
if not currency:
currency = create_user_currency(user_id, short_code)
currency.amount -= amount
session.commit()
return currency
def get_angry_message():
"""
Returns an angry message, because why not?
"""
return ["Sen burayı ahır mı sandın?!",
"Paranı da al git!",
"Hadsiz herif, sen kimsin?",
"Yüzsüzlüğe bak ya!"][randint(0, 4)]
def get_currency(**arguments):
"""
Creates a query with given arguments for currency selection. Returns the query.
"""
query = session.query(Currency)
if "id" in arguments:
query = query.filter_by(id=int(arguments["id"]))
if "short_code" in arguments:
query = query.filter_by(short_code=str(arguments["short_code"]))
if "value" in arguments:
query = query.filter_by(value=float(arguments["value"]))
return query
def update_currencies():
"""
Downloads all available currencies and prepares the database.
"""
response = get("http://apilayer.net/api/live?access_key=%s" % (Config.APILAYER_KEY)).json()
for currency_code in response["quotes"].keys():
currency = get_currency(short_code=currency_code[3:]).first()
if not currency:
currency = Currency(short_code=currency_code[3:])
session.add(currency)
session.commit()
currency.value = response["quotes"][currency_code] / Config.CURRENCY_DIVIDER
currency.last_update = get_current_date()
session.commit()
def convert_currency(base_short_code, target_short_code):
"""
Converts the base currency to target currency with database ratio.
Requires database objects.
"""
if base_short_code == target_short_code:
return 1
target_currency = get_currency(short_code=target_short_code).first()
if base_short_code == Config.CURRENCY_CODE:
return target_currency.value
base_currency = get_currency(short_code=base_short_code).first()
if target_short_code == Config.CURRENCY_CODE:
return (1 / base_currency.value)
return (base_currency.value / target_currency.value)
def get_system_variable(variable):
"""
Returns a system variable from database.
"""
return session.query(System).filter_by(variable=variable).first()
def create_system_variable(variable, value):
"""
Creates and returns a system variable with given arguments.
"""
variable = System(variable=variable, value=value)
session.add(variable)
session.commit()
return variable
def get_bet(**arguments):
"""
Creates a query with the given arguments for a bet. Returns the query.
"""
query = session.query(Bet)
if "id" in arguments:
query = query.filter_by(id=int(arguments["id"]))
if "created_by" in arguments:
query = query.filter_by(created_by=int(arguments["created_by"]))
if "bet" in arguments:
query = query.filter_by(bet=str(arguments["bet"]))
if "rate" in arguments:
query = query.filter_by(rate=float(arguments["rate"]))
return query
def create_bet(created_by, bet, rate):
"""
Creates a currency for the user. Returns the created currency or None if not exists.
"""
bet = Bet(created_by=created_by, bet=bet, rate=rate)
session.add(bet)
session.commit()
return bet
def get_user_bet(**arguments):
"""
Creates a query with the given arguments for a bet. Returns the query.
"""
query = session.query(UserBet)
if "id" in arguments:
query = query.filter_by(id=int(arguments["id"]))
if "user_id" in arguments:
query = query.filter_by(user_id=int(arguments["user_id"]))
if "bet_id" in arguments:
query = query.filter_by(bet_id=int(arguments["bet_id"]))
if "deposit" in arguments:
query = query.filter_by(deposit=float(arguments["deposit"]))
return query
def create_user_bet(user_id, bet_id, deposit):
"""
Creates a currency for the user. Returns the created currency or None if not exists.
"""
user_bet = UserBet(user_id=user_id, bet_id=bet_id, deposit=deposit)
session.add(user_bet)
session.commit()
return user_bet
async def send_usage_message(channel, message):
await channel.send("**KULLANIM:** `%s`" % (message))