Update main.py

This commit is contained in:
Paillat
2023-02-21 11:05:26 +01:00
parent 595da5207b
commit 76aa30ba40

49
main.py
View File

@@ -39,7 +39,6 @@ async def get_ln(ctx: discord.AutocompleteContext):
return [color for color in languages if color.startswith(ctx.value.lower())] return [color for color in languages if color.startswith(ctx.value.lower())]
@bot.slash_command(name="private_present", description="Generate a presentation with marp, private command for user 707196665668436019") @bot.slash_command(name="private_present", description="Generate a presentation with marp, private command for user 707196665668436019")
#we create a function that takes the subject of the presentation and the style of the presentation as arguments, and that
@option(name="subject", description="The subject of the presentation", required=True) @option(name="subject", description="The subject of the presentation", required=True)
@option(name="style", description="The style of the presentation", required=False, autocomplete=get_style) @option(name="style", description="The style of the presentation", required=False, autocomplete=get_style)
@option(name="language", description="The language of the presentation", required=False, autocomplete=get_ln) @option(name="language", description="The language of the presentation", required=False, autocomplete=get_ln)
@@ -85,38 +84,9 @@ class:
else: marp = marp + "\n---" else: marp = marp + "\n---"
prompt = f"{intstructions} {indications} The subject of the presentation is: {subject} The Language is: {language} <|endofprompt|> \n {marp}" prompt = f"{intstructions} {indications} The subject of the presentation is: {subject} The Language is: {language} <|endofprompt|> \n {marp}"
subject2 = subject subject2 = subject
subject = subject.replace(" ", "-") forbidden = ["\\", "/", "?", "!", ":", ";", "(", ")", "[", "]", "{", "}", "'", '"', "=", "+", "*", "&", "^", "%", "$", "#", "@", "`", "~", "|", "<", ">", ",", ".", "?", " "]
#also replae all forbidden characters for i in forbidden:
subject = subject.replace("/", "-") if i in subject: subject = subject.replace(i, "-")
subject = subject.replace("?", "-")
subject = subject.replace("!", "-")
subject = subject.replace(":", "-")
subject = subject.replace(";", "-")
subject = subject.replace("(", "-")
subject = subject.replace(")", "-")
subject = subject.replace("[", "-")
subject = subject.replace("]", "-")
subject = subject.replace("{", "-")
subject = subject.replace("}", "-")
subject = subject.replace("'", "-")
subject = subject.replace('"', "-")
subject = subject.replace("=", "-")
subject = subject.replace("+", "-")
subject = subject.replace("*", "-")
subject = subject.replace("&", "-")
subject = subject.replace("^", "-")
subject = subject.replace("%", "-")
subject = subject.replace("$", "-")
subject = subject.replace("#", "-")
subject = subject.replace("@", "-")
subject = subject.replace("`", "-")
subject = subject.replace("~", "-")
subject = subject.replace("|", "-")
subject = subject.replace("<", "-")
subject = subject.replace(">", "-")
subject = subject.replace(",", "-")
subject = subject.replace(".", "-")
subject = subject.replace("?", "-")
#we save teh subject in base64 in a variable #we save teh subject in base64 in a variable
b64 = base64.urlsafe_b64encode(subject.encode("utf-8")) b64 = base64.urlsafe_b64encode(subject.encode("utf-8"))
#if dosen't exist, create a directory called "userid" where the userid is the id of the user who called the command #if dosen't exist, create a directory called "userid" where the userid is the id of the user who called the command
@@ -158,11 +128,12 @@ class:
print ("generating image " + images + "with " + str(use_images)) print ("generating image " + images + "with " + str(use_images))
r = await imagesGeneration.generate(images, f"{os.getcwd()}\\data\\{uid}\\{b64}{datenow}\\", str(use_images), apikey) r = await imagesGeneration.generate(images, f"{os.getcwd()}\\data\\{uid}\\{b64}{datenow}\\", str(use_images), apikey)
if str(use_images) == "sd": os.rename(f"{os.getcwd()}\\.\\data\\{uid}\\{b64}{datenow}\\{images}_0.png", f"{os.getcwd()}\\data\\{uid}\\{b64}{datenow}\\{images}.png") if str(use_images) == "sd": os.rename(f"{os.getcwd()}\\.\\data\\{uid}\\{b64}{datenow}\\{images}_0.png", f"{os.getcwd()}\\data\\{uid}\\{b64}{datenow}\\{images}.png")
if str(use_images) == "dalle": if str(use_images) == "dalle":
image_url = r['data'][0]['url'] image_url = r['data'][0]['url']
img_data = requests.get(image_url).content img_data = requests.get(image_url).content
with open(f'./data/{uid}/{b64}{datenow}/{images}.png', 'wb') as handler: with open(f'./data/{uid}/{b64}{datenow}/{images}.png', 'wb') as handler:
handler.write(img_data) handler.write(img_data)
asyncio.sleep(15) #wait 15 seconds to avoid rate limiting
with open(f"./data/{uid}/{b64}{datenow}/{subject}.md", "w", encoding="utf8") as f: f.write(present) with open(f"./data/{uid}/{b64}{datenow}/{subject}.md", "w", encoding="utf8") as f: f.write(present)
cmd = f"--pdf --allow-local-files ./data/{uid}/{b64}{datenow}/{subject}.md" cmd = f"--pdf --allow-local-files ./data/{uid}/{b64}{datenow}/{subject}.md"
if os.path.exists("./marp.exe"): if os.path.exists("./marp.exe"):
@@ -189,14 +160,10 @@ class:
@bot.slash_command(name="list", description="List all the presentations you have created") @bot.slash_command(name="list", description="List all the presentations you have created")
async def list(ctx: discord.ApplicationContext): async def list(ctx: discord.ApplicationContext):
#we create an embed with the list of presentations
embed = discord.Embed(title="Presentations", description="Here is the list of all the presentations you have created. You can download the presentation in different formats (pdf, markdown, html) by doing `/get` \"*presentation id*\". The images are generated by an ai. If you want to modify your presentation you can use the markdown file. More information about how to modify the file [HERE](https://marp.app).", color=0x00ff00) embed = discord.Embed(title="Presentations", description="Here is the list of all the presentations you have created. You can download the presentation in different formats (pdf, markdown, html) by doing `/get` \"*presentation id*\". The images are generated by an ai. If you want to modify your presentation you can use the markdown file. More information about how to modify the file [HERE](https://marp.app).", color=0x00ff00)
#we get the list of presentations
liste = await get_presentations(str(ctx.author.id)) liste = await get_presentations(str(ctx.author.id))
#we add the list of presentations to the embed
for key in liste: for key in liste:
embed.add_field(name=f"{liste[key]}", value=f"</get:1063051827010084925> `{key}`", inline=False) embed.add_field(name=f"{liste[key]}", value=f"</get:1063051827010084925> `{key}`", inline=False)
#now we send the embed
await ctx.respond(embed=embed, ephemeral=True) await ctx.respond(embed=embed, ephemeral=True)
async def get_presentations(uid): async def get_presentations(uid):
@@ -211,20 +178,14 @@ async def get_presentations(uid):
@option(name="pid", description="The id of the presentation", required=True) @option(name="pid", description="The id of the presentation", required=True)
async def get(ctx: discord.ApplicationContext, pid: str): async def get(ctx: discord.ApplicationContext, pid: str):
uid = str(ctx.author.id) uid = str(ctx.author.id)
#we get the list of presentations
liste = await get_presentations(uid) liste = await get_presentations(uid)
#we check if the presentation id is in the list
if pid in liste: if pid in liste:
#if it is we send the pdf, markdown and html files
files = [discord.File(f"./data/{uid}/{pid}/{liste[pid]}.pdf"), discord.File(f"./data/{uid}/{pid}/{liste[pid]}.md"), discord.File(f"./data/{uid}/{pid}/{liste[pid]}.html")] files = [discord.File(f"./data/{uid}/{pid}/{liste[pid]}.pdf"), discord.File(f"./data/{uid}/{pid}/{liste[pid]}.md"), discord.File(f"./data/{uid}/{pid}/{liste[pid]}.html")]
await ctx.respond(files=files, ephemeral=True) await ctx.respond(files=files, ephemeral=True)
#when the bot is ready we print a message #when the bot is ready we print a message
@bot.event @bot.event
async def on_ready(): async def on_ready():
print("Bot is ready") print("Bot is ready")
#print the name of the os (linux or windows)
print(str(os.name))
#if the data directory doesn't exist we create it
if not os.path.exists("data"): if not os.path.exists("data"):
os.mkdir("data") os.mkdir("data")
@bot.event @bot.event