-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
148 lines (105 loc) · 4.42 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import ctypes
import functools
import logging
import os
import socket
import sys
import time
import traceback
from typing import Optional, Sized, Union, Tuple
import discord
import requests
import ujson
import db
from constants import admin_user_id
def plural(count: Union[int, Sized]) -> str:
if isinstance(count, int):
return 's' if count != 1 else ''
else:
return 's' if len(count) != 1 else ''
def log_error(message: Optional[str] = None, flash_window: bool = True) -> str:
error = message if message else traceback.format_exc()
log.error(error)
if flash_window:
try:
ctypes.windll.user32.FlashWindow(ctypes.windll.kernel32.GetConsoleWindow(), True)
except AttributeError: # linux
pass
return error
async def report_error(client: discord.Client, message: Optional[str] = None):
error = log_error(message)
await (await user_from_id(client, admin_user_id)).send(f"```\n{error[-1990:]}```")
def handle_potential_request_error(req: requests.Response, code: int):
if req.status_code != code:
log.warning(f"Bad HTTP status_code: {req.status_code}, should be {code} (url={req.url})")
log.warning(req.text)
def detailed_user(message: Optional[discord.Message] = None, user: Optional[discord.User] = None) -> Optional[str]:
if message:
user = message.author
elif not user:
return
return f'{user.global_name} ({user.name}, {user.id})'
def nickname(author: discord.User) -> str:
nicknames = {234520815658336258: "Vamp",
587491655129759744: "EllaTAS",
513223843721117713: "The Senate",
671098132959985684: "Mr. Wolf",
226515080752267286: "Soloiini",
794291191726211103: "Ash",
761176028982018048: "Daniell"}
return nicknames[author.id] if author.id in nicknames else (author.global_name if author.global_name else author.name)
def load_sj_data() -> Tuple[dict, dict]:
with open('sj.json', 'r', encoding='UTF8') as sj_file:
sj_data: dict = ujson.load(sj_file)
sj_data_filenames = {sj_data[sj_map][4]: sj_map for sj_map in sj_data}
return sj_data, sj_data_filenames
def log_timestamp() -> str:
current_time = time.time()
current_time_local = time.localtime(current_time)
return time.strftime('%Y-%m-%d %H:%M:%S,', current_time_local) + str(round(current_time % 1, 3))[2:]
def missing_channel_permissions(channel: discord.TextChannel) -> list:
improvements_channel_permissions = channel.permissions_for(channel.guild.me)
permissions_needed = {'View Channel': improvements_channel_permissions.view_channel,
'Send Messages': improvements_channel_permissions.send_messages,
'Read Messages': improvements_channel_permissions.read_messages,
'Read Message History': improvements_channel_permissions.read_message_history,
'Add Reactions': improvements_channel_permissions.add_reactions,
'Manage Messages': improvements_channel_permissions.manage_messages}
return [perm for perm in permissions_needed if not permissions_needed[perm]]
def get_user_github_account(discord_id: int) -> Optional[list]:
try:
return db.githubs.get(discord_id, consistent_read=False)
except db.DBKeyError:
return
@functools.cache
def host() -> str:
if os.path.isfile('host'):
with open('host', 'r', encoding='UTF8') as host_file:
return host_file.read().strip('" \n')
else:
log_error("Couldn't determine host for about command")
return "Unknown"
def saved_log_name(base_name: str) -> str:
return f'{base_name}_{int(os.path.getmtime(f'{base_name}.log'))}_{socket.gethostname()}.log'
async def user_from_id(client: discord.Client, user_id: int) -> discord.User:
user = client.get_user(user_id)
if not user:
user = await client.fetch_user(user_id)
return user
class LogPlaceholder:
@staticmethod
def debug(msg):
print("DEBUG:", msg)
@staticmethod
def info(msg):
print("INFO:", msg)
@staticmethod
def warning(msg):
print("WARNING:", msg, file=sys.stderr)
@staticmethod
def error(msg):
print("ERROR:", msg, file=sys.stderr)
@staticmethod
def critical(msg):
print("CRITICAL:", msg, file=sys.stderr)
log: Optional[logging.Logger] = None