forked from DynamatrixOSS/pingernos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
112 lines (102 loc) · 4.04 KB
/
utils.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
from re import sub
from json import load, decoder
from os import getenv
from sys import exit as sysexit
from discord.ext.commands import HelpCommand
from discord.ext.bridge.context import BridgeContext, BridgeExtContext, BridgeApplicationContext
from discord import Embed
import mysql.connector as mysql
try:
from dotenv import load_dotenv
load_dotenv()
except ModuleNotFoundError:
print('You did not install the dotenv module! You will not be able to use a .env file.')
try:
from mcstatus import JavaServer
from mcstatus.pinger import PingResponse
except ModuleNotFoundError:
print('You did not install the mcstatus module! Exiting now...')
sysexit()
class Utils:
@staticmethod # This is a static method, you can call it without creating an instance of the class, but does not have access to the class or its attributes (self)
def remove_colors_from_string(text) -> str:
text = sub(r"§[0-9a-r]", "", text)
return text
class Colors:
blue = 0xadd8e6
red = 0xf04747
green = 0x90ee90
orange = 0xfaa61a
@staticmethod
def get_data() -> dict:
usejson = True # Set to True to a config.json
if usejson:
try:
with open('config.json', 'r', encoding="UTF-8") as file:
data = load(file)
except FileNotFoundError:
print('config.json not found! Exiting now...')
sysexit()
except decoder.JSONDecodeError:
print('config.json is not valid! Exiting now...')
sysexit()
if not usejson:
try:
data = {
"Token": getenv('TOKEN'),
"Prefix": getenv('PREFIX'),
"Owners": getenv('OWNERS').split(','),
"FeatureGuilds": getenv('FEATURE_GUILDS').split(','),
"Database": {
"Host": getenv('DB_HOST'),
"User": getenv('DB_USER'),
"Password": getenv('DB_PASSWORD'),
"Database": getenv('DB_DATABASE'),
"Port": getenv('DB_PORT')
},
"Logs": {
"JoinWebhook": getenv('LOGS_JOINWEBHOOK'),
"LeaveWebhook": getenv('LOGS_LEAVEWEBHOOK')
}
}
except AttributeError:
print('You did not fill out the environment variables! Exiting now...')
sysexit()
return data
@staticmethod
async def get_server_status(serverip: str) -> PingResponse:
server = await JavaServer.async_lookup(serverip)
stat = await server.async_status()
return stat
@staticmethod
async def mysql_login():
data = Utils.get_data()
return mysql.connect(
host=data['Database']['Host'],
user=data['Database']['User'],
password=data['Database']['Password'],
database=data['Database']['Database'])
class HelpCmd(HelpCommand):
async def send_bot_help(self, mapping):
channel = self.get_destination()
await channel.send("Type in `/` to see the commands!", reference=self.context.message, mention_author=False,
delete_after=15)
@staticmethod
async def respond(ctx: BridgeContext, message: str="", embed: Embed=None) -> None:
if isinstance(ctx, BridgeApplicationContext):
if embed is not None:
await ctx.respond(message, embed=embed)
return
await ctx.respond(message)
return
if isinstance(ctx, BridgeExtContext):
if embed is not None:
await ctx.respond(message, embed=embed, mention_author=False)
return
await ctx.respond(message, mention_author=False)
return
if embed is not None:
await ctx.respond(message, embed=embed)
return
await ctx.respond(message)
return