mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2025-07-16 07:50:16 +02:00
Initial work on Discord integration.
Manually created REST API manager as per the documented functions. Attempting to integrade discord-py wrapper, unsuccesful thusfar. Addresses #88
This commit is contained in:
parent
d14ec57575
commit
7e05054854
8
services/managers/discord_manager.py
Normal file
8
services/managers/discord_manager.py
Normal file
@ -0,0 +1,8 @@
|
||||
from services.managers.util import discord_client
|
||||
from django.conf import settings
|
||||
|
||||
class DiscordManager:
|
||||
def __init__(self, discord_client):
|
||||
self.discord_client = discord_client
|
||||
discord_client.client.run()
|
||||
super().__init__()
|
138
services/managers/util/discord_api_manager.py
Normal file
138
services/managers/util/discord_api_manager.py
Normal file
@ -0,0 +1,138 @@
|
||||
import requests
|
||||
import json
|
||||
from django.conf import settings
|
||||
|
||||
DISCORD_URL = "https://discordapp.com/api"
|
||||
|
||||
class DiscordManager:
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def get_auth_token():
|
||||
data = {
|
||||
"email" : settings.DISCORD_USER_EMAIL,
|
||||
"password": settings.DISCORD_USER_PASSWORD,
|
||||
}
|
||||
custom_headers = {'content-type':'application/json'}
|
||||
path = DISCORD_URL + "/auth/login"
|
||||
r = requests.post(path, headers=custom_headers, data=json.dumps(data))
|
||||
r.raise_for_status()
|
||||
return r.json()['token']
|
||||
|
||||
@staticmethod
|
||||
def add_server(name):
|
||||
data = {"name": name}
|
||||
custom_headers = {'content-type':'application/json', 'authorization': DiscordManager.get_auth_token()}
|
||||
path = DISCORD_URL + "/guilds"
|
||||
r = requests.post(path, headers=custom_headers, data=json.dumps(data))
|
||||
r.raise_for_status()
|
||||
return r.json()
|
||||
|
||||
@staticmethod
|
||||
def rename_server(server_id, name):
|
||||
data = {"name": name}
|
||||
custom_headers = {'content-type':'application/json', 'authorization': DiscordManager.get_auth_token()}
|
||||
path = DISCORD_URL + "/guilds/" + str(server_id)
|
||||
r = requests.patch(path, headers=custom_headers, data=json.dumps(data))
|
||||
r.raise_for_status()
|
||||
return r.json()
|
||||
|
||||
@staticmethod
|
||||
def delete_server(server_id):
|
||||
custom_headers = {'content-type':'application/json', 'authorization': DiscordManager.get_auth_token()}
|
||||
path = DISCORD_URL + "/guilds/" + str(server_id)
|
||||
r = requests.delete(path, headers=custom_headers)
|
||||
r.raise_for_status()
|
||||
|
||||
@staticmethod
|
||||
def get_members(server_id):
|
||||
custom_headers = {'accept':'application/json', 'authorization': DiscordManager.get_auth_token()}
|
||||
path = DISCORD_URL + "/guilds/" + str(server_id) + "/members"
|
||||
r = requests.get(path, headers=custom_headers)
|
||||
r.raise_for_status()
|
||||
return r.json()
|
||||
|
||||
@staticmethod
|
||||
def get_bans(server_id):
|
||||
custom_headers = {'accept':'application/json', 'authorization': DiscordManager.get_auth_token()}
|
||||
path = DISCORD_URL + "/guilds/" + str(server_id) + "/bans"
|
||||
r = requests.get(path, headers=custom_headers)
|
||||
r.raise_for_status()
|
||||
return r.json()
|
||||
|
||||
@staticmethod
|
||||
def ban_user(server_id, user_id, delete_message_age=0):
|
||||
custom_headers = {'authorization': DiscordManager.get_auth_token()}
|
||||
path = DISCORD_URL + "/guilds/" + str(server_id) + "/bans/" + str(user_id) + "?delete-message-days=" + str(delete_message_age)
|
||||
r = requests.put(path, headers=custom_headers)
|
||||
r.raise_for_status()
|
||||
|
||||
@staticmethod
|
||||
def unban_user(server_id, user_id):
|
||||
custom_headers = {'authorization': DiscordManager.get_auth_token()}
|
||||
path = DISCORD_URL + "/guilds/" + str(server_id) + "/bans/" + str(user_id)
|
||||
r = requests.delete(path, headers=custom_headers)
|
||||
r.raise_for_status()
|
||||
|
||||
@staticmethod
|
||||
def generate_role(server_id):
|
||||
custom_headers = {'accept':'application/json', 'authorization': DiscordManager.get_auth_token()}
|
||||
path = DISCORD_URL + "/guilds/" + str(server_id) + "/roles"
|
||||
r = requests.post(path, headers=custom_headers)
|
||||
r.raise_for_status()
|
||||
return r.json()
|
||||
|
||||
@staticmethod
|
||||
def edit_role(server_id, role_id, name, color=0, hoist=True, permissions=36953089):
|
||||
custom_headers = {'content-type':'application/json', 'authorization': DiscordManager.get_auth_token()}
|
||||
data = {
|
||||
'color': color,
|
||||
'hoist': hoist,
|
||||
'name': name,
|
||||
'permissions': permissions,
|
||||
}
|
||||
path = DISCORD_URL + "/guilds/" + str(server_id) + "/roles/" + str(role_id)
|
||||
r = requests.patch(path, headers=custom_headers, data=json.dumps(data))
|
||||
r.raise_for_status()
|
||||
return r.json()
|
||||
|
||||
|
||||
@staticmethod
|
||||
def delete_role(server_id, role_id):
|
||||
custom_headers = {'authorization': DiscordManager.get_auth_token()}
|
||||
path = DISCORD_URL + "/guilds/" + str(server_id) + "/roles/" + str(role_id)
|
||||
r = requests.delete(path, headers=custom_headers)
|
||||
r.raise_for_status()
|
||||
|
||||
@staticmethod
|
||||
def get_invite(invite_id):
|
||||
custom_headers = {'accept': 'application/json'}
|
||||
path = DISCORD_URL + "/invite/" + str(invite_id)
|
||||
r = requests.get(path, headers=custom_headers)
|
||||
r.raise_for_status()
|
||||
return r.json()
|
||||
|
||||
@staticmethod
|
||||
def accept_invite(invite_id):
|
||||
custom_headers = {'accept': 'application/json'}
|
||||
path = DISCORD_URL + "/invite/" + str(invite_id)
|
||||
r = requests.post(path, headers=custom_headers)
|
||||
r.raise_for_status()
|
||||
return r.json()
|
||||
|
||||
@staticmethod
|
||||
def create_invite(channel_id):
|
||||
custom_headers = {'authorization': DiscordManager.get_auth_token()}
|
||||
path = DISCORD_URL + "/channels/" + str(channel_id) + "/invites"
|
||||
r = requests.post(path, headers=custom_headers)
|
||||
r.raise_for_status()
|
||||
return r.json()
|
||||
|
||||
@staticmethod
|
||||
def delete_invite(invite_id):
|
||||
custom_headers = {'accept': 'application/json'}
|
||||
path = DISCORD_URL + "/invite/" + str(invite_id)
|
||||
r = requests.delete(path, headers=custom_headers)
|
||||
r.raise_for_status()
|
17
services/managers/util/discord_client.py
Normal file
17
services/managers/util/discord_client.py
Normal file
@ -0,0 +1,17 @@
|
||||
import discord
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
client = discord.Client()
|
||||
|
||||
print('Logging in to Discord as ' + settings.DISCORD_USER_EMAIL)
|
||||
client.login(settings.DISCORD_USER_EMAIL, settings.DISCORD_USER_PASSWORD)
|
||||
if not client.is_logged_in:
|
||||
print('Logging in to Discord failed')
|
||||
raise ValueError('Supplied Discord credentials failed login')
|
||||
|
||||
@client.event
|
||||
def on_ready():
|
||||
server = client.servers[0]
|
||||
user = client.user
|
||||
print('Connected as ' + user.name)
|
Loading…
x
Reference in New Issue
Block a user