-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChatWars-v2.py
567 lines (487 loc) · 26.3 KB
/
ChatWars-v2.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
from telethon import TelegramClient, events, sync
from telethon.tl.functions.messages import SendMessageRequest
from telethon.tl.functions.messages import GetBotCallbackAnswerRequest
from datetime import datetime
import random, re, asyncio, logging
import json, urllib.request
# logging.basicConfig(level=logging.DEBUG)
# == V2 ==
# TODO evaluate buttons
# TODO improve /stop functionality
# TODO improved logging
# TODO more "ERROR: Unhandled Exception"
# TODO Config file
# TODO Configurable ERROR action (such as /g_def)
# TODO Allow other(s) to send control messages on behalf of self
# TODO
# == V3 ==
# TODO A bot that you send control commands to
# TODO Additionally the bot stores your config in a db
# TODO This config controls multiple delay timings,
# TODO logging, error_action, etc...
# global variables
bot_active = False
current_activity = None # Valley / Swamp / Arena
activity_counter = None
start_event = None
arena_test_closed = None
use_found_energy = False
exhaust_energy = False
exhaust_activity_reply_quests = False
results = 0
quest_return_json = None
# user config
Control_Channel_URL = 'https://t.me/joinchat/Fa7...'
Session_File = 'Username'
quest_return_url = 'https://raw.githubusercontent.com/TheRealNoob/TelegramBots/master/quests.json'
# Method override on the Telegram client : Force a 1-5 sec delay on all outgoing messages to Chat Wars
class CreateClient(TelegramClient):
async def __call__(self, request, **kw):
# print(request)
if (isinstance(request, SendMessageRequest) or isinstance(request, GetBotCallbackAnswerRequest)) and hasattr(
request.peer, 'user_id') and request.peer.user_id == ChatWars_Channel.id:
if bot_active:
await asyncio.sleep(random.randint(1, 5))
return await super().__call__(request, **kw)
else:
return await super().__call__(request, **kw)
def __init__(self, **kwargs):
super().__init__(api_id=123456,
api_hash='qwertyuiopasdfghjkl123456789',
connection_retries=1000,
**kwargs)
sync.syncify(CreateClient)
# user config
client = CreateClient(session=Session_File).start()
Control_Channel = client.get_entity(Control_Channel_URL)
ChatWars_Channel = client.get_entity('@chtwrsbot')
# regex strings
reg_quests = r"""🌲Forest \d{1,2}min
Many things can happen in the forest.
🍄Swamp \d{1,2}min
Who knows what is lurking in mud.
🏔Mountain Valley \d{1,2}min(?: 🔥){0,1}
Watch out for landslides.
🗡Foray 🔋🔋
Foray is a dangerous activity. Someone can notice you and may beat you up. But if you go unnoticed, you will acquire a lot of loot.
📯Arena [🔒]{0,1}
Arena isn't a place for the weak. Here you fight against other players and if you stand victorious, you acquire precious experience."""
reg_quests_arena_open = r"""🌲Forest \d{1,2}min
Many things can happen in the forest.
🍄Swamp \d{1,2}min
Who knows what is lurking in mud.
🏔Mountain Valley \d{1,2}min
Watch out for landslides.
🗡Foray 🔋🔋
Foray is a dangerous activity. Someone can notice you and may beat you up. But if you go unnoticed, you will acquire a lot of loot.
📯Arena
Arena isn't a place for the weak. Here you fight against other players and if you stand victorious, you acquire precious experience."""
reg_quests_arena_locked = r"""🌲Forest \d{1,2}min
Many things can happen in the forest.
🍄Swamp \d{1,2}min
Who knows what is lurking in mud.
🏔Mountain Valley \d{1,2}min
Watch out for landslides.
🗡Foray 🔋🔋
Foray is a dangerous activity. Someone can notice you and may beat you up. But if you go unnoticed, you will acquire a lot of loot.
📯Arena 🔒
Arena isn't a place for the weak. Here you fight against other players and if you stand victorious, you acquire precious experience."""
reg_arena_capture_remaining_fights = """📯Welcome to Arena!
Dirty air is soaked with the thick smell of blood\. No one ends up here by an accident: you can't leave once you begin your battle\. I hope, your sword is sharp and your shield is steady\.
Your rank: \d+
Your fights: (\d)\/5
Combat Ranking: \/top5
Fastest-growing: \/top6
Entrance fee: 5💰"""
reg_arena_response = """-{0,1}\d{0,4}❤️.+
VS
-{0,1}\d{0,4}❤️.+
.+! \d{0,4}(?:\([-+]{0,1}\d{1,4}\)){0,1}
.+! \d{0,4}(?:\([-+]{0,1}\d{1,4}\)){0,1}
.+ stands victorious over .+
You received: \d{0,4} exp.
Leaderboard of fighters are updated: .top5 & .top6.*"""
reg_error_not_enough_stamina = """Not enough stamina. Come back after you take a rest.
To get more stamina, invite friends to the game via invite link. Press /promo to get it."""
reg_valley_capture_time = """Mountains can be a dangerous place. You decided to investigate, what's going on. You'll be back in (\d{1,2}) minutes."""
reg_swamp_capture_time = """An adventure is calling. But you went to a swamp. You'll be back in (\d{1,2}) minutes."""
reg_error_too_busy = """You are too busy with a different adventure. Try a bit later."""
reg_arena_too_dark = """It’s hard to see your opponent in the dark. Wait until the morning."""
reg_war_finished = """The wind is howling in the meadows, castles are unusually quiet. Warriors are mending their wounds and repairing armor after a tiresome battle. All the establishments and castle gates are closed for the next couple of minutes. Await the battle report at @chtwrsReports"""
level_up_bytes = b'RIFF\xe6\x14\x00\x00WEBPVP8X\n\x00\x00\x00\x10\x00\x00\x00\x7f\x00\x00\x7f\x00\x00ALPH\xb5\x05\x00'
help_message = """Welcome to control chat.
Here are the following commands:
/arena - Go arena until 5/5
/{valley | swamp} {#,#}+
Quest X number of times
Additionally, adding a "+" after the number appends an additional quest action when saving energy
/{valley | swamp}
Quest until out of energy
/status - current status of your control bot
/stop - stops the control bot from sending commands at cw bot
/ping - confirm the bot is running
/help - help message
Bugs:
Stopping and starting again without a few minutes in between will cause it to run twice, causing itself to error"""
reg_status_update_capture_stamina = """Atk: \d+ 🛡Def: \d+\\n🔥Exp: \d+\/\d+\\n❤️Hp: \d+\/\d+\\n🔋Stamina: (\d+)\/\d+"""
def reset_status():
global bot_active, current_activity, activity_counter, start_event, arena_test_closed, use_found_energy, exhaust_energy, exhaust_activity_reply_quests, results
bot_active = False
current_activity = None
activity_counter = None
start_event = None
arena_test_closed = None
use_found_energy = False
exhaust_energy = False
exhaust_activity_reply_quests = False
results = 0
def print_unhandled_error():
print("{} {} {} {}".format(datetime.now(), bot_active, current_activity, activity_counter))
def refresh_quest_return_data():
global quest_return_json
json_text = urllib.request.urlopen(quest_return_url).read().decode('utf-8')
quest_return_json = json.loads(json_text)
@client.on(events.NewMessage(chats=ChatWars_Channel, incoming=True))
async def one(event):
global bot_active, current_activity, activity_counter, start_event, arena_test_closed, use_found_energy, exhaust_activity_reply_quests, results
if bot_active:
# Valley
if current_activity == "Valley":
# inside quest command
if re.search(reg_quests, event.raw_text):
print_unhandled_error()
if activity_counter > 0:
print("{} sending valley command".format(datetime.now()))
await event.click(text="⛰️Valley")
else:
print("ERROR: Unhandled exception")
await start_event.reply("ERROR: Unhandled exception")
await start_event.reply(
"Bot active: {}\nCurrent activity: {}\nActivity counter: {}".format(bot_active,
current_activity,
activity_counter))
await client.send_message(ChatWars_Channel, "⬅Back")
reset_status()
# # # Valley Handlers # # #
# Return from Valley
responses = quest_return_json["Valley"]["Successful"] + quest_return_json["Valley"]["Unsuccessful"]
for x in responses:
if x in event.raw_text:
results += 1
exhaust_activity_reply_quests = False
activity_counter -= 1
print_unhandled_error()
if activity_counter > 0:
sleep_time = random.randint(5, 300)
print("{} sleeping {}".format(datetime.now(), sleep_time))
await asyncio.sleep(sleep_time)
if bot_active and current_activity == "Valley":
print("{} Sending quests command".format(datetime.now()))
if random.randint(1, 3) == 1:
await client.send_message(ChatWars_Channel, "🏅Me")
await asyncio.sleep(random.randint(3, 5))
await client.send_message(ChatWars_Channel, "🗺Quests")
elif activity_counter == 0:
if exhaust_energy:
exhaust_activity_reply_quests = True
await client.send_message(ChatWars_Channel, "🏅Me")
else:
print("{} valley done".format(datetime.now()))
await start_event.reply("Done\nQuests Completed: {}".format(results))
reset_status()
# not enough stamina
if re.search(reg_error_not_enough_stamina, event.raw_text):
await start_event.reply("ERROR: Not enough Stamina\nQuests Completed: {}".format(results))
reset_status()
# if we saved / found an energy while questing
if use_found_energy and re.search(r"\+1🔋", event.raw_text):
activity_counter += 1
# if warehouse is full
if re.search("your warehouse is full and you lost your loot", event.raw_text):
await start_event.reply("WARNING: Warehouse full")
# Swamp
elif current_activity == "Swamp":
# inside quest command
if re.search(reg_quests, event.raw_text):
print_unhandled_error()
if activity_counter > 0:
print("{} sending swamp command".format(datetime.now()))
await event.click(text="🍄Swamp")
else:
print("ERROR: Unhandled exception")
await start_event.reply("ERROR: Unhandled exception")
await start_event.reply(
"Bot active: {}\nCurrent activity: {}\nActivity counter: {}".format(bot_active,
current_activity,
activity_counter))
await client.send_message(ChatWars_Channel, "⬅Back")
reset_status()
# # # Swamp Handlers # # #
# Return from Swamp
responses = quest_return_json["Swamp"]["Successful"] + quest_return_json["Swamp"]["Unsuccessful"]
for x in responses:
if x in event.raw_text:
results += 1
exhaust_activity_reply_quests = False
activity_counter -= 1
print_unhandled_error()
if activity_counter > 0:
sleep_time = random.randint(5, 300)
print("{} sleeping {}".format(datetime.now(), sleep_time))
await asyncio.sleep(sleep_time)
if bot_active and current_activity == "Swamp":
print("{} Sending quests command".format(datetime.now()))
if random.randint(1, 3) == 1:
await client.send_message(ChatWars_Channel, "🏅Me")
await asyncio.sleep(random.randint(3, 5))
await client.send_message(ChatWars_Channel, "🗺Quests")
elif activity_counter == 0:
if exhaust_energy:
exhaust_activity_reply_quests = True
await client.send_message(ChatWars_Channel, "🏅Me")
else:
print("{} swamp done".format(datetime.now()))
await start_event.reply("Done\nQuests Completed: {}".format(results))
reset_status()
# not enough stamina
if re.search(reg_error_not_enough_stamina, event.raw_text):
await start_event.reply("ERROR: Not enough Stamina\nQuests Completed: {}".format(results))
reset_status()
# if we saved / found an energy while questing
if use_found_energy and re.search(r"\+1🔋", event.raw_text):
activity_counter += 1
# if warehouse is full
if re.search("your warehouse is full and you lost your loot", event.raw_text):
await start_event.reply("WARNING: Warehouse full")
# Arena
elif current_activity == "Arena":
# inside quest command
if re.search(reg_quests, event.raw_text):
# if arena is open
if re.search(reg_quests_arena_open, event.raw_text):
# if unknown count remaining
if activity_counter is None:
# Let's find out how many
await event.click(text="📯Arena")
elif activity_counter >= 0:
# We have some more arena to do
await event.click(text="📯Arena")
else:
print("ERROR: Unhandled exception")
await start_event.reply("ERROR: Unhandled exception")
await start_event.reply(
"Bot active: {}\nCurrent activity: {}\nActivity counter: {}".format(bot_active,
current_activity,
activity_counter))
await client.send_message(ChatWars_Channel, "⬅Back")
reset_status()
# if arena is closed:
if re.search(reg_quests_arena_locked, event.raw_text):
# arena is closed for one of three reasons:
# 1. war less < 2 hours
# 1.1. arena ia already completed
# 2. arena closed while we were working on it
# else: unhandled
# if unknown count remaining
if activity_counter is None:
# Either war in < 2 hours OR arena is already completed
arena_test_closed = True
# send arena button to find out
await event.click(text="📯Arena")
elif activity_counter > 0:
await client.send_message(ChatWars_Channel, "⬅Back")
await client.send_message(ChatWars_Channel, "/g_def")
await start_event.reply(
"ERROR: Arena closed before finishing. Completed: {}/5".format(activity_counter))
reset_status()
else:
print("ERROR: Unhandled exception")
await start_event.reply("ERROR: Unhandled exception")
await start_event.reply(
"Bot active: {}\nCurrent activity: {}\nActivity counter: {}".format(bot_active,
current_activity,
activity_counter))
await client.send_message(ChatWars_Channel, "⬅Back")
reset_status()
# inside arena command
if re.search(reg_arena_capture_remaining_fights, event.raw_text):
activity_counter = int(re.findall(reg_arena_capture_remaining_fights, event.raw_text, re.MULTILINE)[0])
# If arena is closed & unknown counter: we're finding out why
if arena_test_closed:
if activity_counter < 5:
await start_event.reply("ERROR: Arena is closed due to upcoming war.")
await client.send_message(ChatWars_Channel, "⬅Back")
reset_status()
elif activity_counter == 5:
await start_event.reply("ERROR: Arena already completed.")
await client.send_message(ChatWars_Channel, "⬅Back")
reset_status()
else:
# if we have more to do
if activity_counter < 5:
await client.send_message(ChatWars_Channel, "▶️Fast fight")
# error handling
elif activity_counter == 5:
print("ERROR: Unhandled exception")
await start_event.reply("ERROR: Unhandled exception")
await start_event.reply(
"Bot active: {}\nCurrent activity: {}\nActivity counter: {}".format(bot_active,
current_activity,
activity_counter))
await client.send_message(ChatWars_Channel, "⬅Back")
reset_status()
# arena finished
if re.search(reg_arena_response, event.raw_text):
activity_counter += 1
if activity_counter == 5:
await client.send_message(ChatWars_Channel, "/g_def")
await start_event.reply("Done")
reset_status()
elif activity_counter < 5:
await asyncio.sleep(random.randint(3, 15))
await client.send_message(ChatWars_Channel, "🗺Quests")
# # # Arena Handlers # # #
# Opponent not found
if event.raw_text == "You didn’t find an opponent. Return later.":
await client.send_message(ChatWars_Channel, "🗺Quests")
# Not enough money
if event.raw_text == "Not enough gold to pay the entrance fee.":
await start_event.reply("ERROR: Insufficient funds")
reset_status()
# war in < 2 hours
if re.search(reg_arena_too_dark, event.raw_text):
await start_event.reply("ERROR: War in < 2 hours")
reset_status()
# # # Global Handlers # # #
# Too busy response
if re.search(reg_error_too_busy, event.raw_text):
await start_event.reply("ERROR: Too busy\nQuests Completed: {}".format(results))
reset_status()
# Battle is < 5 minutes away
if event.raw_text == "Battle is coming. You have no time for games.":
await start_event.reply("ERROR: War starting soon\nQuests Completed: {}".format(results))
reset_status()
# War is ongoing
if event.raw_text == reg_war_finished:
await start_event.reply("ERROR: War is ongoing\nQuests Completed: {}".format(results))
reset_status()
# Not enough HP
if event.raw_text == "You should heal up a bit first.":
await asyncio.sleep(600)
await client.send_message(ChatWars_Channel, "🗺Quests")
# Level up
# if hasattr(event.message.media.document.thumb, "bytes") and level_up_bytes in event.message.media.document.thumb.bytes:
# await start_event.reply("Level up!")
# Update stamina
if exhaust_energy and re.search(reg_status_update_capture_stamina, event.raw_text):
activity_counter = int(re.findall(reg_status_update_capture_stamina, event.raw_text)[0])
if activity_counter > 0 and exhaust_activity_reply_quests:
await client.send_message(ChatWars_Channel, "🗺Quests")
elif activity_counter == 0:
await start_event.reply("Done\nQuests Completed: {}".format(results))
await client.send_message(ChatWars_Channel, "/g_def")
reset_status()
@client.on(events.NewMessage(chats=Control_Channel, outgoing=True, forwards=False))
async def one(event):
global bot_active, current_activity, activity_counter, start_event, arena_test_closed, use_found_energy, exhaust_energy, exhaust_activity_reply_quests
# valley given number of times
if re.search(r"^/[v|V]alley (\d{1,2})\+?$", event.raw_text):
user_input = int(re.findall(r"^/[v|V]alley (\d{1,2})\+?$", event.raw_text)[0])
if user_input > 0:
if bot_active:
await event.reply("ERROR: Bot already active")
elif not bot_active:
reset_status()
refresh_quest_return_data()
bot_active = True
current_activity = "Valley"
activity_counter = user_input
start_event = event
if re.search(r"^/[v|V]alley (\d{1,2})\+$", event.raw_text):
use_found_energy = True
await start_event.reply("Starting")
await client.send_message(ChatWars_Channel, "🗺Quests")
# valley until exhausted
if re.search(r"^/[v|V]alley$", event.raw_text):
if bot_active:
await event.reply("ERROR: Bot already active")
elif not bot_active:
reset_status()
refresh_quest_return_data()
bot_active = True
current_activity = "Valley"
# activity_counter = 1
exhaust_energy = True
exhaust_activity_reply_quests = True
start_event = event
use_found_energy = True
await start_event.reply("Starting")
await client.send_message(ChatWars_Channel, "🏅Me")
# swamp given number of times
elif re.search(r"^/[s|S]wamp (\d{1,2})\+?$", event.raw_text):
user_input = int(re.findall(r"^/[s|S]wamp (\d{1,2})\+?$", event.raw_text)[0])
if user_input > 0:
if bot_active:
await event.reply("ERROR: Bot already active")
elif not bot_active:
reset_status()
refresh_quest_return_data()
bot_active = True
current_activity = "Swamp"
activity_counter = user_input
start_event = event
if re.search(r"^/[s|S]wamp (\d{1,2})\+$", event.raw_text):
use_found_energy = True
await start_event.reply("Starting")
await client.send_message(ChatWars_Channel, "🗺Quests")
# swamp until exhausted
if re.search(r"^/[s|S]wamp$", event.raw_text):
if bot_active:
await event.reply("ERROR: Bot already active")
elif not bot_active:
reset_status()
refresh_quest_return_data()
bot_active = True
current_activity = "Swamp"
# activity_counter = 1
exhaust_energy = True
exhaust_activity_reply_quests = True
start_event = event
use_found_energy = True
await start_event.reply("Starting")
await client.send_message(ChatWars_Channel, "🏅Me")
# Arena until exhausted
elif re.search("^/[a|A]rena$", event.raw_text):
if bot_active:
await event.reply("ERROR: Bot already active")
elif not bot_active:
reset_status()
refresh_quest_return_data()
bot_active = True
current_activity = "Arena"
start_event = event
await start_event.reply("Starting")
await client.send_message(ChatWars_Channel, "🗺Quests")
elif re.search("^/([s|S]top)|^/[r|R]estart$", event.raw_text):
await event.reply("Done\nQuests Completed: {}".format(results))
reset_status()
elif re.search("^/[p|P]ing$", event.raw_text):
await event.reply("Pong")
elif re.search("^/[s|S]tatus$", event.raw_text):
await event.reply(
"Bot active: {}\nCurrent activity: {}\nActivity counter: {}".format(bot_active, current_activity,
activity_counter))
elif re.search("^/[d|D]ebug$", event.raw_text):
await event.reply(
"Bot active: {}\nCurrent activity: {}\nActivity counter: {}\nstart_event: {}\narena_test_closed: {}\nuse_found_energy: {}\nexhaust_energy: {}\nexhaust_activity_reply_quests: {}".format(
bot_active, current_activity, activity_counter, start_event, arena_test_closed, use_found_energy,
exhaust_energy, exhaust_activity_reply_quests))
elif re.search("^/[h|H]elp$", event.raw_text):
await event.reply(help_message)
# @client.on(events.NewMessage(incoming=True))
# async def two(event):
# print(event.stringify())
# run_forever
print("started")
refresh_quest_return_data()
asyncio.get_event_loop().run_forever()