abidlabs HF staff commited on
Commit
501776d
1 Parent(s): d115945

Create utils.py

Browse files
Files changed (1) hide show
  1. utils.py +41 -0
utils.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import asyncio
4
+ import pickle
5
+ import hashlib
6
+ import pathlib
7
+ from typing import Dict, List
8
+
9
+ import discord
10
+
11
+ lock = asyncio.Lock()
12
+
13
+
14
+ async def update_pickle_file(data: Dict | List, file_path: str):
15
+ async with lock:
16
+ with open(file_path, "wb") as fp:
17
+ pickle.dump(data, fp)
18
+
19
+
20
+ def read_pickle_file(file_path: str):
21
+ with open(file_path, "rb") as fp:
22
+ return pickle.load(fp)
23
+
24
+
25
+ async def send_file_or_text(channel, file_or_text: str):
26
+ # if the file exists, send as a file
27
+ if pathlib.Path(str(file_or_text)).exists():
28
+ with open(file_or_text, "rb") as f:
29
+ return await channel.send(file=discord.File(f))
30
+ else:
31
+ return await channel.send(file_or_text)
32
+
33
+
34
+ def remove_tags(content: str) -> str:
35
+ content = content.replace("<@1040198143695933501>", "")
36
+ content = content.replace("<@1057338428938788884>", "")
37
+ return content.strip()
38
+
39
+
40
+ def hash_user_id(user_id: int) -> str:
41
+ return hashlib.sha256(str(user_id).encode("utf-8")).hexdigest()