2021-01-07 19:13:55 +00:00
|
|
|
import itertools
|
2020-11-21 12:54:01 +00:00
|
|
|
import json
|
2021-01-04 23:41:53 +00:00
|
|
|
import os
|
2020-11-21 12:54:01 +00:00
|
|
|
|
2021-01-07 19:13:55 +00:00
|
|
|
import requests
|
2021-01-04 23:41:53 +00:00
|
|
|
from discord.ext import commands
|
2020-11-21 12:54:01 +00:00
|
|
|
|
2021-01-07 19:13:55 +00:00
|
|
|
|
|
|
|
class EmbedEntry:
|
|
|
|
def __init__(self, name, value, playercount=None):
|
2020-12-18 17:01:49 +00:00
|
|
|
self.name = name
|
|
|
|
self.value = value
|
|
|
|
if playercount:
|
|
|
|
self.playercount = playercount
|
2021-01-07 19:13:55 +00:00
|
|
|
|
|
|
|
|
2020-12-02 22:17:00 +00:00
|
|
|
def format_list_entry(embed, l, name):
|
2020-12-18 17:20:05 +00:00
|
|
|
l_names = [f'{l[i]}' for i in range(len(l))]
|
|
|
|
l_names = [name.replace('_', '\_') for name in l_names]
|
2021-01-07 19:13:55 +00:00
|
|
|
|
|
|
|
em = EmbedEntry(
|
|
|
|
name=name,
|
|
|
|
value=", ".join(l_names),
|
|
|
|
playercount=len(l)
|
|
|
|
)
|
2020-12-18 17:01:49 +00:00
|
|
|
return em
|
2020-12-02 21:59:01 +00:00
|
|
|
|
2021-01-04 23:41:53 +00:00
|
|
|
|
|
|
|
def get_prefix(client, message):
|
|
|
|
prefix = os.getenv('prefix')
|
|
|
|
prefixes = map(''.join, itertools.product(*((letter.upper(), letter.lower()) for letter in prefix)))
|
|
|
|
return commands.when_mentioned_or(*prefixes)(client, message)
|
|
|
|
|
|
|
|
|
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-12-02 21:59:01 +00:00
|
|
|
|
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-12-02 21:59:01 +00:00
|
|
|
|
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-12-02 21:59:01 +00:00
|
|
|
|
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-12-02 21:59:01 +00:00
|
|
|
|
2020-11-21 12:54:01 +00:00
|
|
|
def read_json(file_name):
|
2021-01-07 19:13:55 +00:00
|
|
|
with open(f'{file_name}.json', 'r') as file:
|
2020-11-21 12:54:01 +00:00
|
|
|
data = json.load(file)
|
|
|
|
return data
|
|
|
|
|
2020-12-02 21:59:01 +00:00
|
|
|
|
2020-11-21 12:54:01 +00:00
|
|
|
def write_json(file_name, data):
|
2021-01-07 19:13:55 +00:00
|
|
|
with open(f'{file_name}.json', 'w') as file:
|
2020-12-02 21:59:01 +00:00
|
|
|
json.dump(data, file, indent=4)
|
2020-11-21 12:54:01 +00:00
|
|
|
return data
|
|
|
|
|
2020-12-02 21:59:01 +00:00
|
|
|
|
2021-01-07 19:13:55 +00:00
|
|
|
def config_entry(entry):
|
|
|
|
return read_json('config')[entry]
|
|
|
|
|
|
|
|
|
2021-03-29 22:59:13 +00:00
|
|
|
def hit_endpoint(command, server=1, timeout=10):
|
2021-01-04 23:41:53 +00:00
|
|
|
config_file = read_json('config')
|
|
|
|
if server == 1:
|
|
|
|
ip = config_file['SERVER_IP']
|
|
|
|
pw = config_file['ENDPOINTS_PW']
|
|
|
|
else:
|
|
|
|
ip = config_file['SERVER_IP_2']
|
|
|
|
pw = config_file['ENDPOINTS_PW_2']
|
|
|
|
port = config_file['ENDPOINTS_PORT']
|
|
|
|
url = f"http://{ip}:{port}?password={pw}&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(
|
2021-03-29 22:59:13 +00:00
|
|
|
"GET", url, headers=headers, data=payload, timeout=timeout).text)
|
2021-01-07 19:13:55 +00:00
|
|
|
except Exception as e:
|
2021-03-29 22:59:13 +00:00
|
|
|
raise Exception(f'Error while hitting endpoint: {e}')
|
2020-11-21 12:54:01 +00:00
|
|
|
return response['response']
|
2021-01-07 19:13:55 +00:00
|
|
|
|
|
|
|
|
2021-01-04 23:41:53 +00:00
|
|
|
def get_server_status(server=1):
|
|
|
|
config_file = read_json('config')
|
|
|
|
if server == 1:
|
|
|
|
ip = config_file['SERVER_IP']
|
|
|
|
else:
|
|
|
|
ip = config_file['SERVER_IP_2']
|
|
|
|
port = config_file['PLAYERLIST_PORT']
|
2020-12-08 22:48:05 +00:00
|
|
|
try:
|
2021-01-04 23:41:53 +00:00
|
|
|
requests.get(f"http://{ip}:{port}/list?json=true", timeout=5).json()
|
2020-12-08 22:48:05 +00:00
|
|
|
except:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
2021-03-29 22:59:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_visible_player_count(list_json):
|
|
|
|
total = 0
|
|
|
|
for x in list_json:
|
2021-03-29 23:18:19 +00:00
|
|
|
if isinstance(list_json[x], list):
|
2021-03-29 22:59:13 +00:00
|
|
|
total += len(list_json[x])
|
2021-03-29 23:18:19 +00:00
|
|
|
|
2021-03-29 22:59:13 +00:00
|
|
|
return total
|