This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
310 lines (294 loc) · 19.4 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
from discord import Forbidden, NotFound, HTTPException
from discord.ext import tasks
import datetime
import discord
import asyncio
import aiohttp
import re
import os
import io
def embed(title="", description="", footer="AnimateMyEmojis", icon=discord.Embed.Empty, color=(32, 34, 37), color_obj=None):
embed_to_send = discord.Embed(title=title, description=description, timestamp=datetime.datetime.utcnow(),
color=discord.Color.from_rgb(color[0], color[1], color[2]) if not color_obj else color_obj)
embed_to_send.set_footer(text=footer, icon_url=icon)
return embed_to_send
class AnimateMyEmojis(discord.Client):
async def ensure_webhook(self, channel):
if channel.id in webhook_cache:
return
chan_webhooks = await channel.webhooks()
for chan_webhook in chan_webhooks:
if chan_webhook.user.id == self.user.id:
webhook_cache[channel.id] = chan_webhook
break
if channel.id in webhook_cache:
return
new_webhook = await channel.create_webhook(name="AnimateMyEmojis Webhook",
reason="Webhook for replacing animated emojis, please do not delete!")
webhook_cache[channel.id] = new_webhook
def count_emojis(self, guild, animated):
count = 0
for emoji in guild.emojis:
if emoji.animated == animated:
count += 1
return count
async def on_ready(self):
print(f"Logged in as {self.user}!")
await self.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="discord open up", start=start_datetime), status=discord.Status.online)
update_presence_loop.start()
async def on_message(self, message):
if not message.guild:
return
global emojis_today
try:
only_ping = message.content == f"<@!{self.user.id}>" or message.content == f"<@{self.user.id}>"
starts_with_ping = message.content.startswith(f"<@!{self.user.id}>") or message.content.startswith(f"<@{self.user.id}>")
if only_ping:
await message.reply(embed=embed(title="😎 AnimateMyEmojis",
description="You can **invite me** to __your__ server with __**[this link](https://discord.com/oauth2/authorize?client_id=812756332905365504&permissions=1610689600&scope=bot)**__!\n"
"__**[Here](https://github.com/Willy-JL/animate-my-emojis)**__ you can find more info *and* my source code!\n"
"\n"
"Made with 💙 by [WillyJL](https://linktr.ee/WillyJL)",
color=(93, 173, 236)))
elif starts_with_ping and not only_ping:
if message.content.startswith (f"<@!{self.user.id}>"):
content = message.content[len(f"<@!{self.user.id}>"):]
if message.content.startswith (f"<@{self.user.id}>"):
content = message.content[len(f"<@{self.user.id}>"):]
literal_regex = "<a?:[A-z 0-9]{2,33}:[0-9]{8,100}>"
link_regex = "https:\/\/cdn\.discordapp\.com\/emojis\/[0-9]{8,100}\.[A-z0-9]*"
if re.search(literal_regex, content) or re.search(link_regex, content):
if not message.author.guild_permissions.manage_emojis:
await message.reply(embed=embed(title="⛔ No Permission!",
description="You need the **manage emojis** permission to do that!",
color=(218, 45, 67)))
return
elif not message.channel.permissions_for(message.guild.me).manage_emojis:
await message.reply(embed=embed(description="⛔ The bot doesn't have the **manage emojis** permission!",
color=(218, 45, 67)))
return
await message.add_reaction("🔄")
result = ""
literal_results = list(dict.fromkeys(re.findall(literal_regex, content)))
link_results = list(dict.fromkeys(re.findall(link_regex, content)))
print(f"Yoinking {len(literal_results) + len(link_results)} emojis for @{message.author} in #{message.channel.name} at [{message.guild.name}]")
interrupted = False
ignored_existing = []
warned_animated_slots = False
warned_static_slots = False
for literal_emoji in literal_results:
if interrupted:
break
emoji_id = int(literal_emoji[literal_emoji.rfind(":") + 1:literal_emoji.rfind(">")])
emoji_name = literal_emoji[literal_emoji.find(":") + 1:literal_emoji.rfind(":")].replace(" ", "")
exists = False
for existing_emoji in message.guild.emojis:
if existing_emoji.id == emoji_id:
exists = True
ignored_existing.append(emoji_name)
break
if not exists:
if literal_emoji[1] == "a":
if self.count_emojis(message.guild, animated=True) >= message.guild.emoji_limit:
if not warned_animated_slots:
await message.reply(embed=embed(description="⛔ There are no more free animated emoji slots!",
color=(218, 45, 67)))
warned_animated_slots = True
continue
file_name = f"{emoji_id}.gif"
else:
if self.count_emojis(message.guild, animated=False) >= message.guild.emoji_limit:
if not warned_static_slots:
await message.reply(embed=embed(description="⛔ There are no more free static emoji slots!",
color=(218, 45, 67)))
warned_static_slots = True
continue
file_name = f"{emoji_id}.png"
async with http_client.get(f"https://cdn.discordapp.com/emojis/{file_name}",
allow_redirects=True) as resp:
img = await resp.read()
try:
new_emoji = await message.guild.create_custom_emoji(name=emoji_name,
image=img)
result += "<"
if new_emoji.animated:
result += "a"
result += f":{new_emoji.name}:{new_emoji.id}> "
emojis_today += 1
except HTTPException as e:
exc_msg = str(e).lower()
if "missing permission" in exc_msg:
await message.reply(embed=embed(description="⛔ The bot doesn't have the **manage emojis** permission!",
color=(218, 45, 67)))
interrupted = True
elif "maximum number of emoji" in exc_msg:
await message.reply(embed=embed(description="⛔ There are no more free emoji slots!",
color=(218, 45, 67)))
elif "large" in exc_msg:
await message.reply(embed=embed(description=f"⛔ Emoji \"**{emoji_name}**\" is larger than 256kb!",
color=(218, 45, 67)))
else:
print(exc_msg)
await message.reply(embed=embed(description=f"⛔ Something went wrong!\n```{exc_msg}```",
color=(218, 45, 67)))
for link_emoji in link_results:
if interrupted:
break
emoji_id = int(link_emoji[34:link_emoji.rfind(".")])
emoji_name = str(emoji_id)
exists = False
for existing_emoji in message.guild.emojis:
if existing_emoji.id == emoji_id:
exists = True
ignored_existing.append(emoji_name)
break
if not exists:
if link_emoji[-4:] == ".gif":
if self.count_emojis(message.guild, animated=True) >= message.guild.emoji_limit:
if not warned_animated_slots:
await message.reply(embed=embed(description="⛔ There are no more free animated emoji slots!",
color=(218, 45, 67)))
warned_animated_slots = True
continue
else:
if self.count_emojis(message.guild, animated=False) >= message.guild.emoji_limit:
if not warned_static_slots:
await message.reply(embed=embed(description="⛔ There are no more free static emoji slots!",
color=(218, 45, 67)))
warned_static_slots = True
continue
async with http_client.get(link_emoji, allow_redirects=True) as resp:
img = await resp.read()
try:
new_emoji = await message.guild.create_custom_emoji(name=emoji_name,
image=img)
result += "<"
if new_emoji.animated:
result += "a"
result += f":{new_emoji.name}:{new_emoji.id}> "
emojis_today += 1
except HTTPException as e:
exc_msg = str(e).lower()
if "missing permission" in exc_msg:
await message.reply(embed=embed(description="⛔ The bot doesn't have the **manage emojis** permission!",
color=(218, 45, 67)))
interrupted = True
elif "maximum number of emoji" in exc_msg:
await message.reply(embed=embed(description="⛔ There are no more free emoji slots!",
color=(218, 45, 67)))
elif "large" in exc_msg:
await message.reply(embed=embed(description=f"⛔ Emoji \"**{emoji_name}**\" is larger than 256kb!",
color=(218, 45, 67)))
else:
print(exc_msg)
await message.reply(embed=embed(description=f"⛔ Something went wrong!\n```{exc_msg}```",
color=(218, 45, 67)))
await message.add_reaction("👌")
if ignored_existing:
await message.reply(embed=embed(title="⚠️ Existing Emojis!",
description=f"The following emojis were ignored because they are from this server:\n`{'`, `'.join(ignored_existing)}`",
color=(255, 204, 77)))
if result:
await message.reply(result)
else:
if isinstance(message.author, discord.Member) and not message.author.premium_since:
msg = message.content
emoji_count = 0
for match in reversed(list(re.finditer(":\w{2,32}:", msg))):
if (msg[match.start()-1] == "<" and match.start() > 0) or (msg[match.start()-2:match.start()] == "<a" and match.start() > 1):
continue
emoji_name = msg[match.start()+1:match.end()-1]
new_emoji = discord.utils.get(message.guild.emojis, name=emoji_name)
if new_emoji:
new_emoji_text = f"<{'a' if new_emoji.animated else ''}:{new_emoji.name}:{new_emoji.id}>"
msg = msg[:match.start()] + new_emoji_text + msg[match.end():]
emoji_count += 1
if emoji_count > 0:
print(f"Converting {emoji_count} emojis for @{message.author} in #{message.channel.name} at [{message.guild.name}]")
await self.ensure_webhook(message.channel)
if message.reference:
if isinstance(message.reference.resolved, discord.Message):
mention = f"\n<@!{message.reference.resolved.author.id}> "
if "\n" in message.reference.resolved.content:
reply = "> " + message.reference.resolved.content[:message.reference.resolved.content.find("\n")]
else:
reply = "> " + message.reference.resolved.content
if len(reply) > (2000 - len(msg) - len(mention)):
reply = reply[:(2000-len(msg))-len(mention)-4] + "..."
msg = reply + mention + msg
files = []
for attachment in message.attachments:
fp = io.BytesIO()
await attachment.save(fp, use_cached=True)
fp.seek(0)
files.append(discord.File(fp, filename=attachment.filename,
spoiler=attachment.is_spoiler()))
webhook_msg = None
try:
webhook_msg = await webhook_cache[message.channel.id].send(username=message.author.display_name,
avatar_url=message.author.avatar_url,
content=msg,
embeds=message.embeds,
files=files,
wait=True)
except NotFound:
del webhook_cache[message.channel.id]
await self.ensure_webhook(message.channel)
webhook_msg = await webhook_cache[message.channel.id].send(username=message.author.display_name,
avatar_url=message.author.avatar_url,
content=msg,
embeds=message.embeds,
files=files,
wait=True)
if webhook_msg:
emojis_today += emoji_count
author_id = message.author.id
try:
await message.delete()
except:
pass
await webhook_msg.add_reaction("🚫")
try:
await client.wait_for('reaction_add',
timeout=300,
check=lambda reaction, user: reaction.message.id == webhook_msg.id and user.id == author_id and str(reaction.emoji) == "🚫")
await webhook_msg.delete()
except asyncio.TimeoutError:
await webhook_msg.clear_reaction("🚫")
except Forbidden:
try:
await message.reply(embed=embed(title="⛔ Error!", description="The bot doesn't have enough permissions to complete the action!", color=(218, 45, 67)))
except Forbidden:
pass
@tasks.loop(seconds=15)
async def update_presence_loop():
global cur_presence
if cur_presence == 0:
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name=f"{len(client.guilds)} servers", start=start_datetime), status=discord.Status.dnd)
cur_presence = 1
elif cur_presence == 1:
count = 0
for guild in client.guilds:
count += guild.member_count
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=f"{count} users", start=start_datetime), status=discord.Status.dnd)
cur_presence = 2
elif cur_presence == 2:
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.playing, name=f"{emojis_today} emoji{'s' if emojis_today != 1 else ''} today", start=start_datetime), status=discord.Status.dnd)
cur_presence = 0
cur_presence = 0
emojis_today = 0
http_client = None
webhook_cache = {}
start_datetime = datetime.datetime.now()
if __name__ == "__main__":
client = AnimateMyEmojis()
async def make_aiohttp_session():
global http_client
http_client = aiohttp.ClientSession()
client.loop.run_until_complete(make_aiohttp_session())
try:
client.loop.run_until_complete(client.run(os.environ["token"]))
except RuntimeError:
pass
finally:
client.loop.close()