-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
executable file
·489 lines (367 loc) · 14.7 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
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
import json
import os
import platform
import random
import sys
from discord.ext.commands.core import command
import requests
from lxml import html
from ast import literal_eval
# uWu quote libraries
from owoify import owoify
import discord
from discord.ext import commands, tasks
from discord.ext.commands import Bot
if not os.path.isfile("config.json"):
sys.exit("'config.json' not found! Please add it and try again.")
else:
with open("config.json") as file:
config = json.load(file)
if not os.path.isfile("sources.json"):
sys.exit("'sources.json' not found! Please add it and try again.")
else:
with open("sources.json") as file:
sources = json.load(file)
if not os.path.isfile("mangas.json"):
sys.exit("'mangas.json' not found! Please add it and try again.")
else:
with open("mangas.json") as file:
mangas = json.load(file)
if not os.path.isfile("users.json"):
sys.exit("'users.json' not found! Please add it and try again.")
else:
with open("users.json") as file:
users = json.load(file)
if not os.path.isfile("quotes.json"):
sys.exit("'quotes.json' not found! Please add it and try again.")
else:
with open("quotes.json") as file:
quotes = json.load(file)
intents = discord.Intents.default()
bot = Bot(command_prefix=config["bot_prefix"], intents=intents)
# The code in this even is executed when the bot is ready
@bot.event
async def on_ready():
print(f"Logged in as {bot.user.name}")
print(f"Discord.py API version: {discord.__version__}")
print(f"Python version: {platform.python_version()}")
print(f"Running on: {platform.system()} {platform.release()} ({os.name})")
print("-------------------")
status_task.start()
update.start()
# Setup the game status task of the bot
@tasks.loop(minutes=1.0)
async def status_task():
statuses = ["Degeneracy!"]
await bot.change_presence(activity=discord.Game(random.choice(statuses)))
def write_mangas(new_data, old_data, filename):
with open(filename,'r+') as file:
old_data['mangas'].append(literal_eval(new_data))
# Sets file's current position at offset.
file.seek(0)
# convert back to json.
json.dump(old_data, file, indent = 4)
def write_sources(new_data, old_data, filename):
with open(filename,'r+') as file:
old_data['sources'].append(literal_eval(new_data))
# Sets file's current position at offset.
file.seek(0)
# convert back to json.
json.dump(old_data, file, indent = 4)
def write_users(new_data, old_data, filename):
with open(filename,'r+') as file:
old_data['users'].append(literal_eval(new_data))
# Sets file's current position at offset.
file.seek(0)
# convert back to json.
json.dump(old_data, file, indent = 4)
def page_request(url):
request = requests.get(url).text
tree = html.fromstring(request)
return tree
def xpath_scrape(tree, pattern, href):
if href == True:
return tree.xpath("/"+pattern+"/@href") or tree.xpath("/"+pattern+"[text()]/@href")
else:
return tree.xpath("/"+pattern+"/text()")
# Get manga data
def scan_source(args):
for i in sources['sources']:
if i['link'] in args:
print(f"processing... {args}")
tree = page_request(args)
name = xpath_scrape(tree, i['name_xpath'], False)[0]
link = args
last_updated = xpath_scrape(tree, i['last_updated_xpath'], False)[0]
current_chapter = xpath_scrape(tree, i['chapter_xpath'], False)[0]
chapter_link = i['link']+xpath_scrape(tree, i['chapter_link'], True)[0]
return str(name), str(link), str(last_updated), str(current_chapter), str(chapter_link)
return "", "", "", "", ""
def add_manga(mangas, name, link, last_updated, current_chapter, chapter_link):
manga = {
"name": name,
"link": link,
"last_updated": last_updated,
"current_chapter": current_chapter,
"chapter_link": chapter_link
}
manga = json.dumps(manga)
write_mangas(manga, mangas, 'mangas.json')
with open("mangas.json") as file:
mangas = json.load(file)
return
def del_manga(users, user, list_mangas, name):
flags=0
for j in users['users']:
if user == j['name']:
for k in j['mangas']:
if name == k:
flags += 1
if flags == 1:
for j in users['users']:
if user == j['name']:
count = 0
for l in j['mangas']:
count += 1
if l == name:
break
j['mangas'].pop(count-1)
users = json.dumps(users)
with open('users.json','w') as file:
json.dump(literal_eval(users), file, indent = 4)
with open("users.json") as file:
users = json.load(file)
return True
else:
return False
def add_source(sources, args):
source = {
"name": args[2],
"link": args[3],
"name_xpath": args[4],
"last_updated_xpath": args[5],
"chapter_xpath": args[6],
"chapter_link": args[7]
}
source = json.dumps(source)
write_sources(source, sources, 'sources.json')
with open("sources.json") as file:
sources = json.load(file)
return
def user_add(users, user, name):
flags=0
for j in users['users']:
if user == j['name']:
for k in j['mangas']:
if name == k:
flags += 1
if flags == 0:
for j in users['users']:
if user == j['name']:
j['mangas'].append(name)
else:
return "( ͡U ω ͡U ) Damn king, great choice...\nBut "+name+", already exists in your list!!!!"
users = json.dumps(users)
with open('users.json','w') as file:
json.dump(literal_eval(users), file, indent = 4)
with open("users.json") as file:
users = json.load(file)
return "\n"+name+", has been added to your list!!!!"
def create_user(users, user, id):
print("creating user: "+user)
user = {
"id": id,
"name": user,
"mangas": []
}
user = json.dumps(user)
write_users(user, users, 'users.json')
with open("users.json") as file:
users = json.load(file)
return
def quote():
length = len(quotes)
key = random.randrange(length)
msg = '"' + quotes[key]['Quote']+ '"\n - ' + quotes[key]['Author']
return msg
def scan_mangas(mangas):
updated_list = []
for manga in mangas['mangas']:
name, link, last_updated, current_chapter, chapter_link = scan_source(manga['link'])
if last_updated != manga['last_updated']:
updated_list.append(manga['name'])
manga['last_updated'] = last_updated
manga['current_chapter'] = current_chapter
manga['chapter_link'] = chapter_link
mangas = json.dumps(mangas)
with open('mangas.json','w') as file:
json.dump(literal_eval(mangas), file, indent = 4)
with open("mangas.json") as file:
mangas = json.load(file)
return updated_list
def notify(user, updated_list):
msg = ""
for update in updated_list:
if(len(list(filter (lambda x : x == update, user['mangas']))) > 0):
for z in mangas['mangas']:
if update == z['name']:
msg += z['name']+' | '+z['current_chapter']+'\n'+z['chapter_link']+'\n'
return msg
@tasks.loop(hours=6)
async def update():
print("update() task started")
updated_list = scan_mangas(mangas)
for user in users['users']:
member = int(user['id'])
member = await bot.fetch_user(member)
tmp = notify(user, updated_list)
if tmp:
msg = "**New chapters inbound!**\n"+tmp
await member.send(msg)
return
def help_msg():
text = "**The following is a list of commands:** \n \
`@MangaBot list` \n \
`@MangaBot update` manually force an update of catalogue\n \
`@MangaBot add [LINK/NAME]` \n \
`@MangaBot del [NAME]` \n \
`@MangaBot add [LINK],[LINK],[LINK]....` Mixed types works.\n \
`@MangaBot del [NAME],[NAME],[NAME]....` \n \
`@MangaBot source [NAME] [LINK] [xpath_to_name] [xpath_to_date_updated] [xpath_to_chpt_num] [xpath_to_chapter/episode]` \n \
**Please do not use the source command unless you know what you are doing.**"
return text
# The code in this event is executed every time someone sends a message, with or without the prefix
@bot.event
async def on_message(message):
if bot.user.mentioned_in(message):
user = str(message.author)
message.content = ' '.join(message.content.split())
lists_users = []
for i in users['users']:
lists_users.append(i['name'])
list_mangas = []
for i in mangas['mangas']:
list_mangas.append(i['name'])
if user not in lists_users:
create_user(users, user, message.author.id)
await message.author.send("Hurray! You were not important and did not exist before... You do now!")
if "help" in message.content:
await message.author.send(help_msg())
return
elif "update" in message.content:
updated_list = scan_mangas(mangas)
for user in users['users']:
member = int(user['id'])
member = await bot.fetch_user(member)
tmp = notify(user, updated_list)
if tmp:
print(f"notifying {user} of new chapters")
msg = "**New chapters inbound!**\n```"+tmp+"```"
await member.send(msg)
return
elif "add" in message.content:
command = message.content.split(">")[1]
arg = command.split(" ")[2::]
arg = ' '.join(arg).split(",")
for args in arg:
if args.startswith("http"):
name, link, last_updated, current_chapter, chapter_link = scan_source(args)
if name == "":
msg = "(ㅅꈍ ˘ ꈍ) nishi-nishi, you made a fucksie-wucksie...\nSee the list command for current **sources**."
await message.author.send(msg)
continue
if name not in list_mangas:
add_manga(mangas, name, link, last_updated, current_chapter, chapter_link)
await message.author.send("(ᵘʷᵘ) "+name+", has been added to the archives!!!!")
else:
if args in list_mangas:
name = args
else:
msg = "(ㅅꈍ ˘ ꈍ) nishi-nishi, you made a fucksie-wucksie...\nYou probs used the wrong names or added spaces before commas...\nSee the list command for current **mangas**."
await message.author.send(msg)
continue
for i in mangas['mangas']:
if name.lower() == i['name'].lower():
flag = 0
for i in users['users']:
if user == i['name']:
flag += 1
msg = user_add(users, user, name)
await message.channel.send(msg)
continue
if flag == 0:
create_user(users, user, message.author.id)
msg = user_add(users, user, name)
await message.channel.send(msg)
continue
return
elif "del" in message.content:
command = message.content.split(">")[1]
arg = command.split(" ")[2::]
arg = ' '.join(arg).split(",")
for args in arg:
if del_manga(users, user, list_mangas, args):
await message.author.send("(ᵘʷᵘ) "+args+", has been removed from your list!!!!")
else:
await message.author.send("ಠ_ಠ Yeah there was some kinda error. Debugging is expensive.\nYou probs used the wrong names or added spaces before commas...")
return
elif "list" in message.content:
msg = ""
list = []
msg += "These are the current sources:\n"
for i in sources['sources']:
list.append(i['link'])
msg += "```" + str(list) + "```"
await message.author.send(msg)
list = []
msg = "\nThis is the current library:\n"
msg += "```"+ str(list_mangas) + "```"
await message.author.send(msg)
list = []
msg = "\nThis is your watchlist:\n"
await message.author.send(msg)
for i in users['users']:
if i['name'] == user:
list = i['mangas']
links = []
dates = []
for j in list:
for k in mangas['mangas']:
if str(j) == k['name']:
links.append(k['chapter_link'])
dates.append(k['last_updated'].split('-')[0])
for j in range(0,len(list)):
msg = "```["+list[j]+"]\n"+links[j]+"\n"+dates[j]+" ```\n"
await message.author.send(msg)
if list == "":
await message.author.send("```Damn boi, you need some sauce ༼ つ ◕_◕ ༽つ```")
break
return
elif "source" in message.content:
args = message.content.split(">")[1]
args = args.split(" ")
name = args[2]
if args[3][-1] == "/":
link = args[3][:-1]
args[3] = args[3][:-1]
else:
link = args[3]
for i in sources['sources']:
if link == i['link']:
await message.author.send("ಠ_ಠ This source, "+name+", already exists!!!!")
return
add_source(sources, args)
await message.author.send("(┐◎_◎┌) This source, "+name+", has been added to the list!!!!")
return
elif "uwu" in message.content:
msg = quote()
msg = owoify(msg)
await message.channel.send(msg)
return
else:
await message.author.send(help_msg())
return
return
if __name__ == "__main__":
# Run the bot with the token
bot.run(config["token"])