Reputation: 317
I'm trying use the geoip2 library to get the location of an ip address, but I keep getting a Type error.
The code used to work before, not sure what happened.
@client.command(name="iplocation")
async def ip_location(*,ip):
try:
reader = geoip2.database.Reader('GeoLite2-City.mmdb')
response = reader.city(ip)
await client.say("Country: " + response.country.iso_code)
await client.say("City: " + response.subdivisions.most_specific.name)
await client.say("Postal Code: " + response.postal.code)
await client.say("Latitude: " + str(response.location.latitude))
await client.say("Longitude: " + str(response.location.longitude))
reader.close()
except geoip2.errors.AddressNotFoundError:
await client.say("The Address: " + ip + " Is not in the database")
except ValueError:
await client.say(ip + " Does not appear to be an IPv4 or IPv6 address.")
I expect
Country: US City: New York Postal Code: 10453 Latitude: 1.2931 Longitude: 103.8558
This is just an example.
my error:
Traceback (most recent call last):
File "/home/c0deninja/.local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 50, in wrapped
ret = yield from coro(*args, **kwargs)
File "discordbot2.py", line 116, in ip_location
await client.say("City: " + response.subdivisions.most_specific.name)
TypeError: must be str, not NoneType
Upvotes: 1
Views: 9298
Reputation: 622
The data set you are pulling data from must be using None as a type when there is no city set. In this case the error message is a great tip. You'll have to either allow it to use None as a string by doing the conversion you said works, or test for None, and use a blank string in place of an actual city name.
This error is likely to happen in any instance where there is not a string set in the whatever data set you're working with.
File "discordbot2.py", line 116, in ip_location
await client.say("City: " + response.subdivisions.most_specific.name)
TypeError: must be str, not NoneType
Upvotes: 1