TotalFreedomBot/functions.py

76 lines
1.7 KiB
Python
Raw Normal View History

2020-11-21 12:54:01 +00:00
import json
import requests
from checks import *
2020-12-02 22:17:00 +00:00
def format_list_entry(embed, l, name):
embed.add_field(name="{} ({})".format(name, len(l)),
value=", ".join(l), inline=False)
2020-11-21 12:54:01 +00:00
return embed
2020-11-21 12:54:01 +00:00
def did_mention_other_user(users, author):
for user in users:
if user is not author:
return True
return False
2020-11-21 12:54:01 +00:00
def removed_user_mentions(old, new):
users = []
for user in old:
if user not in new:
users.append(user)
return users
2020-11-21 12:54:01 +00:00
def removed_role_mentions(old, new):
roles = []
for role in old:
if role not in new:
roles.append(role)
return roles
2020-11-21 12:54:01 +00:00
def get_avatar(user, animate=True):
if user.avatar_url:
avatar = str(user.avatar_url).replace(".webp", ".png")
else:
avatar = str(user.default_avatar_url)
if not animate:
avatar = avatar.replace(".gif", ".png")
return avatar
2020-11-21 12:54:01 +00:00
def read_json(file_name):
with open(f'/root/totalfreedom/{file_name}.json', 'r') as file:
data = json.load(file)
return data
2020-11-21 12:54:01 +00:00
def write_json(file_name, data):
with open(f'/root/totalfreedom/{file_name}.json', 'w') as file:
json.dump(data, file, indent=4)
2020-11-21 12:54:01 +00:00
return data
2020-11-21 12:54:01 +00:00
def hit_endpoint(command):
2020-12-08 22:58:39 +00:00
url = f"http://play.totalfreedom.me:3000?password=CENSORED&command={command}"
2020-11-21 12:54:01 +00:00
payload = {}
headers = {}
2020-12-08 22:48:05 +00:00
try:
response = json.loads(requests.request(
"GET", url, headers=headers, data=payload, timeout=100).text)
except:
response = 'Connection Error.'
2020-11-21 12:54:01 +00:00
return response['response']
2020-12-08 22:48:05 +00:00
def get_server_status():
try:
requests.get("http://play.totalfreedom.me:28966/list?json=true", timeout=5).json()
except:
return False
else:
return True