forked from randomCOSMOS/T-Rek
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
111 lines (93 loc) · 3.92 KB
/
app.py
File metadata and controls
111 lines (93 loc) · 3.92 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
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
import os
import json
from dotenv import load_dotenv
from discord import Intents, Client, Message, AllowedMentions
import firebase_admin
from firebase_admin import credentials, firestore
from datetime import datetime
from random import choice
# loading env variables
load_dotenv()
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
FIREBASE_TOKEN = os.getenv("FIREBASE_TOKEN")
# some predefined data
compliments = ["Great job!", "Excellent job!", "Impressive work!", "Great effort!", "Fantastic job!", "Well Done!"]
with open("members.json", "r") as file:
members = json.load(file)
# connecting to the firestore DB
cred = credentials.Certificate(FIREBASE_TOKEN)
app = firebase_admin.initialize_app(cred)
db = firestore.client()
# managing intents
intents = Intents.default()
intents.message_content = True
client = Client(intents=intents)
# logging when bot is online
@client.event
async def on_ready():
print(f"We have logged in as {client.user}")
# function when a msg is sent
@client.event
async def on_message(msg):
# ignore msg if sender is the bot itself, to prevent looping lol
if msg.author == client.user:
return
# only reply msg if prefix is "="
if msg.content.startswith("**") or msg.content.startswith("Prog"):
# msg.content = msg.content[1:]
username = str(msg.author)
user_message = msg.content
channel = msg.channel
# ask confirmation from user
confirmation = await msg.reply("Finalized your post? (Yes/No)", mention_author=True)
def check(m): # check if the channel and user are the same
return m.author == msg.author and m.channel == channel
try: # get the response or time out if afk
response = await client.wait_for('message', check=check, timeout=30.0)
except TimeoutError:
await confirmation.delete()
return
# what happens if response is yes
if response.content.lower() in ("yes", "y", "ye"):
# delete redundant messages
await confirmation.delete()
await response.delete()
# formatting data
try:
link = user_message.split("**Link:**")
project = link[0].split("**Project Status:**")
progress = project[0].split("**Progress:**")
data = {
"progress": progress[1].strip(),
"project status": project[1].strip(),
"link": link[1].strip()
}
except:
try:
link = user_message.split("Link:")
project = link[0].split("Project Status:")
progress = project[0].split("Progress:")
data = {
"progress": progress[1].strip(),
"project status": project[1].strip(),
"link": link[1].strip()
}
except:
await channel.send("The format was wrong! 😔")
return
# data is received and logged both side (client and server)
await channel.send(choice(compliments))
print(f"{datetime.now().strftime("%d/%m/%Y %H:%M:%S")} - Data Received - [{str(channel)}] {username}: '{user_message}'")
# data sent to firebase and logged on server side
doc_ref = db.collection(members[username]["name"]).document(members[username]["domain"])
if doc_ref.get().exists:
doc_ref.update({datetime.today().strftime('%d-%m-%Y'): data})
else:
doc_ref.set({datetime.today().strftime('%d-%m-%Y'): data})
print(f"{datetime.now().strftime("%d/%m/%Y %H:%M:%S")} - Data Stored Successfully!")
else:
await response.delete()
await confirmation.delete()
await channel.send("Ok discarding that!")
# running the bot.....
client.run(DISCORD_TOKEN)