-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault.py
97 lines (73 loc) · 3.05 KB
/
default.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
import time
import json
import discord
import traceback
import timeago as timesince
import datetime
import calendar
from io import BytesIO
def config(filename: str = "config"):
""" Fetch default config file """
try:
with open(f"{filename}.json", encoding='utf8') as data:
return json.load(data)
except FileNotFoundError:
raise FileNotFoundError("JSON file wasn't found")
def traceback_maker(err, advance: bool = True):
""" A way to debug your code anywhere """
_traceback = ''.join(traceback.format_tb(err.__traceback__))
error = ('```py\n{1}{0}: {2}\n```').format(type(err).__name__, _traceback, err)
return error if advance else f"{type(err).__name__}: {err}"
def timetext(name):
""" Timestamp, but in text form """
return f"{name}_{int(time.time())}.txt"
def date(target, clock: bool = True, seconds: bool = False, ago: bool = False, only_ago: bool = False, raw: bool = False):
if isinstance(target, int) or isinstance(target, float):
target = datetime.datetime.utcfromtimestamp(target)
if raw:
if clock:
timestamp = target.strftime("%d %B %Y, %H:%M")
elif seconds:
timestamp = target.strftime("%d %B %Y, %H:%M:%S")
else:
timestamp = target.strftime("%d %B %Y")
if isinstance(target, int) or isinstance(target, float):
target = datetime.datetime.utcfromtimestamp(target)
target = calendar.timegm(target.timetuple())
if ago:
timestamp += f" ({timesince.format(target)})"
if only_ago:
timestamp = timesince.format(target)
return f"{timestamp} (UTC)"
else:
unix = int(time.mktime(target.timetuple()))
timestamp = f"<t:{unix}:{'f' if clock else 'D'}>"
if ago:
timestamp += f" (<t:{unix}:R>)"
if only_ago:
timestamp = f"<t:{unix}:R>"
return timestamp
def responsible(target, reason):
""" Default responsible maker targeted to find user in AuditLogs """
responsible = f"[ {target} ]"
if not reason:
return f"{responsible} no reason given..."
return f"{responsible} {reason}"
def actionmessage(case, mass=False):
""" Default way to present action confirmation in chat """
output = f"**{case}** the user"
if mass:
output = f"**{case}** the IDs/Users"
return f"✅ Successfully {output}"
async def prettyResults(ctx, filename: str = "Results", resultmsg: str = "Here's the results:", loop=None):
""" A prettier way to show loop results """
if not loop:
return await ctx.send("The result was empty...")
pretty = "\r\n".join([f"[{str(num).zfill(2)}] {data}" for num, data in enumerate(loop, start=1)])
if len(loop) < 15:
return await ctx.send(f"{resultmsg}```ini\n{pretty}```")
data = BytesIO(pretty.encode('utf-8'))
await ctx.send(
content=resultmsg,
file=discord.File(data, filename=timetext(filename.title()))
)