├── README.md └── ip-bot.py /README.md: -------------------------------------------------------------------------------- 1 | # Discord IP Lookup Bot 2 | Checks for messages sent to Discord server containing an IP and looks up according information. Sending embed with details to channel message sent. Supports multiple IPs in single message, provides temperature at given IP location. 3 | 4 | **Requires:** 5 | - Python 3.6 or later. 6 | - API key from https://openweathermap.org/ 7 | - Discord bot token from developer portal. 8 | 9 | Uses http://ip-api.com for IP lookup. 10 | 11 | ![Example Embed](https://i.imgur.com/EgmfuUY.png) 12 | -------------------------------------------------------------------------------- /ip-bot.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import discord 3 | import json 4 | import re 5 | 6 | client = discord.Client() 7 | weather_api_key = "KEY" #API key from https://openweathermap.org/ 8 | discord_api_key = "TOKEN" #Discord bot token from developer portal 9 | 10 | @client.event 11 | async def on_ready(): 12 | print("Logged in as {0.user}".format(client)) 13 | await client.change_presence(game=discord.Game(name="admin")) 14 | 15 | @client.event 16 | async def on_message(message): 17 | #Check if message contains an IP 18 | regex = re.compile(r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})") 19 | result_list = re.findall(regex, message.content) 20 | 21 | result_list = list(set(result_list)) #Remove duplicates 22 | 23 | #If at least 1 result returned 24 | if result_list and len(result_list) <= 10: 25 | print(result_list) 26 | 27 | for ip in result_list: #For each IP found in the message. Perform lookup 28 | print(ip) 29 | ip_api_response = requests.get(f"http://ip-api.com/json/{ip}") 30 | json_ip_data = json.loads(ip_api_response.text) 31 | 32 | try: 33 | if json_ip_data["status"] == "success": 34 | lat = str(json_ip_data["lat"]) 35 | lon = str(json_ip_data["lon"]) 36 | coords = lat + ", " + lon 37 | 38 | embed = discord.Embed(title="IP Info", description=ip, color=0xcccccc) #Build embed 39 | embed.add_field(name="Country: ", value=json_ip_data["country"], inline=False) 40 | embed.add_field(name="Region/ State: ", value=json_ip_data["regionName"], inline=True) 41 | embed.add_field(name="City: ", value=json_ip_data["city"], inline=True) 42 | embed.add_field(name="ZIP Code: ", value=json_ip_data["zip"], inline=True) 43 | embed.add_field(name="Coordinates: ", value=coords, inline=True) 44 | embed.add_field(name="ISP: ", value=json_ip_data["isp"], inline=False) 45 | 46 | try: #Get temperature 47 | weather_api_response = requests.get(f"http://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&APPID={weather_api_key}&units=metric") 48 | json_weather_data = json.loads(weather_api_response.text) 49 | temp = str(json_weather_data["main"]["temp"]) + "°C" 50 | 51 | embed.add_field(name="Temp: ", value=temp, inline=False) 52 | except: 53 | print("Error retrieving temperature") 54 | 55 | embed.set_footer(text="Powered by ip-api.com and openweathermap.org") 56 | await client.send_message(message.channel, embed=embed) #Send embed 57 | except KeyError: 58 | print("Partial IP result") 59 | except: 60 | print("Error") 61 | 62 | client.run(discord_api_key) #Discord API key 63 | 64 | --------------------------------------------------------------------------------