Update functions.py

This commit is contained in:
Elmon11 2021-01-05 00:41:53 +01:00 committed by GitHub
parent a87dbc94a7
commit 166bac76cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,16 +1,18 @@
import json import json
import requests import requests
import os
import itertools
from discord.ext import commands
from checks import * from checks import *
class embed_entry: class embed_entry:
def __init__(self, name, value, *, playercount): def __init__(self, name, value, *, playercount):
self.name = name self.name = name
self.value = value self.value = value
if playercount: if playercount:
self.playercount = playercount self.playercount = playercount
def format_list_entry(embed, l, name): def format_list_entry(embed, l, name):
l_names = [f'{l[i]}' for i in range(len(l))] l_names = [f'{l[i]}' for i in range(len(l))]
l_names = [name.replace('_', '\_') for name in l_names] l_names = [name.replace('_', '\_') for name in l_names]
@ -22,6 +24,14 @@ def format_list_entry(embed, l, name):
) )
return em return em
def get_prefix(client, message):
#prefixes = ['TF!', 'Tf!', 'tF!', 'tf!']
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): def did_mention_other_user(users, author):
for user in users: for user in users:
if user is not author: if user is not author:
@ -44,12 +54,6 @@ def removed_role_mentions(old, new):
roles.append(role) roles.append(role)
return roles return roles
def get_prefix(bot, message):
#prefixes = ['TF!', 'Tf!', 'tF!', 'tf!']
prefix = os.getenv('prefix')
prefixes = map(''.join, itertools.product(*((letter.upper(), letter.lower()) for letter in prefix)))
return commands.when_mentioned_or(*prefixes)(bot, message)
def get_avatar(user, animate=True): def get_avatar(user, animate=True):
if user.avatar_url: if user.avatar_url:
@ -73,20 +77,34 @@ def write_json(file_name, data):
return data return data
def hit_endpoint(command): def hit_endpoint(command, server=1):
url = f"http://play.totalfreedom.me:3000?password=CENSORED&command={command}" 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 = {} payload = {}
headers = {} headers = {}
try: try:
response = json.loads(requests.request( response = json.loads(requests.request(
"GET", url, headers=headers, data=payload, timeout=100).text) "GET", url, headers=headers, data=payload, timeout=5).text)
except: except:
response = 'Connection Error.' response = {'response': 'Connection Error.'}
return response['response'] return response['response']
def get_server_status(): 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: try:
requests.get("http://play.totalfreedom.me:28966/list?json=true", timeout=5).json() requests.get(f"http://{ip}:{port}/list?json=true", timeout=5).json()
except: except:
return False return False
else: else: