This repository has been archived by the owner on Jun 16, 2022. It is now read-only.
forked from AtlasMediaGroup/TotalFreedomBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
125 lines (96 loc) · 2.99 KB
/
functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import itertools
import json
import os
import requests
from discord.ext import commands
class EmbedEntry:
def __init__(self, name, value, playercount=None):
self.name = name
self.value = value
if playercount:
self.playercount = playercount
def format_list_entry(embed, l, name):
l_names = [f'{l[i]}' for i in range(len(l))]
l_names = [name.replace('_', '\_') for name in l_names]
em = EmbedEntry(
name=name,
value=", ".join(l_names),
playercount=len(l)
)
return em
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)
def did_mention_other_user(users, author):
for user in users:
if user is not author:
return True
return False
def removed_user_mentions(old, new):
users = []
for user in old:
if user not in new:
users.append(user)
return users
def removed_role_mentions(old, new):
roles = []
for role in old:
if role not in new:
roles.append(role)
return roles
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
def read_json(file_name):
with open(f'{file_name}.json', 'r') as file:
data = json.load(file)
return data
def write_json(file_name, data):
with open(f'{file_name}.json', 'w') as file:
json.dump(data, file, indent=4)
return data
def config_entry(entry):
return read_json('config')[entry]
def hit_endpoint(command, server=1, timeout=10):
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}"
payload = {}
headers = {}
try:
response = json.loads(requests.request(
"GET", url, headers=headers, data=payload, timeout=timeout).text)
except Exception as e:
raise Exception(f'Error while hitting endpoint: {e}')
return response['response']
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']
try:
requests.get(f"http://{ip}:{port}/list?json=true", timeout=5).json()
except:
return False
else:
return True
def get_visible_player_count(list_json):
total = 0
for x in list_json:
if isinstance(list_json[x], list):
total += len(list_json[x])
return total