Skip to content

Instantly share code, notes, and snippets.

@Rapptz
Created Sep 30, 2016
Embed
What would you like to do?
import requests
import json
USER_TOKEN = 'token'
API_BASE = 'https://discordapp.com/api'
class Embeds:
def __init__(self, data):
basic_keys = ('title', 'type', 'description', 'url', 'timestamp', 'color',
'footer', 'image', 'thumbnail', 'provider', 'author')
for key in basic_keys:
setattr(self, key, data.get(key))
self.fields = data.get('fields', [])
class User:
def __init__(self, data):
self.username = data['username']
self.discriminator = data['discriminator']
self.id = int(data['id'])
self.avatar = data['avatar']
class Webhook:
def __init__(self, data):
self.name = data['name']
self.channel_id = int(data['channel_id'])
self.guild_id = int(data['guild_id'])
self.id = int(data['id'])
self.avatar = data['avatar']
self.token = data['token']
self.user = User(data['user'])
def execute(self, content, **kwargs):
payload = {
'content': content,
'username': kwargs.get('username')
}
headers = {
'Authorization': USER_TOKEN
}
url = '%s/webhooks/%s/%s' % (API_BASE, self.id, self.token)
r = requests.post(url, json=payload, headers=headers)
r.raise_for_status()
def create_channel_webhook(channel_id, *, name: str):
payload = {
'name': name,
'avatar': None
}
headers = {
'Authorization': USER_TOKEN
}
url = '%s/channels/%s/webhooks' % (API_BASE, channel_id)
resp = requests.post(url, json=payload, headers=headers)
resp.raise_for_status()
return Webhook(resp.json())
if __name__ == '__main__':
thing = create_channel_webhook(84319995256905728, name='testing')
print(thing.__dict__)
thing.execute(content='hello')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment