mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2025-07-15 07:20:17 +02:00
Delete Discord users if they've left the server.
Closes #968 (cherry picked from commit 99b136b824d831f57a4000bb6813608083f1e4b5) Create new roles with desired attributes in one call. (cherry picked from commit ae4116c0f6a31997966505114f61b87745575dc1)
This commit is contained in:
parent
1ce041b90a
commit
a2f4226381
@ -1,7 +1,6 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import requests
|
|
||||||
import json
|
|
||||||
import re
|
import re
|
||||||
|
import requests
|
||||||
import math
|
import math
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from requests_oauthlib import OAuth2Session
|
from requests_oauthlib import OAuth2Session
|
||||||
@ -272,33 +271,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
|
||||||
|
@ -6,7 +6,7 @@ from alliance_auth.celeryapp import app
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
|
from requests.exceptions import HTTPError
|
||||||
from eveonline.managers import EveManager
|
from eveonline.managers import EveManager
|
||||||
from notifications import notify
|
from notifications import notify
|
||||||
from services.modules.discord.manager import DiscordOAuthManager, DiscordApiBackoff
|
from services.modules.discord.manager import DiscordOAuthManager, DiscordApiBackoff
|
||||||
@ -77,6 +77,15 @@ class DiscordTasks:
|
|||||||
logger.info("Discord group sync API back off for %s, "
|
logger.info("Discord group sync API back off for %s, "
|
||||||
"retrying in %s seconds" % (user, bo.retry_after_seconds))
|
"retrying in %s seconds" % (user, bo.retry_after_seconds))
|
||||||
raise task_self.retry(countdown=bo.retry_after_seconds)
|
raise task_self.retry(countdown=bo.retry_after_seconds)
|
||||||
|
except HTTPError as e:
|
||||||
|
if e.response.status_code == 404:
|
||||||
|
try:
|
||||||
|
if e.response.json()['code'] == 10007:
|
||||||
|
# user has left the server
|
||||||
|
DiscordTasks.delete_user(user)
|
||||||
|
return
|
||||||
|
finally:
|
||||||
|
raise e
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if task_self:
|
if task_self:
|
||||||
logger.exception("Discord group sync failed for %s, retrying in 10 mins" % user)
|
logger.exception("Discord group sync failed for %s, retrying in 10 mins" % user)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user