matt36
matt36

Reputation: 35

discord bot returns "list index out of range" when trying to dm a member

basically when I run a normal dm command it dms the user correctly every time but the command worked fine before I added the dm part so I have no idea what line the error is referring to. usually it wouldn't give me an error but it wouldn't do anything at all so I add an Exception to find "list index out of range" a thought i had might be in the for field in embed.fields but I'm not sure...

async def tag(ctx, message:int, member: discord.Member, action, *, args):
    try:
        channel = await client.fetch_channel(client.channel)
        message = await channel.fetch_message(message)
        embed = message.embeds[0]
        if member is not None:
            channel1 = member.dm_channel
            if channel1 is None:
                channel1 = await member.create_dm()
            await channel1.send('test')
        if action == "approve":
            embed_dict = embed.to_dict()
            for field in embed_dict["fields"]:
                if field["name"] == "status":
                    field["value"] += f"```python\n{ctx.message.author} tagged this suggestion as **Approved**\nReason:  {args}```"
            embed = discord.Embed.from_dict(embed_dict)
            await message.edit(embed=embed)
        elif action == "deny":
            embed_dict = embed.to_dict()
            for field in embed_dict["fields"]:
                if field["name"] == "status":
                    field["value"] += f"```python\n{ctx.message.author} tagged this suggestion as **Denied**\nReason:  {args}```"
            embed = discord.Embed.from_dict(embed_dict)
            await message.edit(embed=embed)
        else:
            ctx.send('please specify approve or deny after the member argument')
    except Exception as e:
        print(e)

there is my code. it takes an embed and adds a "comment" you could call it to the embed.

Upvotes: 0

Views: 337

Answers (1)

Just for fun
Just for fun

Reputation: 4225

The error is caused by embed = message.embeds[0] this line as this is the only line when you try to index the list

What this shows?: The fetched message has no embeds so message.embeds is an empty list [] so there is no element at index 0 and thus the error is raised.

Make sure to use the perfect Message ID

Upvotes: 1

Related Questions