I am making a discord.py bot that fetches specific json content. But, I get the error “string indices must be integers, not ‘str'”, the speciic line that this error happens is:
games_titles = [info['title'] for info in game_info]
after that line, there’s just
select_options = [
app_commands.OptionChoice(name=title, value=title)
for title in games_titles
]
select = app_commands.SelectMenu(options=select_options)
await interaction.followup.send(
content="Please select a game:",
components=[select]
)
def check(response):
return response.user.id == interaction.user.id and response.component and response.component.custom_id == select.custom_id
response = await bot.wait_for("component", check=check)
selected_game_title = response.values[0]
selected_game_info = next((info for info in game_info['hits'] if info['title'] == selected_game_title), None)
if selected_game_info is None:
await interaction.followup.send("Either the api is dead or ratelimited. Please try again in a few minutes.")
else:
embed = discord.Embed(title=selected_game_info['title'])
icon_url = platform_icons.get(selected_game_info['icon'], 'https://img.icons8.com/fluency/96/ps-controller.png')
embed.set_thumbnail(url=icon_url)
igdb_url = selected_game_info.get('igdb_url', 'None')
site = selected_game_info.get('site', 'None')
link = selected_game_info.get('link', 'No download link available')
embed.add_field(name="IGDB", value=igdb_url, inline=False)
embed.add_field(name="Site", value=site, inline=False)
embed.add_field(name="Download Link", value=link, inline=False)
await interaction.followup.send(embed=embed)
a part of the json request is:
{'hits': [{'id': '78b79803-da00-4208-86cf-14d360352712202405-2900-3810-', 'bios': None, 'core': None, 'icon': 'Switch', 'link': '(link here)', 'playable': 'False', 'system': 'switch,nintendo switch', 'title': '(game name here)', 'igdb_url': 'https://igdb.com/games/no-second-prize', 'site': '(site)'}
what i can see is the id is surrounded by arrays, which could be an issue, but i’m not sure.
traceback is
Traceback:
Traceback (most recent call last):
File "C:Users{MY USER}downloadsnyabot.py", line 54, in search_command
games_titles = [info['title'] for info in game_info]
~~~~^^^^^^^^^
TypeError: string indices must be integers, not 'str'
i tried making an embed with all game info from a json request. shows up a little markdown, you select the game, and it’ll show up the page of that game. expected behaviour should be that it shows all of the info normally into a nice little embed on discord. actual behaviour is that it just sent “An error occurred: string indices must be integers, not ‘str'” and didn’t even get to show anything at all.
joetroll is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.