-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
63 lines (53 loc) · 1.84 KB
/
common.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
import os
from dotenv import load_dotenv
import logging
import datetime
from pathlib import Path
class Common():
load_dotenv()
logging_level = int(os.getenv('logging_level'))
filename = datetime.datetime.now().strftime("%Y%m%d") + "_banano_node_bot.log"
logdir = Path(__file__).resolve().parent / "logs"
# Make directory if it doesn't already exist
if not os.path.exists(logdir):
try:
os.makedirs(logdir)
except OSError as e:
print(f"Error creating {logdir} :", e)
exit()
logfile = logdir / filename
logging.basicConfig(filename=logfile, format='%(asctime)-10s - %(levelname)s - %(message)s', level=logging_level)
logger = logging.getLogger(__name__)
def __init__(self):
pass
# Helper function for changing seconds into nice formatted string of
# number of days, hours, minutes, and seconds
@staticmethod
def get_days_from_secs(secs):
# Turn seconds into days, hours, minutes, seconds
time = int(secs)
# Break down into days, hours, minutes, seconds
day = time // (24 * 3600)
time = time % (24 * 3600)
hours = time // 3600
time %= 3600
minutes = time // 60
time %= 60
seconds = time
return f"{day} days, {hours} hours, {minutes} minutes, and {seconds} seconds"
# Helper function for generic logging
@staticmethod
def log(msg):
Common.logger.info(msg)
# Convert units raw to banano. Banano is divisible by 30.
@staticmethod
def rawToBanano(raw):
return raw / 1e30
@staticmethod
def log_error(msg):
Common.logger.error(msg)
# Helper function for logging bot commands
# <- {User} : {command}
@staticmethod
def logit(ctx):
Common.logger.info(f"-> {ctx.message.author} : {ctx.command}")