-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.py
287 lines (226 loc) · 11 KB
/
source.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import selfcord
import re
import json
from poe_api_wrapper import PoeApi
def remove_urls(input_string):
# Define a regular expression pattern to match URLs
url_pattern = re.compile(r'https?://\S+|www\.\S+')
# Use the sub method to replace URLs with an empty string
result_string = url_pattern.sub('', input_string)
return result_string
# File path to store all data in a single file
ALL_DATA_FILE = "all_data.json"
# Load existing data from the file
try:
with open(ALL_DATA_FILE, 'r') as file:
all_data = json.load(file)
except FileNotFoundError:
# If the file is not found, initialize an empty dictionary
all_data = {"user_chat_codes": {}, "ai_author_data": {}, "user_ai_author_association": {}}
def save_all_data():
# Save all data to the file
with open(ALL_DATA_FILE, 'w') as file:
json.dump(all_data, file)
token = ""
bot = selfcord.Bot()
client = PoeApi("")
@bot.on("ready")
async def ready(time):
print(f"Connected To {bot.user.name}\n Startup took {time:0.2f} seconds")
@bot.on("message")
async def responder(message):
# Check if the message is a reply to the bot
if message.author == bot.user:
return ""
if str(bot.user.id) in message.content:
print("command found : " + message.content)
if(message.content == "<@1190289562711359600> ?"):
await message.channel.send("""
```@Daïmon [auteur], [question]```
Auteurs : \n
`Épicure, Platon, Aristote, Spinoza, Kant, Descartes, Rousseau, Hume, Hegel, Schopenhauer, Nietzsche, Sartre, Arendt, Wittgenstein`
""")
return ""
# Split the message content into parts
parts = message.content.split(",")
# Ensure the message contains enough parts
if len(parts) >= 2:
author = parts[0].strip() # Hobbes
author = author.replace("<@1190289562711359600> ", "").strip()
question = ",".join(parts[1:]).strip() # Join the remaining parts as the question
print("author = '" + author + "'")
ai_author = "unknown"
if(author.startswith("Socrate")):
ai_author = "Socrate-AI"
if(author.startswith("Heidegger")):
ai_author = "H-aidegger"
if(author.startswith("Épicure")):
ai_author = "Ep-aicure"
if(author.startswith("Platon")):
ai_author = "Pl-aiton"
if(author.startswith("Aristote")):
ai_author = "Ai-ristote"
if(author.startswith("Spinoza")):
ai_author = "Sp-ainoza"
if(author.startswith("Kant")):
ai_author = "K-aint"
if(author.startswith("Descartes")):
ai_author = "D-aicartes"
if(author.startswith("Hobbes")):
ai_author = "Hobb-ais"
if(author.startswith("Rousseau")):
ai_author = "Rousse-aiu"
if(author.startswith("Hume")):
ai_author = "Hume-ai"
if(author.startswith("Hegel")):
ai_author = "H-aigel"
if(author.startswith("Schopenhauer")):
ai_author = "Schopenh-aiuer"
if(author.startswith("Nietzsche")):
ai_author = "N-aitzsche"
if(author.startswith("Sartre")):
ai_author = "S-airtre"
if(author.startswith("Arendt")):
ai_author = "Ai-rendt"
if(author.startswith("Wittgenstein")):
ai_author = "Wittg-ainstein"
if(author.startswith("Commentaire")):
ai_author = "Platon-eprof-texte"
if(author.startswith("Dissertation")):
ai_author = "Platon-eprof"
if(ai_author == "unknown"):
#await message.channel.send("Hm, je connais bien Épicure, Platon, Aristote, Spinoza, Kant, Descartes, Rousseau, Hegel, Nietzsche, Sartre, Arendt... Je peux aussi répondre à des questions sur le commentaire de texte ou la dissertation ! Il suffit de me tag et d'indiquer le nom du philosophe ou l'exercice en premier dans le message.")
return ""
if author.startswith("Commentaire"):
# Handle the case where author starts with "Commentaire"
await message.channel.send("🧑🏫🎒 Analyse de commentaire / d'explication de texte en cours...")
elif author.startswith("Dissertation"):
# Handle the case where author starts with "Dissertation"
await message.channel.send("🧑🏫🎒 Analyse de dissertation en cours...")
else:
# Handle other cases
await message.channel.send("... ✨ " + author + " vient de se réveiller...")
# Do something with the decomposed parts (e.g., print or respond)
# Non-streamed example:
for chunk in client.send_message(ai_author, question):
pass
messageId = chunk["messageId"]
print("normal response = " + chunk["text"])
reponse = remove_urls(chunk["text"])
reponse = reponse.replace("]](", "]]")
data = client.get_citations(messageId)
citations = data['data']['message']['citations']
# Loop through citations and replace in the response
for i, citation in enumerate(citations, start=1):
formatted_citation = ">>> *"+citation['text']+"*" + "\n\n" + "```("+citation['title']+")```¤"
formatted_citation = ' '.join(formatted_citation.split())
reponse = reponse.replace(f"[[{i}]]", "\n\n "+formatted_citation+" \n\n")
# Split reponse using '¤' as the delimiter
response_parts = reponse.split('¤')
# Save the chatCode and ai_author in their respective dictionaries
all_data["user_chat_codes"][str(message.author.id)] = {"chatCode": chunk["chatCode"], "ai_author": ai_author}
all_data["ai_author_data"][ai_author] = ai_author # Add the ai_author if it doesn't exist
all_data["user_ai_author_association"][str(message.author.id)] = {"ai_author": ai_author}
all_data["user_ai_author_association"][message.author.id] = {"ai_author": author}
# Save all data to the file
save_all_data()
# Send each part separately
for part in response_parts:
await message.channel.send(part)
## else:
## # Handle the case where the message doesn't have enough parts
## await message.channel.send("Oh lala, et c'est le drame...")
@bot.on("message")
async def user_reply(message):
if(message.content.startswith("<")):
return ""
if(len(message.mentions) == 0):
print("pas de mention, ciao")
return ""
# Check if the message is a reply to the bot
if message.author != bot.user:
print(message.author)
print(bot.user)
if str(message.author.id) in all_data["user_chat_codes"]:
author_id = str(message.author.id)
ai_author = ""
user_association_data = all_data["user_ai_author_association"][author_id]
ai_author = user_association_data["ai_author"]
# sanitiaze the message
question = message.content.strip()
if(ai_author.startswith("Socrate")):
ai_author = "Socrate-AI"
if(ai_author.startswith("Heidegger")):
ai_author = "H-aidegger"
if(ai_author.startswith("Épicure")):
ai_author = "Ep-aicure"
if(ai_author.startswith("Platon")):
ai_author = "Pl-aiton"
if(ai_author.startswith("Aristote")):
ai_author = "Ai-ristote"
if(ai_author.startswith("Spinoza")):
ai_author = "Sp-ainoza"
if(ai_author.startswith("Kant")):
ai_author = "K-aint"
if(ai_author.startswith("Descartes")):
ai_author = "D-aicartes"
if(ai_author.startswith("Hobbes")):
ai_author = "Hobb-ais"
if(ai_author.startswith("Rousseau")):
ai_author = "Rousse-aiu"
if(ai_author.startswith("Hume")):
ai_author = "Hume-ai"
if(ai_author.startswith("Hegel")):
ai_author = "H-aigel"
if(ai_author.startswith("Schopenhauer")):
ai_author = "Schopenh-aiuer"
if(ai_author.startswith("Nietzsche")):
ai_author = "N-aitzsche"
if(ai_author.startswith("Sartre")):
ai_author = "S-airtre"
if(ai_author.startswith("Arendt")):
ai_author = "Ai-rendt"
if(ai_author.startswith("Wittgenstein")):
ai_author = "Wittg-ainstein"
if(ai_author.startswith("Commentaire")):
ai_author = "Platon-eprof-texte"
if(ai_author.startswith("Dissertation")):
ai_author = "Platon-eprof"
print("reply question = " + question)
print("reply ai author = " + ai_author)
chat_code_data = all_data["user_chat_codes"][str(message.author.id)]
chat_code = chat_code_data["chatCode"]
print("reply chat code = " + chat_code)
#ai_author = chat_code_data["ai_author"]
# Check if the user has previously interacted with the specific ai_author
if ai_author in all_data["ai_author_data"]:
print("reply author = '" + ai_author + "'")
# Do something with the decomposed parts (e.g., print or respond)
# Non-streamed example:
for chunk in client.send_message(ai_author, question, chatCode=chat_code):
pass
messageId = chunk["messageId"]
print("reply response = " + chunk["text"])
reponse = remove_urls(chunk["text"])
reponse = reponse.replace("]](", "]]")
data = client.get_citations(messageId)
citations = data['data']['message']['citations']
# Loop through citations and replace in the response
for i, citation in enumerate(citations, start=1):
formatted_citation = ">>> *"+citation['text']+"*" + "\n\n" + "```("+citation['title']+")```¤"
formatted_citation = ' '.join(formatted_citation.split())
reponse = reponse.replace(f"[[{i}]]", "\n\n "+formatted_citation+" \n\n")
# Split reponse using '¤' as the delimiter
response_parts = reponse.split('¤')
# Save the chatCode and ai_author in their respective dictionaries
all_data["user_chat_codes"][str(message.author.id)] = {"chatCode": chunk["chatCode"], "ai_author": ai_author}
all_data["ai_author_data"][ai_author] = ai_author # Add the ai_author if it doesn't exist
all_data["user_ai_author_association"][str(message.author.id)] = {"ai_author": ai_author}
# Save all data to the file
save_all_data()
# Send each part separately
for part in response_parts:
await message.channel.send(part)
else:
save_all_data()
bot.run(token)