-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbot.py
127 lines (113 loc) · 4.93 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
import os
import re
import html
import textwrap
import discord
import kadal
from dateutil.parser import parse
class TachiBoti(discord.Client):
def __init__(self):
super().__init__()
# Note: this uses an OR regex hack.
# Full match will match anything, but group 1 will match
# the proper regex.
self.regex = {
"anime": re.compile(r"`[\s\S]*?`|{(.*?)}"),
"manga": re.compile(r"<.*?https?:\/\/.*?>|<a?:.+?:\d*>|`[\s\S]*?`|<(.*?)>")
}
self.tachi_id = 349436576037732353
self.klient = kadal.Klient(loop=self.loop)
self.anilist_cover_url = "https://img.anili.st/media/"
@staticmethod
def get_title(media):
return (media.title.get("english")
or media.title.get("romaji")
or media.title.get("native"))
async def format_embed(self, name, media, method, *, allow_adult):
try:
media = await method(name, popularity=True, allow_adult=allow_adult)
except kadal.MediaNotFound:
return
desc = "***" + ", ".join(media.genres) + "***\n"
if media.description is not None:
short_desc = textwrap.shorten(media.description, width=256 - len(desc), placeholder="")
desc += f"{short_desc}... [(more)]({media.site_url})"
# dirty half-fix until i figure something better out
replacements = [
(r"</?i/?>", ""),
(r"</?br/?>", "\n")
]
for regex, regex_replace in replacements:
desc = html.unescape(re.sub(regex, regex_replace, desc, flags=re.I | re.M))
footer = re.sub(r".*\.", "", str(media.format))
footer_text = footer.replace("TV", "ANIME").replace("_", " ").title()
if len(footer_text) <= 3:
footer_text = footer_text.upper()
status = re.sub(r".*\.", "", str(media.status))
status_text = status.replace("_", " ").capitalize()
color_hex = media.cover_color or "2F3136"
embed_color = int(color_hex.lstrip('#'), 16)
title = self.get_title(media)
e = discord.Embed(title=title, description=desc, color=embed_color)
e.set_footer(text=f"{footer_text} • {status_text}",
icon_url="https://anilist.co/img/logo_al.png")
e.set_image(url=f"{self.anilist_cover_url}{media.id}")
if any(media.start_date.values()):
e.timestamp = parse(str(media.start_date), fuzzy=True)
e.url = media.site_url
return e
async def search(self, message, regex, media, search_method, *, allow_adult):
m = regex.findall(message.clean_content)
m_clean = list(filter(bool, m))
if m_clean:
async with message.channel.typing():
embed = discord.Embed()
if len(m_clean) > 1:
fmt = ""
for name in m_clean:
try:
media = await search_method(name, popularity=True, allow_adult=allow_adult)
title = self.get_title(media)
fmt += f"[**{title}**]({media.site_url})\n"
except kadal.MediaNotFound:
pass
embed.description = fmt
embed.color = discord.Color(0x2f3136)
else:
embed = await self.format_embed(m_clean[0], media, search_method, allow_adult=allow_adult)
if not embed:
return
await message.channel.send(embed=embed)
async def on_message(self, message):
if message.author.bot: # Ignore other bots messages
return
if message.author == self.user: # Ignore own messages
return
for media, regex in self.regex.items():
method = self.klient.search_anime if media == "anime" else self.klient.search_manga
await self.search(message, regex, media, method, allow_adult=message.channel.is_nsfw())
async def on_ready(self):
print("~-~-~-~-~-~-~-~-~-~-~")
print(f"Bot: {self.user.name}")
print(f"ID: {self.user.id}")
print("~-~-~-~-~-~-~-~-~-~-~")
print("Ready!")
async def on_member_join(self, member):
if member.guild.id != self.tachi_id:
return
try:
await member.send("""
Welcome to Tachiyomi!\n
Before asking anything in <#349436576037732355>, please make sure to check the <#403520500443119619> channel, \
there's a very high chance you won't even have to ask.
Most if not all entries in <#403520500443119619> are up to date, \
and the channel is updated regularly to reflect the status of extensions and the app in general.
""") # noqa
except discord.errors.Forbidden: # Can't DM member, give up.
pass
bot = TachiBoti()
token = os.environ.get('TOKEN')
if token is None:
with open("token") as f:
token = f.readline()
bot.run(token)