-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
593 lines (481 loc) · 23.7 KB
/
main.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
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
from auth import access_token, token, api_token, client_id, webhook_url, database
from twitchio.ext import commands
from pathlib import Path
import traceback
import requests
import aiohttp
import asyncpg
import asyncio
import logging
import json
import re
twitch = requests.get('https://api.twitch.tv/kraken/channel', headers={
'Content-Type': 'application/vnd.twitchtv.v5+json',
'Client-ID': client_id,
'Authorization': access_token
})
channel_id = twitch.json()
logger = logging.getLogger('main')
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('logs.log')
fh.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter('[%(asctime)s] [%(levelname)s]: %(message)s', '%d/%m/%Y %H:%M')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)
async def check_points(channel, user):
with open('./channels.json') as channels_file:
content = json.load(channels_file)
channel = content[channel]['id']
async def fetch(session, url):
async with session.get(url) as response:
return await response.json()
async def main():
async with aiohttp.ClientSession() as session:
r = await fetch(session, f'https://api.streamelements.com/kappa/v2/points/{channel}/{user}')
point_amount = r["points"]
return point_amount
return await main()
async def bulk_add_points(channel, data):
with open('./channels.json') as channels_file:
content = json.load(channels_file)
authtoken = content[channel]['token']
channelid = content[channel]['id']
print(data)
async def fetch(session, url):
async with session.put(url, headers={"Authorization": authtoken,
"Content-Type": "application/json"}, json=data) as response:
return response
async def main():
async with aiohttp.ClientSession() as session:
await fetch(session, f'https://api.streamelements.com/kappa/v2/points/{channelid}')
await main()
async def add_points(channel, user, amount):
with open('./channels.json') as channels_file:
content = json.load(channels_file)
authtoken = content[channel]['token']
channel = content[channel]['id']
async def fetch(session, url):
async with session.put(url, headers={"Authorization": authtoken}) as response:
return await response.json()
async def main():
async with aiohttp.ClientSession() as session:
r = await fetch(session, f'https://api.streamelements.com/kappa/v2/points/{channel}/{user}/{amount}')
logger.info(r)
await main()
async def postto_webhook(content):
async def fetch(session, url):
async with session.post(url, headers={"Content-Type": "application/json"}, data={content}) as response:
return await response.json()
async def main():
async with aiohttp.ClientSession() as session:
r = await fetch(session, webhook_url)
logger.info(r)
await main()
with open('./channels.json', 'r+') as channels_file:
channels = json.load(channels_file)
channels = channels['channels']
class Botto(commands.Bot):
def __init__(self):
super().__init__(prefix=['!', '?'],
irc_token=token,
api_token=api_token,
client_id=channel_id,
nick='adure_bot',
initial_channels=channels)
#################
# ON READY EVENT
#################
async def event_ready(self):
logger.info(f"Logged in as {self.nick}")
logger.info(f"Connected to channels:.. {', '.join(self.initial_channels)}")
#ws = bot._ws
#for channel in ws._initial_channels:
# await ws.send_privmsg(channel, "Hi There HeyGuys")
###################
# ON MESSAGE EVENT
###################
async def event_message(self, message):
print(f"{message.author.name}: {message.content}")
await self.handle_commands(message)
###################
# ON COMMAND ERROR
###################
async def event_command_error(self, ctx, error):
if isinstance(error, commands.CommandNotFound):
pass
else:
logger.error(f"{error} - {ctx.channel.name}")
traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr)
##################
# RESTART COMMAND
##################
@commands.command(aliases=['restart'])
async def restart_command(self, message):
if message.message.tags['mod'] == 1 or any(message.author.name in s for s in channels):
logger.info("Restarting...")
await message.send("BibleThump cya")
restart_program()
#######################
# OPEN BETTING COMMAND
#######################
@commands.command(aliases=['open'])
async def open_command(self, message):
if message.message.tags['mod'] == 1 or any(message.author.name in s for s in channels):
open_channel = message.channel.name
betters_file_path = Path(f"./{open_channel}_betters.json")
betters_dict = {
'is_open': 1,
'betters': []
}
if betters_file_path.exists():
with open(betters_file_path, 'w+') as betters_file:
betters_file.seek(0)
json.dump(betters_dict, betters_file, separators=(',', ': '), indent=4)
betters_file.truncate()
logger.info(f"Cleared {open_channel}_betters.json file")
else:
with open(f"./{open_channel}_betters.json", 'w+') as new_betters_file:
json.dump(betters_dict, new_betters_file, separators=(',', ': '), indent=4)
logger.info(f"Created {open_channel}_betters.json file")
logger.info(f"Betting open - {message.channel.name}")
await message.send("Betting open! Use '!bet <outcome> <wager>' to bet on the game (!howtobet)")
########################
# CLOSE BETTING COMMAND
########################
@commands.command(aliases=['close'])
async def close_command(self, message):
if message.message.tags['mod'] == 1 or any(message.author.name in s for s in channels):
close_channel = message.channel.name
win_votes = 0
loss_votes = 0
total_wager = 0
with open(f'./{close_channel}_betters.json', 'r+') as betters_file:
contents = json.load(betters_file)
contents['is_open'] = 0
betters_file.seek(0)
json.dump(contents, betters_file, separators=(',', ': '), indent=4)
betters_file.truncate()
logger.info(f"Betting closed - {message.channel.name}")
await message.send("Betting closed!")
if len(contents['betters']) != 0:
for user in contents['betters']:
if user['outcome'] == 'win':
win_votes += 1
else:
loss_votes += 1
total_wager += int(user['wager'])
w_percentage = (win_votes / len(contents['betters'])) * 100
l_percentage = (loss_votes / len(contents['betters'])) * 100
logger.info(f"{total_wager} Points bet | {win_votes}({w_percentage:.1f}%) voted win | {loss_votes}({l_percentage:.1f}%) voted lose")
await message.send(f"{total_wager} Points bet | {win_votes}({w_percentage:.1f}%) voted win | {loss_votes}({l_percentage:.1f}%) voted lose")
####################
# ENTER BET COMMAND
####################
@commands.command(aliases=['bet', 'guess'])
async def bet_command(self, message, outcome, wager):
bet_channel = message.channel.name
bettername = message.author.name
userpoints = await check_points(bet_channel, bettername)
users = []
with open(f'./{bet_channel}_betters.json', 'r+') as betters_file:
contents = json.load(betters_file)
is_open = contents['is_open']
if is_open == 0:
logger.error(f"{bettername} tried to bet while betting is closed")
await message.send(f"{bettername}, betting is closed")
return
if outcome not in ['win', 'loss', 'lose']:
logger.error(f"Bet attempt with non-accepted outcome by {bettername}")
await message.send(f"{bettername}, please enter an accepted outcome (win, loss, lose)")
return
if wager == 'all':
wager = str(userpoints)
if re.fullmatch('^\d+\%', wager):
pnumber = wager.replace('%', '')
wager = (int(pnumber) * userpoints) / 100
wager = str(round(wager))
if wager.isdigit() is False:
logger.error(f"Bet attempt with non-digit wager by {bettername}")
await message.send(f"{bettername}, your wager is not a number")
return
if userpoints < int(wager):
logger.error(f"{bettername} tried to enter with insufficient points")
await message.send(f"{bettername}, you do not have enough points")
return
if int(wager) > 5000000:
logger.error(f"{bettername} wager over 5000000")
await message.send(f"{bettername}, max wager is 5000000")
return
if len(contents['betters']) != 0:
for user in contents['betters']:
users.append(user['user'])
if bettername in users:
logger.error(f"{bettername} tried to bet, but they have already entered")
await message.send(f"{bettername}, you can only bet once")
return
bet_dict = {
'user': bettername,
'outcome': outcome,
'wager': wager
}
contents['betters'].append(bet_dict)
betters_file.seek(0)
json.dump(contents, betters_file, separators=(',', ': '), indent=4)
betters_file.truncate()
logger.info(f"Entered {bettername} betting {outcome} with a {wager} Point wager")
await message.send(f"Entered {bettername} betting {outcome} with a {wager} Point wager")
await add_points(bet_channel, bettername, str(int(wager) * -1))
@commands.command(aliases=['win'])
async def win_command(self, message):
if message.message.tags['mod'] == 1 or any(message.author.name in s for s in channels):
channel = message.channel.name
win_bets = 0
points_won = 0
points_lost = 0
with open(f"{channel}_betters.json") as betters_file:
contents = json.load(betters_file)
is_open = contents['is_open']
if is_open == 1:
logger.info(f"Betting is still open - {channel}")
await message.send("Betting is still open!")
return
logger.info(f"Game won - {channel}")
await message.send("Win! PogChamp")
if len(contents['betters']) == 0:
logger.info(f"No one has bet - {channel}")
await message.send("No one has bet!")
return
winners = {
"mode": "add",
"users": []
}
for user in contents['betters']:
if user['outcome'] == 'win':
win_bets += 1
points_won += int(user['wager'])
winners['users'].append({'username': user['user'], 'current': int(user['wager']) * 2})
else:
points_lost += int(user['wager'])
await asyncio.sleep(0.1)
await bulk_add_points(channel, winners)
percentage = (win_bets / len(contents['betters'])) * 100
logger.info(str("%.2f" % percentage)+f"% of people got it right. {str(points_won)} Points won. {str(points_lost)} Points lost.")
await message.send(str("%.2f" % percentage)+f"% of people got it right. {str(points_won)} Points won. {str(points_lost)} Points lost.")
@commands.command(aliases=['loss', 'lose'])
async def loss_command(self, message):
if message.message.tags['mod'] == 1 or any(message.author.name in s for s in channels):
channel = message.channel.name
loss_bets = 0
points_won = 0
points_lost = 0
with open(f"{channel}_betters.json") as betters_file:
contents = json.load(betters_file)
is_open = contents['is_open']
if is_open == 1:
logger.info(f"Betting is still open - {channel}")
await message.send("Betting is still open!")
return
logger.info(f"Game lost - {channel}")
await message.send("Loss! BibleThump")
if len(contents['betters']) == 0:
logger.info(f"No one has bet - {channel}")
await message.send("No one has bet!")
return
winners = {
'users': [],
'mode': 'add'
}
for user in contents['betters']:
if user['outcome'] == 'win':
points_lost += int(user['wager'])
else:
loss_bets += 1
points_won += int(user['wager'])
winners['users'].append({'username': user['user'], 'current': int(user['wager']) * 2})
await asyncio.sleep(0.1)
await bulk_add_points(channel, winners)
percentage = (loss_bets / len(contents['betters'])) * 100
logger.info(str("%.2f" % percentage)+f"% of people got it right. {str(points_won)} Points won. {str(points_lost)} Points lost.")
await message.send(str("%.2f" % percentage)+f"% of people got it right. {str(points_won)} Points won. {str(points_lost)} Points lost.")
@commands.command(aliases=['status'])
async def status_command(self, message):
if message.message.tags['mod'] == 1 or any(message.author.name in s for s in channels):
channel = message.channel.name
with open(f"{channel}_betters.json") as betters_file:
contents = json.load(betters_file)
is_open = contents['is_open']
if is_open == 0:
logger.info(f"Betting is closed. {str(len(contents['betters']))} betters in list. - {channel}")
await message.send(f"Betting is closed. {str(len(contents['betters']))} betters in list.")
else:
logger.info(f"Betting is open. {str(len(contents['betters']))} betters in list. - {channel}")
await message.send(f"Betting is open. {str(len(contents['betters']))} betters in list.")
# Strawpoll create poll api endpoint deprecated :(
@commands.command(aliases=['strawpoll'])
async def strawpoll_command(self, message, question, *options):
if message.message.tags['mod'] == 1 or any(message.author.name in s for s in channels):
topoll = {
"title": question,
"options": list(options),
"multi": False
}
async def fetch(session, url):
async with session.post(url, headers={'Content-Type': 'application/json'}, data=topoll) as response:
return await response.json()
async def main():
async with aiohttp.ClientSession() as session:
r = await fetch(session, f'https://www.strawpoll.me/api/v2/polls')
print(r)
await main()
pollurl = f"https://www.strawpoll.me/{id}"
logger.info(f"Created poll: {pollurl} - {message.channel.name}")
await message.send(pollurl)
@commands.command(aliases=['print'])
async def print_command(self, message):
channel = message.channel.name
with open(f"{channel}_betters.json") as betters_file:
contents = json.load(betters_file)
if len(contents['betters']) == 0:
await message.send("No betters in list!")
logger.warning("No betters in list on print command")
return
betters_print = []
for bet in contents['betters']:
betters_print.append(f"{bet['user']} {bet['outcome']} {bet['wager']}")
form_message = ', '.join(betters_print)
await message.send(form_message)
logger.info(f"Sent print command to {channel} as {form_message}")
@commands.command(aliases=['clip'])
async def clip_command(self, message, name):
clip = await self.create_clip(api_token, message.channel.name)
await postto_webhook(clip)
@commands.command(aliases=['createvote'])
async def createvote_command(self, message, question, *options):
if message.message.tags['mod'] == 1 or any(message.author.name in s for s in channels):
vote_channel = message.channel
voters_file_path = Path(f"./{vote_channel.name}_voters.json")
voters_dict = {
'is_running': 1,
'question': question,
'options': list(options),
'voters': []
}
if voters_file_path.exists():
with open(voters_file_path, 'w+') as voters_file:
voters_file.seek(0)
json.dump(voters_dict, voters_file, separators=(',', ': '), indent=4)
voters_file.truncate()
logger.info(f"Cleared {vote_channel.name}_voters.json file")
else:
with open(f"./{vote_channel.name}_voters.json", 'w+') as new_voters_file:
json.dump(voters_dict, new_voters_file, separators=(',', ': '), indent=4)
logger.info(f"Created {vote_channel.name}_voters.json file")
await vote_channel.send("Vote created! You may now enter the vote with an accepted outcome, using !vote <outcome> <wager> (wager is optional)")
@commands.command(aliases=['endvote'])
async def endvote_command(self, message, outcome):
if message.message.tags['mod'] == 1 or any(message.author.name in s for s in channels):
vote_channel = message.channel
vote_user = message.author.name
correct_votes = 0
incorrect_votes = 0
points_won = 0
points_lost = 0
with open(f'./{vote_channel.name}_voters.json', 'r+') as voters_file:
contents = json.load(voters_file)
options = contents['options']
contents['is_running'] = 0
voters_file.seek(0)
json.dump(contents, voters_file, separators=(',', ': '), indent=4)
voters_file.truncate()
logger.info(f"Voting finished - {vote_channel.name}")
await vote_channel.send("Voting finished!")
if len(contents['voters']) == 0:
logger.info(f"No one has voted - {vote_channel.name}")
await vote_channel.send("No one has voted!")
return
if outcome not in options:
await vote_channel.send(f"{vote_user}, please use an accepted outcome entered on vote creation ({', '.join(options)})")
return
winners = {
"mode": "add",
"users": []
}
for user in contents['voters']:
if user['outcome'] == outcome:
correct_votes += 1
points_won += int(user['wager'])
winners['users'].append({'username': user['user'], 'current': int(user['wager']) * 2})
else:
points_lost += int(user['wager'])
await asyncio.sleep(0.1)
await bulk_add_points(vote_channel.name, winners)
percentage = (correct_votes / len(contents['voters'])) * 100
logger.info(str("%.2f" % percentage)+f"% of people voted correctly. {str(points_won)} Points won. {str(points_lost)} Points lost.")
await vote_channel.send(str("%.2f" % percentage)+f"% of people voted correctly. {str(points_won)} Points won. {str(points_lost)} Points lost.")
@commands.command(aliases=['vote'])
async def vote_command(self, message, outcome, wager='0'):
vote_channel = message.channel
vote_user = message.author.name
voters = []
with open(f'./{vote_channel.name}_voters.json', 'r+') as voters_file:
contents = json.load(voters_file)
options = contents['options']
is_running = contents['is_running']
if bool(is_running) == False:
await vote_channel.send(f"{vote_user}, no vote currently running")
return
if outcome not in options:
await vote_channel.send(f"{vote_user}, please enter an accepted outcome ({', '.join(options)})")
return
if len(contents['voters']) != 0:
for user in contents['voters']:
voters.append(user['user'])
if vote_user in voters:
logger.error(f"{vote_user} tried to vote, but they have already voted")
await message.send(f"{vote_user}, you can only vote once")
return
if wager == '0':
pass
else:
userpoints = await check_points(vote_channel.name, vote_user)
if wager == 'all':
wager = str(userpoints)
if re.fullmatch('^\d+\%', wager):
pnumber = wager.replace('%', '')
wager = (int(pnumber) * userpoints) / 100
wager = str(round(wager))
if wager.isdigit() is False:
logger.error(f"Vote attempt with non-digit wager by {vote_user}")
await message.send(f"{vote_user}, your wager is not a number")
return
if userpoints < int(wager):
logger.error(f"{vote_user} tried to enter with insufficient points")
await message.send(f"{vote_user}, you do not have enough points")
return
await add_points(vote_channel.name, vote_user, str(int(wager) * -1))
vote_dict = {
'user': vote_user,
'outcome': outcome,
'wager': wager
}
contents['voters'].append(vote_dict)
voters_file.seek(0)
json.dump(contents, voters_file, separators=(',', ': '), indent=4)
voters_file.truncate()
logger.info(f"Entered {vote_user} voting {outcome} with a {wager} Point wager")
await message.send(f"Entered {vote_user} voting {outcome} with a {wager} Point wager")
# TODO: integrate postgres database
async def init_database():
pool = await asyncpg.create_pool(database='postgres',
user=database['user'],
port=database['port'],
password=database['password'])
logger.info(f"Connected to database... User: {database['user']}, Port: {database['port']}")
# RUN IT
bot = Botto()
bot.loop.run_until_complete(init_database())
bot.run()