-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdefault.py
More file actions
79 lines (57 loc) · 2.15 KB
/
Copy pathdefault.py
File metadata and controls
79 lines (57 loc) · 2.15 KB
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
import time
import json
import discord
import traceback
import timeago as timesince
from collections import namedtuple
from io import BytesIO
import os
def get(file):
try:
with open(file, encoding='utf8') as data:
return json.load(data, object_hook=lambda d: namedtuple('X', d.keys())(*d.values()))
except AttributeError:
raise AttributeError("命令執行失敗。")
except FileNotFoundError:
raise FileNotFoundError("找不到 JSON 檔案。")
def get_from_env(key: str):
val = os.environ.get(key)
if val is None:
return None
try:
return json.loads(val, object_hook=lambda d: namedtuple('X', d.keys())(*d.values()))
except AttributeError:
raise AttributeError("命令執行失敗。")
def traceback_maker(err, advance: bool = True):
_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):
return f"{name}_{int(time.time())}.txt"
def timeago(target):
return timesince.format(target)
def date(target, clock=True):
if clock is False:
return target.strftime("%d %B %Y")
return target.strftime("%d %B %Y, %H:%M")
def responsible(target, reason):
responsible = f"[ {target} ]"
if reason is None:
return f"{responsible} 沒有給出原因 ..."
return f"{responsible} {reason}"
def actionmessage(case, mass=False):
output = f"**{case}** the user"
if mass is True:
output = f"**{case}** the IDs/Users"
return f"✅ 成功地 {output}"
async def prettyResults(ctx, filename: str = "Results", resultmsg: str = "這是結果: ", loop=None):
if not loop:
return await ctx.send("結果為空 ...")
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()))
)