bot: Made curl convert to md
All checks were successful
Build Docker / Build Docker (push) Successful in 33s

This commit is contained in:
Nathan Woodburn 2023-08-12 22:39:36 +10:00
parent be888fac87
commit a74943dec3
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
2 changed files with 36 additions and 4 deletions

37
bot.py
View File

@ -4,6 +4,7 @@ import discord
from discord import app_commands
import requests
import dns.resolver
import markdownify
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
@ -90,11 +91,41 @@ async def curl(ctx, url: str):
proxyURL = "https://proxy.hnsproxy.au"
response = requests.get(url, proxies={"http": proxyURL, "https": proxyURL},verify=False)
output = response.text
if (len(output) > 1900):
output = output[:1900]
await ctx.response.send_message(f"Response for {url}:\n{output}")
# Get BODY only
output = output.split("<body")[1]
output = output.split("</body>")[0]
output = output.split(">", 1)[1]
# Replace any relative links with absolute links
output = output.replace('href="/', 'href="' + url + '/')
parsed = markdownify.markdownify(output, heading_style="ATX")
# Delete any empty lines
parsed = "\n".join([s for s in parsed.splitlines() if s.strip()])
output = response.text
# Get title
if (output.find("<title>") != -1):
title = output.split("<title>")[1]
title = title.split("</title>")[0]
else:
title = url
if (len(parsed) > 4096):
parsed = parsed[:4096]
# Delete any incomplete lines
parsed = "\n".join(parsed.splitlines()[:-1])
parsed = parsed + "\n..."
# if url is a tld only replace it with the url https://hns.au (due to Discord not allowing tld only links)
if (url.find(".") == -1):
url = "https://hns.au"
# Create an embed
embed = discord.Embed(title=title, url=url, description=parsed)
embed.set_footer(text="Parsed by HNSProxy",icon_url="https://hns.au/assets/img/favicon.png")
embed.timestamp = discord.utils.utcnow()
await ctx.response.send_message(embed=embed)
except requests.exceptions.RequestException as e:
await ctx.response.send_message(f"An error occurred: {e}")
except Exception as e:
await ctx.response.send_message(f"An error occurred: {e}")
@tree.command(name="invite", description="Invite me to your server")
async def invite(ctx):

View File

@ -2,3 +2,4 @@ discord.py
python-dotenv
requests
dnspython
markdownify