forked from valgilbert/pacCoinTipBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoxdCoinDiscordTipBot.py
More file actions
executable file
·439 lines (394 loc) · 15.4 KB
/
foxdCoinDiscordTipBot.py
File metadata and controls
executable file
·439 lines (394 loc) · 15.4 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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
import json
import subprocess
from typing import Any
import time as time_util
from datetime import datetime
import logging
import discord
from discord.ext import commands
from pytz import timezone
''' begin configuration section '''
TOKEN = '____CHANGEME____'
BOTNAME = '____CHANGEME____'
BOTUUID = '____CHANGEME____'
BOTCHID = '____CHANGEME____'
CLI_EXE = '____CHANGEME____'
RPC_USER = '____CHANGEME____'
RPC_PASS = '____CHANGEME____'
LOG_FILE = '____CHANGEME____'
''' end configuration section '''
''' DO NOT EDIT BELLOW THS LINE '''
fh = logging.FileHandler(LOG_FILE)
fh.setLevel(logging.INFO)
ft = logging.Formatter('%(asctime)-15s - %(message)s')
fh.setFormatter(ft)
logger = logging.getLogger(BOTNAME)
logger.setLevel(logging.INFO)
logger.addHandler(fh)
bot = commands.Bot(command_prefix='!')
bot.remove_command('help')
@bot.command(pass_context=True)
async def help():
n="The following commands are at your disposal:"
v="!info, !balance, !deposit, !withdraw, !tip, !rain, !price, and !time"
msg = discord.Embed(color=0x00b3b3)
msg.add_field(name=n, value=v, inline=False)
await bot.say(embed=msg)
@bot.command(pass_context=True)
async def info(ctx):
msg = \
"""
```
INFO SECTION
```
commands like *!tip* & *!withdraw* have a specfic format,\
use them like so:
Tipping format:
`!tip @[user] [amount] (without brackets)`
Rain format:
`!rain [amount] (without brackets)`
Withdrawing format:
`!withdraw [address] [amount] (without brackets)`
WHERE:
`[address] = withdraw #$FOXD address`
`[user] = discord username`
`[amount] = amount of #$FOXD to utilise`
*NOTE*:
- don't deposit a significant amount of #$FOXD through this #BOT
- make sure that you enter a valid #$FOXD address when you perform a withdraw
- we are not responsible of your funds if something bad happen to this #BOT
```
USE THIS #BOT AT YOUR OWN RISK
```
"""
embed = discord.Embed(color=0x00b3b3)
embed.add_field(name="\a", value=msg, inline=False)
await bot.say(embed=embed)
@bot.command(pass_context=True)
async def balance(ctx):
user_name = ctx.message.author.name
user_uuid = ctx.message.author.id
if user_name is None:
msg = "Invalid username!"
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
if user_uuid is None:
msg = "Invalid userid!"
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
ret = await rpc_call('getbalance', [str(user_uuid)])
if ret is None:
msg = 'rpc internal error!'
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
balance = str(round(float(balance), 8))
msg = f'@{user_name} your current balance is: {balance} $FOXD'
embed = discord.Embed(color=discord.Color.green())
embed.add_field(name="BALANCE", value=msg, inline=True)
await bot.send_message(ctx.message.author, embed=embed)
@bot.command(pass_context=True)
async def deposit(ctx):
user_name = ctx.message.author.name
user_uuid = ctx.message.author.id
if user_name is None:
msg = "Invalid username!"
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
if user_uuid is None:
msg = "Invalid userid!"
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
ret = await rpc_call('getaccountaddress', str(user_uuid))
if ret is None:
msg = 'rpc internal error!'
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
msg = f'@{user_name} your depositing address is: `{ret}`'
embed = discord.Embed(color=discord.Color.green())
embed.add_field(name="DEPOSIT", value=msg, inline=True)
await bot.send_message(ctx.message.author, embed=embed)
@bot.command(pass_context=True)
async def tip(ctx):
user_name = ctx.message.author.name
user_uuid = ctx.message.author.id
if user_name is None:
msg = "Invalid username!"
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
if user_uuid is None:
msg = "Invalid userid!"
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
message = ctx.message.content.split(' ')
if len(message) != 3:
msg = "Please use !tip <username> <amount>!"
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
if not isValidUsername(message[1]):
msg = "Please input a valid username (ex: @JonDoe01#0964)!"
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
if not isValidAmount(message[2]):
msg = "Please input a valid amount (ex: 1000)!"
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
amount = float(message[2])
target = message[1]
uuid = list(filter(str.isdigit, target))
target_uuid = str(''.join(uuid))
if amount > 100000 or amount < 1:
msg = "Please send value between 1 and 100,000 $FOXD!"
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
if target_uuid == user_uuid:
msg = "You can't tip yourself!"
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
if target_uuid == BOTUUID:
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value="HODL.", inline=True)
await bot.say(embed=embed)
return False
if not target_uuid:
msg = f'@{target} has no activity in this chat!'
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
ret = await rpc_call('getbalance', [str(user_uuid)])
if ret is None:
msg = 'rpc internal error!'
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
balance = float(ret)
if balance < amount:
msg = f'@{user_name} you have insufficent funds.'
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
tx = await rpc_call('move', [str(user_uuid), str(target_uuid), str(amount)])
if tx is None:
msg = 'rpc internal error!'
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
msg = f'@{user_name} tipped {target} of {amount} $FOXD'
embed = discord.Embed(color=discord.Color.green())
embed.add_field(name="TIP", value=msg, inline=True)
await bot.say(embed=embed)
@bot.command(pass_context=True)
async def rain(ctx):
user_name = ctx.message.author.name
user_uuid = ctx.message.author.id
if user_name is None:
msg = "Invalid username!"
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
if user_uuid is None:
msg = "Invalid userid!"
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
message = ctx.message.content.split(' ')
if len(message) != 2:
msg = "Please use !rain <amount>!"
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
if not isValidAmount(message[1]):
msg = "Please input a valid amount (ex: 1000)!"
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
amount = float(message[1])
if amount > 100000 or amount < 1:
msg = "Please send value between 1 and 100,000 $FOXD!"
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
ret = await rpc_call('getbalance', [str(user_uuid)])
if ret is None:
msg = 'rpc internal error!'
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
balance = float(ret)
if balance < amount:
msg = f'@{user_name} you have insufficent funds.'
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
users_online = {}
for server in bot.servers:
for u in server.members:
if str(u.status) is 'online': users_online[u.id] = u.name
online = len(users_online)
pamount = str(float(amount/online))
for key in sorted(users_online):
time_util.sleep(0.1)
target_uuid = key
target_name = users_online[target_uuid]
tx = await rpc_call('move', [str(user_uuid), str(target_uuid), str(pamount)])
if tx is None:
msg = f"failed to #tip @{target_name}!"
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
sub_list = list(users_online.values())
user_list = ",".join(sub_list[0:2])
other_list = online - 50
_msg = f"{user_name} invoked rain spell with {pamount} $FOXD over #{online}"
if online > 50: msg = f"{_msg} 50 users ({user_list}) and {other_list} other people"
else: msg = f"{_msg} users ({user_list})"
embed = discord.Embed(color=discord.Color.green())
embed.add_field(name="RAIN", value=msg, inline=True)
await bot.say(embed=embed)
@bot.command(pass_context=True)
async def withdraw(ctx):
user_name = ctx.message.author.name
user_uuid = ctx.message.author.id
if user_name is None:
msg = "Invalid username!"
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
if user_uuid is None:
msg = "Invalid userid!"
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
message = ctx.message.content.split(' ')
if len(message) != 3:
msg = 'Please use !withdraw <address> <amount>!'
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
is_valid = await rpc_call('validateaddress', [message[1]])['isvalid']
if not is_valid:
msg = 'Please input a valid $FOXD address!'
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
if not isValidAmount(message[2]):
msg = 'Please input a valid amount (ex: 1000)!'
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
amount = float(message[2])
address = message[1]
ret = await rpc_call('getbalance', [str(user_uuid)])
if ret is None:
msg = 'rpc internal error!'
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
balance = float(ret)
if balance < amount+1:
msg = f'@{user_name} you have insufficent funds.'
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
tx = await rpc_call('sendfrom', (str(user_uuid), str(address), str(amount)))
if tx is None:
msg = 'rpc internal error!'
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
msg = f'@{user_name} has successfully withdrew {amount} $FOXD to address: {address}'
embed = discord.Embed(color=discord.Color.green())
embed.add_field(name="WITHDRAW", value=msg, inline=True)
await bot.say(embed=embed)
@bot.command(pass_context=True)
async def time(ctx):
msg = datetime.utcnow().strftime("%a %b %d %H:%M:%S %Y") + " UTC\n"
msg += datetime.now(timezone('EST')).strftime("%a %b %d %H:%M:%S %Y %Z")
embed = discord.Embed(color=0x00b3b3)
embed.add_field(name="TIME", value=msg, inline=True)
await bot.say(embed=embed)
@bot.event
async def on_ready(): print('Bot is ready for use!')
@bot.event
async def on_message(message, user: discord.Member = None):
if message is None: return
if user is None: user = message.author
msg = message.content
uuid = user.id
user = user.name
logger.info(f"@{user} [#{uuid}]: {msg}")
cuid = str(message.channel.id)
if cuid != BOTCHID and message.content.startswith('!'):
logger.info(f"wrong #BOTCHID [@{BOTCHID}]")
await bot.delete_message(message)
else: await bot.process_commands(message)
async def rpc_call(method: str, params: list[Any]) -> dict[str, Any] | None:
try: result = subprocess.run((
lambda m, p: [CLI_EXE,
f'-rpcuser={RPC_USER}',
f'-rpcpassword={RPC_PASS}',
m, *p])(method, params),
stdout = subprocess.PIPE)
except: return
ret = result.stdout.strip().decode('utf-8')
if len(ret) == 0: return
else: return json.loads(ret)
def isValidUsername(user):
if re.match('^<@\!?[0-9]+>$', user): return True
else: return False
def isValidAmount(amount):
try:
if type(amount) is not float: raise ValueError
else: return True
except ValueError: return False
def main(): bot.run(TOKEN)
if __name__ == '__main__':
main()