Create new role with desired attributes in one call.

This commit is contained in:
Adarnof 2018-02-22 15:22:35 -05:00
parent 3080d7d868
commit ae4116c0f6

View File

@ -1,6 +1,4 @@
import requests import requests
import json
import re
import math import math
from django.conf import settings from django.conf import settings
from requests_oauthlib import OAuth2Session from requests_oauthlib import OAuth2Session
@ -270,33 +268,28 @@ class DiscordOAuthManager:
return cache.get_or_set(DiscordOAuthManager._generate_cache_role_key(name), get_or_make_role, GROUP_CACHE_MAX_AGE) return cache.get_or_set(DiscordOAuthManager._generate_cache_role_key(name), get_or_make_role, GROUP_CACHE_MAX_AGE)
@staticmethod @staticmethod
def __generate_role(): def __generate_role(name, **kwargs):
custom_headers = {'accept': 'application/json', 'authorization': 'Bot ' + settings.DISCORD_BOT_TOKEN} custom_headers = {'accept': 'application/json', 'authorization': 'Bot ' + settings.DISCORD_BOT_TOKEN}
path = DISCORD_URL + "/guilds/" + str(settings.DISCORD_GUILD_ID) + "/roles" path = DISCORD_URL + "/guilds/" + str(settings.DISCORD_GUILD_ID) + "/roles"
r = requests.post(path, headers=custom_headers) data = {'name': name}
data.update(kwargs)
r = requests.post(path, headers=custom_headers, json=data)
logger.debug("Received status code %s after generating new role." % r.status_code) logger.debug("Received status code %s after generating new role." % r.status_code)
r.raise_for_status() r.raise_for_status()
return r.json() return r.json()
@staticmethod @staticmethod
def __edit_role(role_id, name, color=0, hoist=False, permissions=36785152): def __edit_role(role_id, **kwargs):
custom_headers = {'content-type': 'application/json', 'authorization': 'Bot ' + settings.DISCORD_BOT_TOKEN} custom_headers = {'content-type': 'application/json', 'authorization': 'Bot ' + settings.DISCORD_BOT_TOKEN}
data = {
'color': color,
'hoist': hoist,
'name': name,
'permissions': permissions,
}
path = DISCORD_URL + "/guilds/" + str(settings.DISCORD_GUILD_ID) + "/roles/" + str(role_id) path = DISCORD_URL + "/guilds/" + str(settings.DISCORD_GUILD_ID) + "/roles/" + str(role_id)
r = requests.patch(path, headers=custom_headers, data=json.dumps(data)) r = requests.patch(path, headers=custom_headers, json=kwargs)
logger.debug("Received status code %s after editing role id %s" % (r.status_code, role_id)) logger.debug("Received status code %s after editing role id %s" % (r.status_code, role_id))
r.raise_for_status() r.raise_for_status()
return r.json() return r.json()
@staticmethod @staticmethod
def _create_group(name): def _create_group(name):
role = DiscordOAuthManager.__generate_role() return DiscordOAuthManager.__generate_role(name)
return DiscordOAuthManager.__edit_role(role['id'], name)
@staticmethod @staticmethod
@api_backoff @api_backoff