import nextcord
from nextcord.ext import commands
from config import TOKEN, API_KEY
import aiohttp
bot = commands.Bot(command_prefix = "!", intents = nextcord.Intents.all())
@bot.event
async def on_ready():
print("Bot has connected to Discord")
@bot.command()
async def weather(ctx: commands.Context, *, city):
url = "http://api.weatherapi.com/v1/current.json"
params = {
"key": API_KEY,
"q": city
}
async with aiohttp.ClientSession() as session:
async with session.get(url, params=params) as responses:
data = await responses.json()
print(data)
location = data["location"]["name"]["localtime"]
I keep getting the TypeError at this line “location = data[“location”][“name”][“localtime”]”
TypeError: string indices must be integers, not 'str'
In the weather API the information is stored in nested dictionaries. I am accessing those value by using location = data["outer_key"]["inner_key"]
The command i am passing is !weather "city_name"
What am i doing wrong?
New contributor
Aashish Koundinya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1