如何在使用 for 循环时不发送垃圾邮件

How to not spam when using a for loop

我正在制作一个音乐机器人,目前我正在尝试添加一个显示队列中所有歌曲的队列命令。

upcoming = list(itertools.islice(player.queue._queue, 0, 9))
counter = 1

for song in upcoming:
    counter = counter + 1
    print(f"{counter}. {song['title']}")
    embed = discord.Embed(description=f"**{counter}**. [{song['title']}]({song['url']})")
    embed.set_thumbnail(url=self.bot.user.avatar_url)
    embed.set_author(name="Playing Next:")
    await ctx.send(embed=embed)

这是我期望的样子:

1. Song 1
2. Song 2
3. Song 3 
4. Song 4
5. Song 5 
6. Song 6
7. Song 7
8. Song 8
9. Song 9

而是在单独的嵌入中发送每一行。

您正在创建一个新的嵌入并为循环的每次迭代发送它。在不了解列表理解的情况下,最简单的解决方案是将所有内容移出 for 循环,除了以下内容:

embed = discord.Embed(description='')

for song in upcoming:
    embed.description += f"**{counter}**. [{song['title']}]({song['url']})\n"

... other embed stuff
... await send embed