Add ip command

This commit is contained in:
Nexus 2024-04-21 23:45:35 +01:00
parent c332d25adf
commit 3a6fc75f0d
Signed by: nex
GPG key ID: 0FA334385D0B689F

View file

@ -1,5 +1,6 @@
import asyncio
import io
import json
import os
import re
import time
@ -7,6 +8,7 @@ import typing
from pathlib import Path
import discord
import httpx
from discord.ext import commands
from dns import asyncresolver
from rich.console import Console
@ -253,6 +255,38 @@ class NetworkCog(commands.Cog):
await ctx.defer()
await ctx.respond(file=discord.File(f))
@commands.slash_command(name="ip")
async def get_ip_address(self, ctx: discord.ApplicationContext, lookup: str = None):
"""Fetches IP info from SHRONK IP servers"""
await ctx.defer()
async with httpx.AsyncClient(headers={"User-Agent": "Mozilla/5.0 Jimmy/v2"}) as client:
if not lookup:
response = await client.get("https://api.ipify.org")
lookup = response.text
servers = [
"ip.shronk.net",
"ip.i-am.nexus",
"ip.shronk.nicroxio.co.uk"
]
embed = discord.Embed(
title="IP lookup information for: %s" % lookup,
)
for server in servers:
try:
start = time.perf_counter()
response = await client.get(f"https://{server}/{lookup}")
end = time.perf_counter()
except (httpx.HTTPError, ConnectionError) as e:
embed.add_field(
name=server,
value=f"An error occurred while fetching the data: {e}",
)
else:
v = json.dumps(response.json(), indent=4)
embed.add_field(name="%s (%.2fms)" % (server, (end - start) * 1000), value="```json\n%s\n```" % v)
await ctx.respond(embed=embed)
def setup(bot):
bot.add_cog(NetworkCog(bot))