forked from natewong1313/bird-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.py
137 lines (112 loc) · 4.23 KB
/
webhook.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
import requests, time, datetime, json
#modified from https://github.com/lovvskillz/python-discord-webhook
class DiscordWebhook:
def __init__(self, url, **kwargs):
self.url = url
self.content = kwargs.get("content")
self.username = kwargs.get("username")
self.avatar_url = kwargs.get("avatar_url")
self.tts = kwargs.get("tts", False)
self.files = kwargs.get("files", dict())
self.embeds = kwargs.get("embeds", [])
self.proxies = kwargs.get("proxies", None)
def add_file(self, file, filename):
self.files["_{}".format(filename)] = (filename, file)
def add_embed(self, embed):
self.embeds.append(embed.__dict__ if isinstance(embed, DiscordEmbed) else embed)
def remove_embed(self, index):
self.embeds.pop(index)
def get_embeds(self):
return self.embeds
def set_proxies(self, proxies):
self.proxies = proxies
@property
def json(self):
data = dict()
embeds = self.embeds
self.embeds = list()
for embed in embeds:
self.add_embed(embed)
for key, value in self.__dict__.items():
if value and key not in ["url", "files", "filename"]:
data[key] = value
embeds_empty = all(not embed for embed in data["embeds"]) if "embeds" in data else True
return data
def execute(self):
if bool(self.files) is False:
response = requests.post(self.url, json=self.json, proxies=self.proxies)
else:
self.files["payload_json"] = (None, json.dumps(self.json))
response = requests.post(self.url, files=self.files, proxies=self.proxies)
class DiscordEmbed:
def __init__(self, **kwargs):
self.title = kwargs.get("title")
self.description = kwargs.get("description")
self.url = kwargs.get("url")
self.timestamp = kwargs.get("timestamp")
self.color = kwargs.get("color")
self.footer = kwargs.get("footer")
self.image = kwargs.get("image")
self.thumbnail = kwargs.get("thumbnail")
self.video = kwargs.get("video")
self.provider = kwargs.get("provider")
self.author = kwargs.get("author")
self.fields = kwargs.get("fields", [])
def set_title(self, title):
self.title = title
def set_description(self, description):
self.description = description
def set_url(self, url):
self.url = url
def set_timestamp(self, timestamp=str(datetime.datetime.utcfromtimestamp(time.time()))):
self.timestamp = timestamp
def set_color(self, color):
self.color = color
def set_footer(self, **kwargs):
self.footer = {
"text": kwargs.get("text"),
"icon_url": kwargs.get("icon_url"),
"proxy_icon_url": kwargs.get("proxy_icon_url")
}
def set_image(self, **kwargs):
self.image = {
"url": kwargs.get("url"),
"proxy_url": kwargs.get("proxy_url"),
"height": kwargs.get("height"),
"width": kwargs.get("width"),
}
def set_thumbnail(self, **kwargs):
self.thumbnail = {
"url": kwargs.get("url"),
"proxy_url": kwargs.get("proxy_url"),
"height": kwargs.get("height"),
"width": kwargs.get("width"),
}
def set_video(self, **kwargs):
self.video = {
"url": kwargs.get("url"),
"height": kwargs.get("height"),
"width": kwargs.get("width"),
}
def set_provider(self, **kwargs):
self.provider = {
"name": kwargs.get("name"),
"url": kwargs.get("url"),
}
def set_author(self, **kwargs):
self.author = {
"name": kwargs.get("name"),
"url": kwargs.get("url"),
"icon_url": kwargs.get("icon_url"),
"proxy_icon_url": kwargs.get("proxy_icon_url"),
}
def add_embed_field(self, **kwargs):
self.fields.append({
"name": kwargs.get("name"),
"value": kwargs.get("value"),
"inline": kwargs.get("inline", True)
})
def del_embed_field(self, index):
self.fields.pop(index)
def get_embed_fields(self):
return self.fields