-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_salt.py
More file actions
135 lines (105 loc) · 3.35 KB
/
Copy pathcommand_salt.py
File metadata and controls
135 lines (105 loc) · 3.35 KB
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
import commands
import time
import util
import discord
cooldown = 86400
@commands.command(condition=lambda line : commands.first_arg_match(line, 'salt'))
async def command_salt(line, message, meta, reng):
args = line.split()
db = reng.db()
cur = db.cursor()
cur.execute('''
SELECT u.salt_amount, u.salt_last_claim, u.salt_remind
FROM user u
WHERE u.user_id = ?
''', (message.author.id,))
salt_amount, last_claim, salt_remind = cur.fetchone()
if len(args) == 1:
return f'You have {salt_amount} salt.'
elif args[1] == 'claim':
now = time.time()
since = now - last_claim
if since < cooldown:
int_since = int(cooldown - since)
h = int_since // 3600
m = (int_since % 3600) // 60
s = int_since % 60
ret = 'Your next available claim is in'
if h > 0:
ret += f' {h} hour{"s" if h > 1 else ""}'
if m > 0:
ret += f' {m} minute{"s" if m > 1 else ""}'
if s > 0 or (h == 0 and m == 0):
ret += f' {s} second{"s" if s > 1 else ""}'
return ret + '.'
else:
cur.execute('''
UPDATE user
SET salt_amount = ?, salt_last_claim = ?, salt_reminded = ?
WHERE user_id = ?
''', (salt_amount + 2000, now, False, message.author.id))
db.commit()
return f'You gained 2000 salt. You now have {salt_amount + 2000} salt.'
elif args[1] == 'gift':
if len(args) != 4:
return '**[Usage]** !salt gift <mention> <amount>'
match = util.user_mention_regex.fullmatch(args[2])
if match == None:
return f'**[Error]** Arg 2 ({args[2]}) must be a valid user mention.'
user_id = int(match.group(1))
user = reng.client.get_user(user_id)
if user == None:
return f'**[Error]** Arg 2 ({args[2]}) must be a valid user mention.'
if message.author.id == user_id:
return f'**[Error]** You cannot gift salt to yourself.'
if message.author.bot:
return f'**[Error]** You cannot gift salt to a bot.'
try:
transaction = int(args[3])
except ValueError:
return f'**[Error]** Arg 3 ({args[3]}) must be a positive integer.'
if transaction <= 0:
f'**[Error]** Arg 3 ({args[3]}) must be a positive integer.'
if salt_amount < transaction:
return f'**[Error]** You do not have enough salt.'
cur.execute('''
INSERT OR IGNORE INTO user(user_id, salt_amount)
VALUES (?, ?)
''', (user_id, transaction))
other_salt_amount = transaction
if cur.rowcount == 0:
cur.execute('''
SELECT u.salt_amount
FROM user u
WHERE u.user_id = ?
''', (user_id,))
other_salt_amount, = cur.fetchone()
other_salt_amount += transaction
cur.execute('''
UPDATE user
SET salt_amount = ?
WHERE user_id = ?
''', (other_salt_amount, user_id))
salt_amount -= transaction
cur.execute('''
UPDATE user
SET salt_amount = ?
WHERE user_id = ?
''', (salt_amount, message.author.id))
db.commit()
try:
await user.create_dm()
await user.send(f'{user.mention} You have been gifted {transaction} salt from {message.author.mention}. You now have {other_salt_amount} salt.')
except discord.errors.Forbidden:
pass
return f'You gifted {transaction} salt. You now have {salt_amount} salt.'
elif args[1] == 'remind':
salt_remind = not salt_remind
cur.execute('''
UPDATE user
SET salt_remind = ?
WHERE user_id = ?
''', (salt_remind, message.author.id))
db.commit()
return f'You have turned o{"n" if salt_remind else "ff"} daily claim reminders.'
return '**[Usage]** !salt [claim|gift|remind]'