import os from dotenv import load_dotenv import discord from discord import app_commands import requests import dns.resolver import markdownify load_dotenv() TOKEN = os.getenv('DISCORD_TOKEN') ADMINID = 0 KUTT_APIKEY=os.getenv('LINK_API_KEY') KUTT_URL=os.getenv('LINK_URL') LOG_CHANNEL = int(os.getenv('LOG_CHANNEL')) intents = discord.Intents.default() client = discord.Client(intents=intents) tree = app_commands.CommandTree(client) activityMessage="over the server" statusType="watching" # Commands @tree.command(name="ping", description="Check bot connection") async def ping(ctx): await ctx.response.send_message("Pong!",ephemeral=True) @tree.command(name="shortlink", description="Shorten a link") async def shortlink(ctx, link: str, name: str = None): if (ctx.user.id != ADMINID): await log("User: " + str(ctx.user.name) + " tried to use the shortlink command") await ctx.response.send_message("You don't have permission to use this command",ephemeral=True) else: url=f"{KUTT_URL}/api/v2/links" headers = {'X-API-KEY' : KUTT_APIKEY} data = {'target' : link, 'customurl' : name} if (name == None): data = {'target' : link} x = requests.post(url, data = data, headers = headers) print(x.text) if (x.status_code != 200 and x.status_code != 201): await ctx.response.send_message("ERROR: " + x.text,ephemeral=True) link=x.json()['link'] await ctx.response.send_message("Link: " + link,ephemeral=False) @tree.command(name="botstatus", description="Set the bot status") async def botstatus(ctx, message: str, statusmethod: str = None): if (ctx.user.id != ADMINID): await log("User: " + str(ctx.user.name) + " tried to use the botstatus command") await ctx.response.send_message("You don't have permission to use this command",ephemeral=True) else: global activityMessage activityMessage=message global statusType if (statusmethod == None): statusmethod="watching" else: statusType=statusmethod.lower() updateStatus() await ctx.response.send_message("Status updated",ephemeral=True) @tree.command(name="dig", description="Dig a dns record") async def dig(ctx, domain: str, record_type: str = "A"): record_type = record_type.upper() # Format dig @100.74.29.146 -p 5350 resolver = dns.resolver.Resolver() resolver.nameservers = ["100.74.29.146"] resolver.port = 5350 try: # Query the DNS record response = resolver.resolve(domain, record_type) print(response) records = "" for record in response: print(record) records = records + "\n" + str(record) # Send the result to the Discord channel await ctx.response.send_message(f"DNS records for {domain} ({record_type}):{records}") except dns.resolver.NXDOMAIN: await ctx.response.send_message(f"Domain {domain} not found.") except dns.exception.DNSException as e: await ctx.response.send_message(f"An error occurred: {e}") @tree.command(name="curl", description="HTTP request") async def curl(ctx, url: str): try: proxyURL = "https://proxy.hnsproxy.au" response = requests.get(url, proxies={"http": proxyURL, "https": proxyURL},verify=False) output = response.text # Get BODY only output = output.split("")[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("") != -1): title = output.split("<title>")[1] title = title.split("")[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): await ctx.response.send_message("https://discord.com/api/oauth2/authorize?client_id=1006128164218621972&permissions=0&scope=bot",ephemeral=True) async def log(message): channel=client.get_channel(LOG_CHANNEL) await channel.send(message) def updateStatus(): global activityMessage global statusType if (statusType == "watching"): activity=discord.Activity(type=discord.ActivityType.watching, name=activityMessage) elif (statusType == "playing"): activity=discord.Activity(type=discord.ActivityType.playing, name=activityMessage) elif (statusType == "listening"): activity=discord.Activity(type=discord.ActivityType.listening, name=activityMessage) elif (statusType == "competing"): activity=discord.Activity(type=discord.ActivityType.competing, name=activityMessage) else: activity=discord.Activity(type=discord.ActivityType.watching, name=activityMessage) client.loop.create_task(client.change_presence(activity=activity)) # When the bot is ready @client.event async def on_ready(): global ADMINID ADMINID = client.application.owner.id await log("Bot is starting...") await tree.sync() updateStatus() await log("Bot is ready") client.run(TOKEN)