You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用discord.py实现单条消息多行发送及格式设置?

单条消息发送多行内容的两种方法

方法1:用换行符\n拼接字符串

把所有帮助内容用\n串联起来,一次性传给send()方法就能避免消息被打断。修改后的代码示例:

if message.content.startswith("!help"):
    help_msg = "List of Commands:\n" \
               "!chk *platform* *IGN* | checks rank of given player. (only specify platform if NOT pc.)\n" \
               "!help | shows this list\n" \
               "!updaterole OR !updaterank *IGN* | updates your role to your rank. (only PC users atm...)\n" \
               "Names and commands aren't cAsE sEnSiTivE, so both !CHK and !chk works."
    await message.channel.send(help_msg)

方法2:用Python三重引号多行字符串

这种写法更直观,不需要手动添加\n,直接按换行编辑内容即可:

if message.content.startswith("!help"):
    help_msg = """List of Commands:
!chk *platform* *IGN* | checks rank of given player. (only specify platform if NOT pc.)
!help | shows this list
!updaterole OR !updaterank *IGN* | updates your role to your rank. (only PC users atm...)
Names and commands aren't cAsE sEnSiTivE, so both !CHK and !chk works."""
    await message.channel.send(help_msg)
额外的Discord消息格式技巧

Discord原生支持Markdown语法,结合discord.py可以让消息排版更清晰:

  • 加粗文本:用**内容**包裹,比如**注意:**
  • 斜体文本:用*内容*包裹,和你原来标注参数的写法一致
  • 代码片段:用内容包裹,适合突出命令或参数格式
  • 无序列表:用- 开头,有序列表用1. 开头,比如:
    list_msg = """- !chk: 查询玩家段位
    
  1. 输入命令
  2. 填写平台(非PC时必填)和IGN"""
- 分隔线:用`---`分割不同内容板块

# 进阶:用Embed做更专业的帮助菜单
如果想让帮助信息更美观整洁,推荐使用discord.py的`Embed`类,它支持标题、字段、颜色等自定义选项,排版更规整:
```python
from discord import Embed

if message.content.startswith("!help"):
  embed = Embed(title="Bot Command Help", description="All available bot commands", color=0x00ff00)
  embed.add_field(name="!chk", value="`!chk *platform* *IGN*`\n查询指定玩家段位(非PC玩家需指定平台)", inline=False)
  embed.add_field(name="!help", value="显示此帮助菜单", inline=False)
  embed.add_field(name="!updaterole / !updaterank", value="`!updaterole *IGN*`\n更新你的段位身份组(目前仅支持PC玩家)", inline=False)
  embed.set_footer(text="命令不区分大小写,!CHK和!chk都有效")
  await message.channel.send(embed=embed)

内容的提问来源于stack exchange,提问作者th4t0n3f15h

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 00:40:09