-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpacCoinDiscordTipBot.py
More file actions
executable file
·707 lines (568 loc) · 20.3 KB
/
pacCoinDiscordTipBot.py
File metadata and controls
executable file
·707 lines (568 loc) · 20.3 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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import subprocess
import requests
import logging
import discord, asyncio
from discord.ext import commands
import re
import time as time_util
from datetime import datetime
from pytz import timezone
''' begin configuration section '''
BOTNAME = '____CHANGEME____'
BOTUUID = '____CHANGEME____'
BOTCHID = '____CHANGEME____'
TOKEN = '____CHANGEME____'
LOG_FILE = '____CHANGEME____'
APP_EXE = '____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')
def main():
bot.run(TOKEN)
@bot.command(pass_context=True)
async def help(ctx):
text = ctx.message.content
name = ctx.message.author.name
uuid = ctx.message.author.id
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):
uuid = ctx.message.author.id
name = ctx.message.author.name
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 #$PAC address`
`[user] = discord username`
`[amount] = amount of #$PAC to utilise`
*NOTE*:
- don't deposit a significant amount of #$PAC through this #BOT
- make sure that you enter a valid #$PAC 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
cmd = [
APP_EXE,
'-rpcuser=bitcoin',
'-rpcpassword=local321',
'getbalance',
str(user_uuid)
]
ret = rpc_call(cmd)
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)
balance = str('{:,.8f}'.format(balance))
msg = '@{0} your current balance is: {1} $PAC'.format(user_name, balance)
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)
#await bot.say(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
cmd = [
APP_EXE,
'-rpcuser=bitcoin',
'-rpcpassword=local321',
'getaccountaddress',
str(user_uuid)
]
ret = rpc_call(cmd)
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 = '@{} your depositing address is: `{}`'.format(user_name, 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)
# await bot.say(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 $PAC!"
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 silly!"
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 = '@{} has no activity in this chat!'.format(target)
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
cmd = [
APP_EXE,
'-rpcuser=bitcoin',
'-rpcpassword=local321',
'getbalance',
str(user_uuid)
]
ret = rpc_call(cmd)
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 = '@{0} you have insufficent funds.'.format(user_name)
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
cmd = [
APP_EXE,
'-rpcuser=bitcoin',
'-rpcpassword=local321',
'move',
str(user_uuid),
str(target_uuid),
str(amount),
]
tx = rpc_call(cmd)
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 = '@{0} tipped {1} of {2} $PAC'.format(user_name, target, amount)
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 $PAC!"
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
cmd = [
APP_EXE,
'-rpcuser=bitcoin',
'-rpcpassword=local321',
'getbalance',
str(user_uuid)
]
ret = rpc_call(cmd)
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 = '@{0} you have insufficent funds.'.format(user_name)
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 = '{:.8f}'.format(float(amount/online))
for key in sorted(users_online):
time_util.sleep(0.1)
target_uuid = key
target_name = users_online[target_uuid]
cmd = [
APP_EXE,
'-rpcuser=bitcoin',
'-rpcpassword=local321',
'move',
str(user_uuid),
str(target_uuid),
str(pamount),
]
tx = rpc_call(cmd)
if tx is None:
msg = "failed to #tip @{}!".format(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;
if online > 50:
msg = "{} invoked rain spell with {} $PAC over #{} 50 users ({}) and {} other people"\
.format(user_name, pamount, online, user_list, other_list)
else:
msg = "{} invoked rain spell with {} $PAC over #{} users ({})"\
.format(user_name, pamount, online, 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
if not isValidAddress(message[1]):
msg = 'Please input a valid $PAC 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]
cmd = [
APP_EXE,
'-rpcuser=bitcoin',
'-rpcpassword=local321',
'getbalance' ,
str(user_uuid)
]
ret = rpc_call(cmd)
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 = '@{0} you have insufficent funds.'.format(user_name)
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
cmd = [
APP_EXE,
'-rpcuser=bitcoin',
'-rpcpassword=local321',
'sendfrom',
str(user_uuid),
str(address),
str(amount),
]
tx = rpc_call(cmd)
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 = '@{0} has successfully withdrew {2} $PAC to address: {1}'\
.format(user_name, address, amount)
embed = discord.Embed(color=discord.Color.green())
embed.add_field(name="WITHDRAW", value=msg, inline=True)
await bot.say(embed=embed)
def yobit(market):
getmarket = 'https://yobit.net/api/3/ticker/pac_'+market.lower()
response = requests.get(getmarket)
json_data = response.json()
if not json_data or not json_data.get('pac_btc') or json_data.get('error'):
return {}
json_data = json_data.get('pac_btc')
message = {}
message['*LastPrice*'] = '{:,.8f}'.format(float(json_data['last']))
message['*AskPrice*'] = '{:,.8f}'.format(float(json_data['sell']))
message['*BidPrice*'] = '{:,.8f}'.format(float(json_data['buy']))
message['*Volume*'] = '{:,.8f}'.format(float(json_data['vol_cur']))
message['*Volume*'] = '{:,.8f}'.format(float(json_data['vol']))
return message
def cryptopia(market):
getmarket = 'https://www.cryptopia.co.nz/api/GetMarket/$PAC_'+market
response = requests.get(getmarket)
json_data = response.json()
if not json_data or json_data.get('error'):
return {}
if not json_data['Data']:
return {}
message = {}
message['*LastPrice*'] = '{:,.8f}'.format(json_data['Data']['LastPrice'])
message['*AskPrice*'] = '{:,.8f}'.format(json_data['Data']['AskPrice'])
message['*BidPrice*'] = '{:,.8f}'.format(json_data['Data']['BidPrice'])
message['*Volume*'] = '{:,.8f}'.format(json_data['Data']['BaseVolume'])
return message
def tradesatoshi(market):
getmarket = 'https://tradesatoshi.com/api/public/GetTicker?market=$PAC_'+market
response = requests.get(getmarket)
json_data = response.json()
if not json_data or not json_data.get('result') or json_data.get('error'):
return {}
json_data = json_data.get('result')
message = {}
message['*LastPrice*'] = '{:,.8f}'.format(float(json_data['last']))
message['*AskPrice*'] = '{:,.8f}'.format(float(json_data['ask']))
message['*BidPrice*'] = '{:,.8f}'.format(float(json_data['bid']))
return message
@bot.command(pass_context=True)
async def price(ctx):
markets = ('BTC', 'LTC', 'BCH', 'DOGE', 'USDT')
message = ctx.message.content.split(' ')
if len(message) != 2:
msg = 'Please use !price [btc/ltc/bch/doge/usdt]!'
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
market = message[1].upper()
if market not in markets:
msg = 'This market is not available!'
embed = discord.Embed(color=discord.Color.red())
embed.add_field(name="ERROR", value=msg, inline=True)
await bot.say(embed=embed)
return False
message = {
"yobit": yobit(market),
"cryptopia": cryptopia(market),
"tradesatoshi": tradesatoshi(market)
}
msg = json.dumps(message, indent=4, sort_keys=True)
embed = discord.Embed(color=0x00b3b3)
embed.add_field(name="PRICE", 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.command(pass_context=True)
async def mcap(ctx):
user_name = ctx.message.author.name
user_uuid = ctx.message.author.id
await bot.say('work in progress!')
@bot.command(pass_context=True)
async def moon(ctx):
user_name = ctx.message.author.name
user_uuid = ctx.message.author.id
await bot.say('Moon mission inbound!')
@bot.command(pass_context=True)
async def hi(ctx):
name = ctx.message.author.name
uuid = ctx.message.author.id
msg = 'Hello @{}[{}], how are you doing today?'.format(name, uuid)
await bot.say(msg)
@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("@{} [#{}]: {}".format(user, uuid, msg))
cuid = str(message.channel.id)
if cuid != BOTCHID and message.content.startswith('!'):
logger.info("wrong #BOTCHID [@{}]".format(BOTCHID))
await bot.delete_message(message)
else:
await bot.process_commands(message)
def rpc_call(cmd):
try:
result = subprocess.run(cmd,
stdout=subprocess.PIPE)
except:
return
ret = result.stdout.strip()
ret = ret.decode('utf-8')
if len(ret) == 0:
return
else:
return ret
def isValidAddress(param):
if len(param) < 30:
return False
elif len(param) > 35:
return False
elif not param.isalnum():
return False
elif not param[0] == 'P':
return False
else:
return True
def isValidUsername(user):
if re.match('^<@\!?[0-9]+>$', user):
return True
else:
return False
def isValidAmount(amount):
try:
float(amount)
except ValueError:
return False
else:
return True
if __name__ == '__main__':
main()