|
1
|
|
|
""" |
|
2
|
|
|
Properly welcome your new members into your server! |
|
3
|
|
|
""" |
|
4
|
|
|
from __future__ import annotations |
|
5
|
|
|
|
|
6
|
|
|
import json |
|
7
|
|
|
import logging |
|
8
|
|
|
from asyncio import sleep |
|
9
|
|
|
from os import getenv |
|
10
|
|
|
from typing import TYPE_CHECKING |
|
11
|
|
|
|
|
12
|
|
|
from aiohttp import ClientSession |
|
13
|
|
|
|
|
14
|
|
|
from pincer import Cog, Client, command |
|
15
|
|
|
from pincer.objects import File, Message |
|
16
|
|
|
from pincer.objects.events.guild import GuildMemberAddEvent |
|
17
|
|
|
|
|
18
|
|
|
if TYPE_CHECKING: |
|
19
|
|
|
from pincer.objects import Channel, User |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
class Welcome(Cog): |
|
23
|
|
|
channel: Channel |
|
|
|
|
|
|
24
|
|
|
BASE_API_URL = "https://api.xiler.net" |
|
25
|
|
|
REQUEST_HEADERS = { |
|
26
|
|
|
"Content-Type": "application/json", |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
def __init__(self, client: Client): |
|
30
|
|
|
super().__init__(client) |
|
31
|
|
|
self.__session: ClientSession = ClientSession(self.BASE_API_URL) |
|
32
|
|
|
|
|
33
|
|
|
with open("./cogs/templates/welcome.html") as f: |
|
34
|
|
|
self.template = f.read() |
|
35
|
|
|
|
|
36
|
|
|
def _build_html(self, user: User) -> str: |
|
37
|
|
|
"""Build a HTML template to welcome new users!""" |
|
38
|
|
|
arguments = { |
|
39
|
|
|
"icon": user.get_avatar_url(256), |
|
40
|
|
|
"username": user.username, |
|
41
|
|
|
"discriminator": user.discriminator |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
res = self.template |
|
45
|
|
|
|
|
46
|
|
|
for key, value in arguments.items(): |
|
47
|
|
|
res = res.replace(f"%{key}%", value) |
|
48
|
|
|
|
|
49
|
|
|
return res |
|
50
|
|
|
|
|
51
|
|
|
async def generate_welcome_for_user(self, user: User, ttl=0) -> File: |
|
52
|
|
|
"""Generate the welcome file, this gets sent to discord!""" |
|
53
|
|
|
body = { |
|
54
|
|
|
"html": self._build_html(user), |
|
55
|
|
|
"config": { |
|
56
|
|
|
"format": "png", |
|
57
|
|
|
"width": 700, |
|
58
|
|
|
"height": 258, |
|
59
|
|
|
"transparent": True, |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
async with self.__session.post( |
|
64
|
|
|
"/v1/html-to-image", |
|
65
|
|
|
# "/v1/html-to-image-chrome", # use this endpoint with the variant, |
|
66
|
|
|
# as it requires more modern css support |
|
67
|
|
|
data=json.dumps(body), |
|
68
|
|
|
headers=self.REQUEST_HEADERS |
|
69
|
|
|
) as res: |
|
70
|
|
|
if res.ok: |
|
71
|
|
|
image_bytes = await res.read() |
|
72
|
|
|
return File(image_bytes, "png", f"welcome-{user.id}.png") |
|
73
|
|
|
|
|
74
|
|
|
logging.error(await res.read()) |
|
75
|
|
|
|
|
76
|
|
|
# Allow a maximum of 5 retries |
|
77
|
|
|
if ttl < 5: |
|
78
|
|
|
await sleep(0.5) |
|
79
|
|
|
return await self.generate_welcome_for_user(user, ttl + 1) |
|
80
|
|
|
|
|
81
|
|
|
raise RuntimeError( |
|
82
|
|
|
"TTL Exceeded, not retrying request! (failed for welcomes)" |
|
83
|
|
|
) |
|
84
|
|
|
|
|
85
|
|
|
@Client.event |
|
86
|
|
|
async def on_ready(self): |
|
87
|
|
|
self.channel = await self.client.get_channel(int(getenv("WELCOME_CHANNEL"))) |
|
88
|
|
|
|
|
89
|
|
|
@Client.event |
|
90
|
|
|
async def on_guild_member_add(self, event: GuildMemberAddEvent): |
|
91
|
|
|
banner = await self.generate_welcome_for_user(event) |
|
92
|
|
|
await self.channel.send(Message(attachments=[banner])) |
|
93
|
|
|
|