dotfiles/scripts/discord-whois

132 lines
3.8 KiB
Text
Raw Normal View History

2020-05-29 18:46:19 +00:00
#!/usr/bin/env python3
# <https://discord.com/developers/docs/resources/user#user-object>
# <https://discord.com/developers/docs/resources/user#user-object#get-user>
# <https://discord.com/developers/docs/reference>
2020-05-29 18:46:19 +00:00
import sys
import os
import requests
import colorama
import time
import argparse
2020-05-29 18:46:19 +00:00
DISCORD_EPOCH = 1420070400000 # milliseconds
# https://discord.com/developers/docs/resources/user#user-object-user-flags
DISCORD_FLAGS = {
"Discord Employee": 1 << 0,
"Discord Partner": 1 << 1,
"HypeSquad Events": 1 << 2,
"Bug Hunter Level 1": 1 << 3,
"House of Bravery": 1 << 6,
"House of Brilliance": 1 << 7,
"House of Balance": 1 << 8,
"Early Supporter": 1 << 9,
"Team User": 1 << 10,
"System": 1 << 12,
"Bug Hunter Level 2": 1 << 14,
"Verified Bot": 1 << 16,
"Verified Bot Developer": 1 << 17,
}
parser = argparse.ArgumentParser()
parser.add_argument("user_snowflake", type=int)
parser.add_argument("--bot-token", type=str)
parser.add_argument("--image-size", type=int)
parser.add_argument("--get-prop", type=str)
cli_args = parser.parse_args()
user_snowflake = cli_args.user_snowflake
bot_token = cli_args.bot_token
if bot_token is None:
with open(
os.path.expanduser("~/.config/dotfiles/discord-tools-bot-token.txt")
) as f:
bot_token = f.read().strip()
image_size = cli_args.image_size
if not (image_size is None or (image_size > 0 and image_size & (image_size - 1)) == 0):
parser.error("image_size must be greater than zero and a power of two")
2020-05-29 18:46:19 +00:00
# no timeout here, sadly, due to this genius: <https://github.com/psf/requests/issues/3099#issuecomment-215522806>
2020-05-29 18:46:19 +00:00
response = requests.get(
"https://discordapp.com/api/users/{}".format(user_snowflake),
headers={"Authorization": "Bot {}".format(bot_token)},
timeout=3,
2020-05-29 18:46:19 +00:00
)
try:
response.raise_for_status()
except requests.HTTPError as err:
print(response.json(), file=sys.stderr)
2020-05-29 18:46:19 +00:00
raise err
raw_data = response.json()
data = {}
2020-05-29 18:46:19 +00:00
data["ID"] = raw_data["id"]
data["Name"] = "{}#{}".format(raw_data["username"], raw_data["discriminator"])
default_avatar_url = "https://cdn.discordapp.com/embed/avatars/{}.png".format(
int(raw_data["discriminator"], 10) % 5
)
avatar_url = (
2020-05-29 18:46:19 +00:00
"https://cdn.discordapp.com/avatars/{}/{}.{}".format(
raw_data["id"],
raw_data["avatar"],
"gif" if raw_data["avatar"].startswith("a_") else "png",
)
if raw_data["avatar"] is not None
else default_avatar_url
)
if image_size is not None:
avatar_url += "?size={}".format(image_size)
data["Avatar"] = avatar_url
data["Default avatar"] = default_avatar_url
data["Bot"] = raw_data.get("bot", False)
data["System user"] = raw_data.get("system", False)
2020-05-29 18:46:19 +00:00
# https://discord.com/developers/docs/reference#convert-snowflake-to-datetime
snowflake_creation_time = (user_snowflake >> 22) + DISCORD_EPOCH
data["Created at"] = "{}.{} UTC".format(
time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(snowflake_creation_time // 1000)),
snowflake_creation_time % 1000,
2020-05-29 18:46:19 +00:00
)
user_flags = raw_data["public_flags"]
2020-05-29 18:46:19 +00:00
if user_flags == 0:
data["Flags"] = "<none>"
2020-05-29 18:46:19 +00:00
else:
user_flag_names = []
for flag_name, bitmask in DISCORD_FLAGS.items():
if user_flags & bitmask:
user_flag_names.append(flag_name)
data["Flags"] = ", ".join(user_flag_names)
if cli_args.get_prop is None:
max_name_length = max(map(len, data.keys()))
for name, value in data.items():
if value is True:
value = "yes"
elif value is False:
value = "no"
print(
"{}{:>{}}:{} {}".format(
colorama.Style.BRIGHT,
name,
max_name_length + 1,
colorama.Style.RESET_ALL,
value,
)
)
else:
print(data[cli_args.get_prop])