我正在使用
discord.py
库开发 Discord 机器人,并遇到了一个问题:打印的控制台输出正确,但 Discord 嵌入中的输出未按预期显示 - 字符串宽度不正确。
这是我的代码的相关部分:
if in_database:
searched_strings: list[str] = ["PLACEHOLDER_MONDAY", "PLACEHOLDER_TUESDAY", "PLACEHOLDER_WEDNESDAY",
"PLACEHOLDER_THURSDAY", "PLACEHOLDER_FRIDAY", "PLACEHOLDER_SATURDAY",
"PLACEHOLDER_SUNDAY"]
days: list[str] = await translate_m(interaction.guild_id, searched_strings)
for row in in_database:
day: str = f"- **{days[row[0] - 1]}**:".ljust(50, ' ')
work_hours += day + f"`{row[1]}`\n"
print(work_hours)
translations[2] = translations[2].replace("[work_hours]", work_hours)
embed: Embed = Embed(description="\n".join(translations[:-1]), color=0xEA2027)
await channel.send(embed=embed)
格式不正确的不和谐嵌入:
所以我的问题是,我该如何解决这个问题?我听说过在不和谐中使用 Codebox 来使用它,但我不喜欢将所有内容都放在 Codebox 中的想法 - 它只是丑陋。
基于这个问题,您可以使用嵌入字段以类似表格的方式格式化营业时间:
day_values = []
hour_values = []
if in_database:
searched_strings: list[str] = ["PLACEHOLDER_MONDAY", "PLACEHOLDER_TUESDAY", "PLACEHOLDER_WEDNESDAY",
"PLACEHOLDER_THURSDAY", "PLACEHOLDER_FRIDAY", "PLACEHOLDER_SATURDAY",
"PLACEHOLDER_SUNDAY"]
days: list[str] = await translate_m(interaction.guild_id, searched_strings)
for row in in_database:
day_values.append(f"**{days[row[0] - 1]}**")
hour_values.append(f"`{row[1]}`")
# I don't know what other stuff you have in `translations` and whether it makes sense
# (from a readability perspective) to simply omit the opening hours in the description
# but add it as fields
embed: Embed = Embed(description="\n".join(translations[:-1]), color=0xEA2027)
embed.add_field(name="Day", value="\n".join(day_values), inline=True)
embed.add_field(name="Time", value="\n".join(hour_values), inline=True)
await channel.send(embed=embed)