Compare commits

..

No commits in common. "c8cb828b7009104570433d6f200374bf5f64c932" and "ce6be5dc705cda3d790b06e55fea16b2622cfc53" have entirely different histories.

2 changed files with 27 additions and 40 deletions

2
.gitignore vendored
View file

@ -1,3 +1 @@
.env .env
__pycache__/**
cache.py

39
main.py
View file

@ -1,4 +1,4 @@
import os, sys import os
from slack_bolt import App from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler from slack_bolt.adapter.socket_mode import SocketModeHandler
from dotenv import load_dotenv from dotenv import load_dotenv
@ -22,18 +22,11 @@ print("Establishing a connection to slack...")
app = App(token=os.environ.get("SLACK_BOT_TOKEN")) app = App(token=os.environ.get("SLACK_BOT_TOKEN"))
client = app.client client = app.client
userMappings = {} print("Building a list of users, please stand by.")
try: users_list = []
if "--no-cache" in sys.argv: cursor = "N/A"
raise ImportError("User requested to skip cache") pages = 0
print("Trying to load user mappings from cache...") while cursor:
from cache import userMappings
except ImportError:
users_list = []
print("Cache load failed, falling back to full load from slack...")
cursor = "N/A"
pages = 0
while cursor:
data = "" data = ""
if cursor != "N/A": if cursor != "N/A":
data = client.users_list(cursor=cursor, limit=1000) data = client.users_list(cursor=cursor, limit=1000)
@ -43,21 +36,17 @@ except ImportError:
users_list.extend(data["members"]) users_list.extend(data["members"])
pages += 1 pages += 1
print(f"Pages of users loaded: {pages}") print(f"Pages of users loaded: {pages}")
del pages print("All pages loaded, generating user mappings now.")
print("Building user mappings now, this shouldn't take long...") del pages
userMappings = {} user_mappings = {}
for user in users_list: for user in users_list:
userMappings[f"<@{user['id']}>"] = ( user_mappings[f"<@{user['id']}>"] = (
f"<@{user['profile']['display_name']}>" f"<@{user['profile']['display_name']}>"
if user["profile"]["display_name"] if user["profile"]["display_name"]
else f"<@{user['id']}>" else f"<@{user['id']}>"
) )
print("All mappings generated, writing cache file now...")
with open("cache.py", "w") as cacheFile:
cacheFile.write(f"userMappings = {userMappings}")
print("Cache saved.")
print("User mappings loaded. User count:", len(userMappings)) print("User mappings loaded. User count:", len(user_mappings))
if __name__ == "__main__": if __name__ == "__main__":
print("^D at any time to terminate program") print("^D at any time to terminate program")
@ -85,8 +74,8 @@ if __name__ == "__main__":
print("Building messages, this might take a little bit...") print("Building messages, this might take a little bit...")
for i in range(len(messages)): for i in range(len(messages)):
label = f'{messages[i]["text"]} ({messages[i]["ts"]})' label = f'{messages[i]["text"]} ({messages[i]["ts"]})'
for user in userMappings: for user in user_mappings:
label = label.replace(user, userMappings[user]) label = label.replace(user, user_mappings[user])
texts[label] = i texts[label] = i
found = messages[ found = messages[
fp.menu( fp.menu(