I am trying to make a bot while learning discord.py, and I was wondering why this did not work:
def create_embed(self, data):
embed = discord.Embed(title=f"User List Page {self.current_page} / {int(len(self.data) / self.sep) + 1}")
for item in data:
embed.add_field(name=item['label'], value=item[''], inline=False)
return embed
It creates this error:
Traceback (most recent call last):
File "/home/runner/NotifBot/venv/lib/python3.10/site-packages/discord/ext/commands/core.py", line 229, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 113, in paginate
await pagination_view.send(ctx)
File "main.py", line 12, in send
await self.update_message(self.data[:self.sep])
File "main.py", line 23, in update_message
await self.message.edit(embed=self.create_embed(data), view=self)
File "main.py", line 17, in create_embed
embed.add_field(name=item['label'], value=item[''], inline=False)
TypeError: string indices must be integers
You are trying to use a string as an index to access a value from a dictionary or a list.
Replace item[''] with item['value'] or whatever the key is for the value in the item dictionary.
For example, if the item dictionary contains a key-value pair like {‘label’: ‘Username’, ‘value’: ‘JohnDoe’}, you should replace item[‘’] with item[‘value’].
The fixed code should be something like:
def create_embed(self, data):
embed = discord.Embed(title=f"User List Page {self.current_page} / {int(len(self.data) / self.sep) + 1}")
for item in data:
embed.add_field(name=item['label'], value=item['value'], inline=False)
return embed