-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
563 lines (434 loc) · 19 KB
/
bot.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
#actual python modules
import discord, os, json, subprocess, datetime
from discord.ext import commands
from dotenv import load_dotenv
from pathlib import Path
from qreader import QReader
import cv2
#my own python files
from geminillm import gemrequest
from urlextract import URLExtract
from cfradar import urlScan
from cfsd import sdgen
from htmlify import makePage
from UploadFile import uploadFileToCloud
import localconverters, cfllm, whispercf
load_dotenv()
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='?', intents=intents)
@bot.command()
async def llmreq(ctx, *, msg):
"""
Straight request to the llm backend.
Does not have any additional features, simply add what you'd like to send as a request.
"""
async with ctx.typing():
try:
resp = gemrequest(msg)
except:
resp = cfllm.llmrequest("You are a helpful assistant that doesn't use emoji", msg)
if resp[0] == True:
await ctx.send(resp[1])
else:
await ctx.send("Prompt likely blocked: "+resp[1])
@bot.command()
async def dumpMemory(ctx):
"""
Dump your memory file.
If exists, it'll try to send both your short term memory JSON file, and your long term memory text file.
"""
usr = str(ctx.message.author)
if Path("ltmemories/"+usr+".txt").exists():
await ctx.send(file=discord.File("ltmemories/"+usr+".txt"))
else:
await ctx.send("No long term memory file found for "+usr)
if Path("stmemories/"+usr+".json").exists():
await ctx.send(file=discord.File("stmemories/"+usr+".json"))
else:
await ctx.send("No short term memory file found for "+usr)
@bot.command()
async def current(ctx):
"""
Will return whether you're in a conversation.
Will have (stale) if your short term memory is older than an hour, in which case it'll be immediately purged (appended to long term memory) when you start a new conversation.
"""
usr = str(ctx.message.author)
if Path("stmemories/"+usr+".json").exists():
toSend = ""
with open("stmemories/"+usr+".json", "r", encoding="utf-8") as mem:
shortTermMemory = json.load(mem)#read short term memory
toSend += "Conversation ongoing for "+ usr +" with "+str(len(shortTermMemory)//2)+" interactions."
date = shortTermMemory[-2]["parts"][0][0:10].split("-")
time = shortTermMemory[-2]["parts"][0][11:19].split(":")
if datetime.datetime.now() - datetime.datetime(int(date[0]), int(date[1]), int(date[2]), int(time[0]), int(time[1]), int(time[2])) > datetime.timedelta(hours=1):
toSend += " (stale)"
else:
await ctx.send("No conversation ongoing for "+usr)
@bot.command()
async def finish(ctx):
"""
Finishes the current conversation.
Will summarize the current conversation and append it to your long term memory.
"""
usr = str(ctx.message.author)
with open("stmemories/"+usr+".json", "r", encoding="utf-8") as mem:
shortTermMemory = json.load(mem)#read short term memory
construct = "The following is a conversation between a chatbot and a user. Summarize the interaction briefly.\n"
for i in shortTermMemory:
construct += i["parts"][0]
construct += "\n"
modelReply = gemrequest(construct)
if modelReply[0] == False:
await ctx.send("something went wrong: "+modelReply[1])
return
else:#append summary to long term memory
with open("ltmemories/"+usr+".txt", "a", encoding="utf-8") as mem:
mem.write(str(datetime.datetime.today) + ": "+modelReply[1])
Path.unlink(Path("stmemories/"+usr+".json"))
await ctx.send("Current conversation summarized and appended to long term memory: "+modelReply[1])
@bot.command()
async def stmemPurge(ctx):
"""
Purges your short term memory file.
This will immediately and irreversibly delete your short term memory! (long term memory not affected)
"""
usr = str(ctx.message.author)
if Path("stmemories/"+usr+".json").exists():
Path.unlink(Path("stmemories/"+usr+".json"))
await ctx.send("Purged short term memory for "+usr)
else:
await ctx.send("No short term memory file found for "+usr)
@bot.command()
async def deregisterAllMemory(ctx):
"""
Deletes all short term and long term memory.
"""
usr = str(ctx.message.author)
if Path("ltmemories/"+usr+".txt").exists():
Path.unlink(Path("ltmemories/"+usr+".txt"))
if Path("stmemories/"+usr+".json").exists():
Path.unlink(Path("stmemories/"+usr+".json"))
await ctx.send("Deleted all memory permanantly for "+usr)
@bot.command()
async def talk(ctx, *, msg):
"""
Converse with the bot!
The bot will remember you to the best of its abilities.
"""
usr = str(ctx.message.author)
#short term memory: list (gemini API multi-turn)
#long term memory: -> Let's just make it a text file for simplicity (not sure how well it'll work)
if not Path("ltmemories/"+usr+".txt").exists():
with open("ltmemories/"+usr+".txt", "w", encoding="utf-8") as mem:
mem.write("")
longTermMemory = ""
else:
with open("ltmemories/"+usr+".txt", "r", encoding="utf-8") as mem:
longTermMemory = mem.read()
if not Path("stmemories/"+usr+".json").exists():
with open("stmemories/"+usr+".json", "w", encoding="utf-8") as mem:
mem.write("")
shortTermMemory = ""
else:
with open("stmemories/"+usr+".json", "r", encoding="utf-8") as mem:
shortTermMemory = json.load(mem)#read short term memory
date = shortTermMemory[-2]["parts"][0][0:10].split("-")
time = shortTermMemory[-2]["parts"][0][11:19].split(":")
time_difference = datetime.datetime.now() - datetime.datetime(int(date[0]), int(date[1]), int(date[2]), int(time[0]), int(time[1]), int(time[2]))
if time_difference > datetime.timedelta(hours=1):
construct = "The following is a conversation between a chatbot and a user. Summarize the interaction briefly.\n"
for i in shortTermMemory:
construct += i["parts"][0]
construct += "\n"
modelReply = gemrequest(construct)
if modelReply[0] == False:
await ctx.send("something went wrong: "+modelReply[1])
return
else:#append summary to long term memory
with open("ltmemories/"+usr+".txt", "a", encoding="utf-8") as mem:
mem.write(str(datetime.datetime.today)+": "+modelReply[1])
with open("stmemories/"+usr+".json", "w", encoding="utf-8") as mem:
mem.write("")
shortTermMemory = ""
async with ctx.typing():
construct = []
if shortTermMemory == "":
construct.append({'role':'user','parts': ["You are a helpful assistant, helping a user named " + usr + ". These are summaries of your previous conversations with the user for context: " + longTermMemory+"\n Do not include a timestamp in your reply."]})
else:
construct = shortTermMemory
construct.append({'role':'user',
'parts': [str(datetime.datetime.now()) + ": " + msg]})
try:
modelReply = gemrequest(construct)
if modelReply[0] == False:
raise
try:
await ctx.send(modelReply[1])
except:#too long
with open("temp.txt", "w", encoding="utf-8") as temp:
temp.write(modelReply)
await ctx.send("The response is too long to send! I'll give you a text file instead.",file = discord.File("temp.txt"))
Path.unlink(Path("temp.txt"))
except:
await ctx.send("Sorry, something seems to have gone wrong. Please try again. This conversation wasn't recorded into memory.")
return
construct.append({'role':'model', 'parts': [modelReply[1]]})
with open("stmemories/"+usr+".json", "w", encoding="utf-8") as mem:
json.dump(construct, mem)
@bot.command()
async def transcribe(ctx):
"""
Transcribes audio files with Whisper.
Cannot transcribe long files! (A few seconds ~ tens of seconds if fine)
"""
async with ctx.typing():
attachments = ctx.message.attachments
if len(attachments) == 0:
await ctx.send("I'll try to parse through a few previous messages to find what file you want transcribed...")
channel = ctx.channel
try:
messages = [message async for message in channel.history(limit=3)]
except discord.HTTPException as e:
await ctx.send(f"An error occurred: {e}")
return
for message in messages:
if len(message.attachments) != 0:
attachments += message.attachments
if len(attachments) == 0:
await ctx.send("I was unable to find an audio file to transcribe. Please try again.")
return
sendText = ""
for i in attachments:
try:
if not (str(i.content_type).split("/")[0] == "audio" ):
continue
sendText += i.filename + "\n"
sendText += "> " + whispercf.cfwhisper(i.url)["result"]["text"] + "\n"
except:
await ctx.send("Sorry, something went wrong while trying to transcribe file: " + i.filename)
await ctx.send(sendText)
@bot.command()
async def ytdlAudio(ctx, msg):
"""
Uses yt-dlp to download audio.
"""
async with ctx.typing():
url = URLExtract().find_urls(msg)[0]
subprocess.run(["yt-dlp","-x","-o","video",url])
localconverters.ffmpeg("video.opus","mp3")
await ctx.channel.send(file=discord.File("video.mp3"))
Path.unlink(Path("video.opus"))
Path.unlink(Path("video.mp3"))
@bot.command()
async def convert(ctx, msg):
"""
Converts media files with ffmpeg or imagemagick.
Will look for the first file's type and choose a converter.
"""
async with ctx.typing():
attachments = ctx.message.attachments
if len(attachments) == 0:
await ctx.send("You need to send me a file to convert!")
return
typ = attachments[0].content_type.split("/")[0]
if typ == "image":
conv = localconverters.imagemagick
elif typ == "video":
conv = localconverters.ffmpeg
elif typ == "audio":
conv = localconverters.ffmpeg
else:
await ctx.send("I can only convert things supported by ffmpeg or imagemagick!")
return
listnewfiles = []
listoldfiles = []
listnewfilenames = []
for i in attachments:
filename = i.filename
await i.save(fp=filename)
listoldfiles.append(filename)
conv(filename,msg)
splitname = filename.split(".")
noext = ""
for i in range(len(splitname)-1):
noext += splitname[i]
newfilename = noext+"."+msg
print(newfilename)
listnewfiles.append(discord.File(newfilename))
listnewfilenames.append(newfilename)
await ctx.channel.send(files=listnewfiles)
#Clean up the downloaded and converted files
for i in listoldfiles:
Path.unlink(Path(i))
for j in listnewfilenames:
Path.unlink(Path(j))
@bot.command()
async def ffmpeg(ctx, msg):
"""
Converts media files with ffmpeg.
"""
async with ctx.typing():
attachments = ctx.message.attachments
if len(attachments) == 0:
await ctx.send("I'll try to parse through a few previous messages to find what file you want to convert...")
channel = ctx.channel
try:
messages = [message async for message in channel.history(limit=3)]
except discord.HTTPException as e:
await ctx.send(f"An error occurred: {e}")
return
for message in messages:
if len(message.attachments) != 0:
attachments += message.attachments
if len(attachments) == 0:
await ctx.send("I was unable to find a file to convert. Please try again.")
return
filesToSend = []
filesToUnlink = []
for i in attachments:
filename = i.filename
await i.save(fp=filename)
localconverters.ffmpeg(filename,msg)
filesToSend.append(discord.File(filename+msg))
filesToUnlink.append(Path(filename))
splitname = filename.split(".")
noext = ""
for i in range(len(splitname)-1):
noext += splitname[i]
newfilename = noext+"."+msg
filesToUnlink.append(Path(newfilename))
await ctx.send(files=filesToSend)
for i in filesToUnlink:
Path.unlink(i)
@bot.command()
async def magick(ctx, msg):
"""
Converts media files with imagemagick.
"""
async with ctx.typing():
attachments = ctx.message.attachments
if len(attachments) == 0:
await ctx.send("I'll try to parse through a few previous messages to find what file you want to convert...")
channel = ctx.channel
try:
messages = [message async for message in channel.history(limit=3)]
except discord.HTTPException as e:
await ctx.send(f"An error occurred: {e}")
return
for message in messages:
if len(message.attachments) != 0:
attachments += message.attachments
if len(attachments) == 0:
await ctx.send("I was unable to find a file to convert. Please try again.")
return
filesToSend = []
filesToUnlink = []
for i in attachments:
filename = i.filename
await i.save(fp=filename)
localconverters.imagemagick(filename,msg)
filesToSend.append(discord.File(filename+msg))
filesToUnlink.append(Path(filename))
splitname = filename.split(".")
noext = ""
for i in range(len(splitname)-1):
noext += splitname[i]
newfilename = noext+"."+msg
filesToUnlink.append(Path(newfilename))
await ctx.send(files=filesToSend)
for i in filesToUnlink:
Path.unlink(i)
@bot.command()
async def urlscan(ctx, msg):
"""
Scan a URL with Cloudflare Radar.
"""
async with ctx.typing():
url = URLExtract.find_urls(msg)[0]
if url[-1] == "/":
url = url[0:-1]
print(url)
scanResults = urlScan(url)
try:
verdict, timeMade = scanResults
if verdict["malicious"]:
await ctx.channel.send("The URL is likely malicious, catergorized as "+str(verdict["categories"])+". The report was made on "+timeMade+". Do NOT access the webpage for your own safety.")
else:
await ctx.channel.send("The URL is likely safe. The report was made on "+timeMade+".")
except:
await ctx.channel.send("I've sent a request to scan the URL. The report should be available at https://radar.cloudflare.com/scan/"+scanResults+" in a few minutes. Check here, or run this command again!")
@bot.command()
async def qrscan(ctx):
"""
Scan a QR code embedded within an image.
"""
async with ctx.typing():
attachments = ctx.message.attachments
if len(attachments) == 0:
await ctx.send("I'll try to parse through a few previous messages to find what file you want to scan...")
channel = ctx.channel
try:
messages = [message async for message in channel.history(limit=5)]
except discord.HTTPException as e:
await ctx.send(f"An error occurred: {e}")
return
for message in messages:
if len(message.attachments) != 0:
attachments += message.attachments
if len(attachments) == 0:
await ctx.send("I was unable to find a file to scan. Please try again.")
return
qreader = QReader()
sendText = ""
for i in attachments:
try:
filename = i.filename
await i.save(fp=filename)
image = cv2.cvtColor(cv2.imread(filename), cv2.COLOR_BGR2RGB)
decoded_text = qreader.detect_and_decode(image=image)
if len(decoded_text) == 0:
sendText += "QR code not found for file: "+filename+"\n"
else:
sendText += "> "+filename+"\n"
for j in decoded_text:
sendText += j + "\n"
Path.unlink(Path(filename))
except:
sendText += "Error occured while processing file: "+filename+"\n"
await ctx.send(sendText)
@bot.command()
async def publish(ctx):
"""
Publish a txt file as a webpage for easier viewing on mobile.
(Warn: the file will be public. Share URLs at your own risk.)
"""
async with ctx.typing():
if len(attachments) == 0:
await ctx.send("I'll try to parse through a few previous messages to find what file you want to use...")
channel = ctx.channel
try:
messages = [message async for message in channel.history(limit=5)]
except discord.HTTPException as e:
await ctx.send(f"An error occurred: {e}")
return
for message in messages:
if len(message.attachments) != 0:
attachments = message.attachments
break
if len(attachments) == 0:
await ctx.send("I was unable to find a text file to publish. Please try again.")
return
for i in attachments:
filename = i.filename
await i.save(fp=filename)
with open(filename, "r", encoding="UTF-8") as txtfile:
htmlLoc = makePage(txtfile.readlines(), description="Automatically generated page from "+filename)
link = uploadFileToCloud(htmlLoc, "webpage/")
Path.unlink(Path(filename))
Path.unlink(Path(htmlLoc))
await ctx.send(link)
@bot.event
async def on_ready():
print(f'Logged in as {bot.user} (ID: {bot.user.id})')
bot.run(os.getenv("DISCORD_BOT_TOKEN"))