This repository has been archived by the owner on Jun 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
bot.py
290 lines (253 loc) · 11.5 KB
/
bot.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
# AutoWaifuClaimer
# Copyright (C) 2020 RandomBananazz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from asyncio import TimeoutError
import discord
import sys
import re
from concurrent.futures import ThreadPoolExecutor
import threading
import logging
import datetime
import aiohttp
import config
from browsers import Browser
from timers import Timer
# noinspection PyArgumentList
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO,
handlers=[
logging.FileHandler(config.LOG_FILE, 'a', 'utf-8'),
logging.StreamHandler(sys.stdout)
])
# Initialize Selenium browser integration in separate module
browser = Browser()
# Declare global timer module
timer: Timer
# Main thread and Discord bot integration here
client = discord.Client()
main_user: discord.User
dm_channel: discord.DMChannel
roll_channel: discord.TextChannel
mudae: discord.Member
ready = False
# To be parsed by $tu and used for the auto roller
timing_info = {
'claim_reset': None,
'claim_available': None,
'rolls_reset': None,
'kakera_available': None,
'kakera_reset': None,
'daily_reset': None
}
async def close_bot():
await client.close()
client.loop.stop()
client.loop.close()
sys.exit()
@client.event
async def on_ready():
# Ensure the browser is ready before proceeding (blocking call)
try:
browser_login.result()
except TimeoutError or ValueError:
await close_bot()
def parse_tu(message):
global timing_info
if message.channel != roll_channel or message.author != mudae: return
match = re.search(r"""^.*?(\w+).*? # Group 1: Username
(can't|can).*? # Group 2: Claim available
(\d+(?:h\ \d+)?)(?=\*\*\ min).*? # Group 3: Claim reset
(\d+(?:h\ \d+)?)(?=\*\*\ min).*? # Group 4: Rolls reset
(?<=\$daily).*?(available|\d+h\ \d+).*? # Group 5: $daily reset
(can't|can).*?(?=react).*? # Group 6: Kakera available
(?:(\d+(?:h\ \d+)?)(?=\*\*\ min)|(now)).*? # Group 7: Kakera reset
(?<=\$dk).*?(ready|\d+h\ \d+) # Group 8: $dk reset
.*$ # End of string
""", message.content, re.DOTALL | re.VERBOSE)
if not match: return
if match.group(1) != main_user.name: return
# Convert __h __ to minutes
times = []
for x in [match.group(3), match.group(4), match.group(5), match.group(7)]:
# Specifically, group 7 may be None if kakera is ready
if x is None:
x = 0
elif 'h ' in x:
x = x.split('h ')
x = int(x[0]) * 60 + int(x[1])
elif x == 'ready' or x == 'now':
x = 0
else:
x = int(x)
times.append(x)
kakera_available = match.group(6) == 'can'
claim_available = match.group(2) == 'can'
timing_info = {
'claim_reset': datetime.datetime.now() + datetime.timedelta(minutes=times[0]),
'claim_available': claim_available,
'rolls_reset': datetime.datetime.now() + datetime.timedelta(minutes=times[1]),
'kakera_available': kakera_available,
'kakera_reset': datetime.datetime.now() + datetime.timedelta(minutes=times[3]),
'daily_reset': datetime.datetime.now() + datetime.timedelta(minutes=times[2]),
}
return True
global main_user, mudae, dm_channel, roll_channel, timer, timing_info, ready
logging.info(f'Bot connected as {client.user.name} with ID {client.user.id}')
main_user = await client.fetch_user(config.USER_ID)
dm_channel = await main_user.create_dm()
roll_channel = await client.fetch_channel(config.CHANNEL_ID)
mudae = await client.fetch_user(config.MUDAE_ID)
# Parse timers by sending $tu command
# Only do so once by checking ready property
if not ready:
logging.info('Attempting to parse $tu command')
pool.submit(Browser.send_text, browser, f'{config.COMMAND_PREFIX}tu')
try:
await client.wait_for('message', check=parse_tu, timeout=3)
except TimeoutError:
logging.critical('Could not parse $tu command, quitting (try again)')
browser.close()
await close_bot()
else:
logging.info('$tu command parsed')
logging.info('Creating new Timer based on parsed information')
timer = Timer(browser, timing_info["claim_reset"], timing_info["rolls_reset"], timing_info["daily_reset"],
timing_info['claim_available'], timing_info["kakera_reset"], timing_info["kakera_available"])
if config.DAILY_DURATION > 0:
threading.Thread(name='daily', target=timer.wait_for_daily).start()
if config.ROLL_DURATION > 0:
threading.Thread(name='roll', target=timer.wait_for_roll).start()
threading.Thread(name='claim', target=timer.wait_for_claim).start()
threading.Thread(name='kakera', target=timer.wait_for_kakera).start()
# For some reason, browser Discord crashes sometime at this point
# Refresh the page to fix
browser.refresh() # Blocking call
logging.info("Listener is ready")
ready = True
@client.event
async def on_message(message):
def parse_embed():
# Regex based parsing adapted from the EzMudae module by Znunu
# https://github.com/Znunu/EzMudae
desc = embed.description
name = embed.author.name
series = None
owner = None
key = False
# Get series and key value if present
match = re.search(r'^(.*?[^<]*)(?:<:(\w+key))?', desc, re.DOTALL)
if match:
series = match.group(1).replace('\n', ' ').strip()
if len(match.groups()) == 3:
key = match.group(2)
# Check if it was a roll
# Look for any
match = re.search(r'(?<=\*)(\d+)', desc, re.DOTALL)
if match: return
# Check if valid parse
if not series: return
# Get owner if present
if not embed.footer.text:
is_claimed = False
else:
match = re.search(r'(?<=Belongs to )\w+', embed.footer.text, re.DOTALL)
if match:
is_claimed = True
owner = match.group(0)
else:
is_claimed = False
# Log in roll list and console/logfile
with open('./data/rolled.txt', 'a') as f:
f.write(f'{datetime.datetime.now()} {name} - {series}\n')
logging.info(f'Parsed roll: {name} - {series} - Claimed: {is_claimed}')
return {'name': name,
'series': series,
'is_claimed': is_claimed,
'owner': owner,
'key': key}
def reaction_check(payload):
# Return if reaction message or author incorrect
if payload.message_id != message.id: return
if payload.user_id != mudae.id: return
# Open thread to click emoji
emoji = payload.emoji
pool.submit(browser.react_emoji, emoji.name, message.id)
return True
## BEGIN ON_MESSAGE BELOW ##
global main_user, mudae, dm_channel, roll_channel, ready
if not ready: return
# Only parse messages from the bot in the right channel that contain a valid embed
if message.channel != roll_channel or message.author != mudae or not len(message.embeds) == 1 or \
message.embeds[0].image.url == message.embeds[0].Empty: return
embed = message.embeds[0]
if not (waifu_result := parse_embed()): return # Return if parsing failed
# If unclaimed waifu was on likelist
if waifu_result['name'] in like_array and not waifu_result['is_claimed']:
if not timer.get_claim_availability(): # No claim is available
logging.warning(f'Character {waifu_result["name"]} was on the likelist but no claim was available!')
await dm_channel.send(content=f"Character {waifu_result['name']} was on the likelist"
f"but no claim was available!", embed=embed)
return
logging.info(f'Character {waifu_result["name"]} in likelist, attempting marry')
# New Mudae bot does not automatically add emojis, just react.
pool.submit(browser.react_emoji, "❤", message.id)
"""
try:
await client.wait_for('raw_reaction_add', check=reaction_check, timeout=3)
except TimeoutError:
logging.critical('Marry failed, could not detect bot reaction')
return
else:
await dm_channel.send(content=f"Marry attempted for {waifu_result['name']}", embed=embed)
timer.set_claim_availability(False)
"""
# If key was rolled
if waifu_result['owner'] == main_user.name and waifu_result['key']:
await dm_channel.send(content=f"{waifu_result['key']} rolled for {waifu_result['name']}", embed=embed)
# If kakera loot available
if waifu_result['is_claimed']:
if not timer.get_kakera_availablilty():
logging.warning(f'Character {waifu_result["name"]} has kakera loot but the loot was not available!')
await dm_channel.send(content=f"Character {waifu_result['name']} had kakera loot"
f" but no loot was available!", embed=embed)
return
logging.info('Attempting to loot kakera')
try:
await client.wait_for('raw_reaction_add', check=reaction_check, timeout=3)
except TimeoutError:
logging.critical('Kakera loot failed, could not detect bot reaction')
return
else:
await dm_channel.send(content=f"Kakera loot attempted for {waifu_result['name']}", embed=embed)
timer.set_kakera_availability(False)
if __name__ == '__main__':
with open('./data/likelist.txt', 'r') as f:
logging.info('Parsing likelist')
like_array = [x.strip() for x in [x for x in f.readlines() if not x.startswith('\n')] if not x.startswith('#')]
pool = ThreadPoolExecutor()
try:
logging.info('Starting browser thread')
browser_login = pool.submit(Browser.browser_login, browser)
client.loop.run_until_complete(client.start(config.BOT_TOKEN))
except KeyboardInterrupt:
logging.critical("Keyboard interrupt, quitting")
client.loop.run_until_complete(client.logout())
except discord.LoginFailure or aiohttp.ClientConnectorError:
logging.critical(f"Improper token has been passed or connection to Discord failed, quitting")
finally:
browser.close()
client.loop.stop()
client.loop.close()