2017-07-10 08:58:51 +00:00
|
|
|
|
|
|
|
# https://github.com/Derpolino/discord-webhooks-python/blob/master/discordWebhooks.py
|
|
|
|
|
|
|
|
import requests
|
|
|
|
import json
|
2017-07-22 07:11:00 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
now = time.time()
|
2017-07-10 08:58:51 +00:00
|
|
|
|
|
|
|
# https://discordapp.com/developers/docs/resources/webhook#execute-webhook
|
|
|
|
class Webhook():
|
|
|
|
|
2017-12-13 11:37:31 +00:00
|
|
|
def __init__(self, url, **kwargs):
|
|
|
|
self.url = url
|
|
|
|
self.wh = {
|
|
|
|
#"username": kwargs.get("username", None),
|
|
|
|
#"avatar_url": kwargs.get("avatar_url", None),
|
|
|
|
"content": None,
|
|
|
|
"embeds": []
|
|
|
|
}
|
|
|
|
|
|
|
|
self.requests = requests.Session()
|
|
|
|
|
|
|
|
def add_embed(self, embed):
|
|
|
|
self.wh["embeds"].append(embed)
|
|
|
|
|
|
|
|
def clear_embeds(self):
|
|
|
|
self.wh["embeds"] = []
|
|
|
|
|
|
|
|
def send(self, content=None, tts=False):
|
|
|
|
self.wh["content"] = content
|
|
|
|
self.wh["tts"] = tts
|
|
|
|
#print(self.wh)
|
|
|
|
#return True # BORRAR ESTO
|
|
|
|
r = self.requests.post(self.url, json=self.wh)
|
|
|
|
|
|
|
|
if "X-RateLimit-Remaining" in r.headers and int(r.headers["X-RateLimit-Remaining"]) <= 1:
|
|
|
|
wait = int(r.headers["X-RateLimit-Reset"])-int(now)
|
|
|
|
raise Exception("RateLimit {0}/{1}, reset in ~{2}s".format(r.headers["X-RateLimit-Remaining"], r.headers["X-RateLimit-Limit"], wait))
|
|
|
|
|
|
|
|
"""rl = "RateLimit {0}/{1}, reset in ~{2}s".format(r.headers["X-RateLimit-Remaining"], r.headers["X-RateLimit-Limit"], wait)
|
|
|
|
if wait < 600:
|
|
|
|
print(rl)
|
|
|
|
time.sleep(wait+1)
|
|
|
|
else:
|
|
|
|
raise Exception(rl)"""
|
|
|
|
|
|
|
|
if r.text != "":
|
|
|
|
raise Exception("Webhook error: {0}".format(r.text))
|
|
|
|
return True
|
2017-07-10 08:58:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
# https://discordapp.com/developers/docs/resources/channel#embed-object
|
|
|
|
def embed(**kwargs):
|
2017-12-13 11:37:31 +00:00
|
|
|
return {
|
|
|
|
"title": kwargs.get("title", None),
|
|
|
|
"description": kwargs.get("description", None),
|
|
|
|
"url": kwargs.get("url", None),
|
|
|
|
"color": kwargs.get("color", None),
|
|
|
|
"image": kwargs.get("image", None),
|
|
|
|
"thumbnail": kwargs.get("thumbnail", None),
|
|
|
|
"footer": kwargs.get("footer", None),
|
|
|
|
"fields": kwargs.get("fields", [])
|
|
|
|
}
|
2017-07-10 08:58:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
def field(name, value, inline=False):
|
2017-12-13 11:37:31 +00:00
|
|
|
return {
|
|
|
|
"name": name,
|
|
|
|
"value": value,
|
|
|
|
"inline": inline
|
|
|
|
}
|
2017-07-10 08:58:51 +00:00
|
|
|
|
|
|
|
|
2017-07-20 10:56:47 +00:00
|
|
|
def image(url, w=50, h=50):
|
2017-12-13 11:37:31 +00:00
|
|
|
return {
|
|
|
|
"url": url,
|
|
|
|
"width": w,
|
|
|
|
"height": h
|
|
|
|
}
|
2017-07-20 10:56:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def thumbnail(url, w=50, h=50):
|
2017-12-13 11:37:31 +00:00
|
|
|
return {
|
|
|
|
"url": url,
|
|
|
|
"width": w,
|
|
|
|
"height": h
|
|
|
|
}
|
2017-07-20 10:56:47 +00:00
|
|
|
|
|
|
|
|
2017-07-10 08:58:51 +00:00
|
|
|
def footer(text, icon_url):
|
2017-12-13 11:37:31 +00:00
|
|
|
return {
|
|
|
|
"text": text,
|
|
|
|
"icon_url": icon_url
|
|
|
|
}
|
2017-07-10 08:58:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
__all__ = ["Webhook", "embed", "field", "footer"]
|