rDrama/files/helpers/discord.py

66 lines
2.2 KiB
Python
Raw Normal View History

2021-10-15 14:08:27 +00:00
from os import environ
import requests
import threading
2022-02-05 23:39:47 +00:00
from .const import *
2021-10-15 14:08:27 +00:00
SERVER_ID = environ.get("DISCORD_SERVER_ID",'').strip()
CLIENT_ID = environ.get("DISCORD_CLIENT_ID",'').strip()
CLIENT_SECRET = environ.get("DISCORD_CLIENT_SECRET",'').strip()
BOT_TOKEN = environ.get("DISCORD_BOT_TOKEN",'').strip()
AUTH = environ.get("DISCORD_AUTH",'').strip()
2021-10-15 14:08:27 +00:00
def discord_wrap(f):
def wrapper(*args, **kwargs):
user=args[0]
if not user.discord_id:
return
thread=threading.Thread(target=f, args=args, kwargs=kwargs)
thread.start()
wrapper.__name__=f.__name__
return wrapper
@discord_wrap
def add_role(user, role_name):
role_id = ROLES[role_name]
url = f"https://discordapp.com/api/guilds/{SERVER_ID}/members/{user.discord_id}/roles/{role_id}"
headers = {"Authorization": f"Bot {BOT_TOKEN}"}
2021-11-14 01:19:32 +00:00
requests.put(url, headers=headers, timeout=5)
2021-10-15 14:08:27 +00:00
2022-03-27 13:39:21 +00:00
@discord_wrap
def remove_role(user, role_name):
role_id = ROLES[role_name]
url = f"https://discordapp.com/api/guilds/{SERVER_ID}/members/{user.discord_id}/roles/{role_id}"
headers = {"Authorization": f"Bot {BOT_TOKEN}"}
requests.delete(url, headers=headers, timeout=5)
2021-10-15 14:08:27 +00:00
@discord_wrap
def remove_user(user):
url=f"https://discordapp.com/api/guilds/{SERVER_ID}/members/{user.discord_id}"
headers = {"Authorization": f"Bot {BOT_TOKEN}"}
2021-11-14 01:19:32 +00:00
requests.delete(url, headers=headers, timeout=5)
2021-10-15 14:08:27 +00:00
@discord_wrap
def set_nick(user, nick):
url=f"https://discordapp.com/api/guilds/{SERVER_ID}/members/{user.discord_id}"
headers = {"Authorization": f"Bot {BOT_TOKEN}"}
data={"nick": nick}
2021-11-14 01:19:32 +00:00
requests.patch(url, headers=headers, json=data, timeout=5)
2021-10-15 14:08:27 +00:00
2021-12-31 14:09:44 +00:00
def send_discord_message(message):
2021-10-15 14:08:27 +00:00
headers = {"Authorization": f"Bot {BOT_TOKEN}"}
data={"content": message}
2022-01-07 21:03:14 +00:00
requests.post("https://discordapp.com/api/channels/924485611715452940/messages", headers=headers, data=data, timeout=5)
2022-04-17 14:59:18 +00:00
requests.post("https://discordapp.com/api/channels/924486091795484732/messages", headers=headers, data=data, timeout=5)
def send_cringetopia_message(message):
headers = {"Authorization": f"Bot {BOT_TOKEN}"}
data={"content": message}
requests.post("https://discordapp.com/api/channels/965264044531527740/messages", headers=headers, data=data, timeout=5)