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

Discord.py机器人:移除API返回JSON嵌入响应中的引号与括号

解决Discord.py嵌入消息中API返回列表的符号问题

你的问题出在直接将API返回的列表转为字符串(str(rs["otherNames"]['usedNames'])),这会保留Python列表的语法符号(括号、引号)。要解决这个问题,需要把列表元素拼接成干净的文本,具体做法如下:

核心修改思路

API返回的usedNames是一个列表(比如["Player1", "Player2"]),你需要用字符串拼接方法(join)把列表元素转换成无多余符号的文本,而不是直接转成字符串。

修改后的代码

@client.command()
async def show(ctx, player):  # General stats
    rs = requests.get(f"{apiLink}/checkban?name={player}")
    if rs.status_code == 200:  # HTTP OK
        rs_data = rs.json()
        used_names = rs_data["otherNames"]["usedNames"]
        
        # 把列表元素用换行拼接,也可以换成", "用逗号分隔
        names_text = "\n".join(used_names) if used_names else "暂无其他曾用名"
        
        embed = discord.Embed(
            title=f"用户 {player} 的其他曾用名",
            description=f"该用户还使用过以下名称:\n{names_text}"
        )
        await ctx.send(embed=embed)

关键说明

  • 使用f-string替代传统字符串拼接,代码更简洁易读
  • "\n".join(used_names)会把列表里的每个名字单独占一行,完全去掉括号和引号
  • 增加了空列表的判断,避免返回空内容,提升用户体验

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 06:54:47