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

执行!en命令触发TypeError: bad operand type for unary +: 'str'错误求助

问题排查:Discord命令执行报错TypeError

问题描述

命令!en L BTC 19000 15000 20000一个月前可正常运行,现在执行触发错误。

报错信息

TypeError: bad operand type for unary +: 'str'

相关代码

async def en(ctx):
    if (ctx.author.id == 573284477158162433 or ctx.author.id == 244240895627624448):
        global channels, message
        x = ctx.message.content
        #command = (''.join(x).split())
        message = ""
        print(len(x))
        if(x[4]=="L"):
            message = "**Type: Long **"+"\n **Ticker: **"+ x[6:9] + "\n  **Entry Price:** " + x[10:15] + "\n **Take Profit: **" + x[16:21]  
            +"\n **Stop Loss: ** " + x[22:len(x)]
            #+ " \n **Comments:** " + x[27:len(x)]
            print(message)
        elif(x[4]=="S"):
            message = "**Type: Short **"+"\n **Ticker: **"+x[6:9] + "\n  **Entry Price:** " + x[10:14] + "\n **Take Profit: **" + x[15:21]
            print(message)

问题原因

  1. 字符串拼接语法错误:在L分支的字符串拼接中,+"\n **Stop Loss: ** " + x[22:len(x)]单独换行且前一行未加续行符\,Python将该行开头的+识别为一元正号运算符,而运算符后是字符串,导致类型不匹配报错。
  2. 参数提取逻辑脆弱:原代码通过固定位置的字符串切片(如x[6:9])获取参数,一旦输入的参数长度、空格数量变化(比如价格位数改变),就会导致切片位置失效,这也是命令突然无法运行的潜在原因。

解决方法

1. 临时修复语法错误

将拼接代码放在同一行,或添加续行符:

# 方案1:同一行完成拼接
message = "**Type: Long **"+"\n **Ticker: **"+ x[6:9] + "\n  **Entry Price:** " + x[10:15] + "\n **Take Profit: **" + x[16:21] +"\n **Stop Loss: ** " + x[22:len(x)]

# 方案2:添加续行符实现多行拼接
message = "**Type: Long **"+"\n **Ticker: **"+ x[6:9] + "\n  **Entry Price:** " + x[10:15] + "\n **Take Profit: **" + x[16:21] \
            +"\n **Stop Loss: ** " + x[22:len(x)]

2. 重构为健壮的参数处理逻辑(推荐)

改用split()分割命令为参数列表,避免依赖固定切片位置,同时用f-string优化字符串格式化:

async def en(ctx):
    if ctx.author.id in (573284477158162433, 244240895627624448):
        global channels, message
        # 分割命令为参数列表,自动处理空格
        args = ctx.message.content.split()
        message = ""
        
        # 检查基础参数是否足够
        if len(args) < 2:
            print("缺少必要参数")
            return
        
        if args[1] == "L":
            # Long命令需要至少6个参数(!en L <Ticker> <Entry> <TakeProfit> <StopLoss>)
            if len(args) < 6:
                print("Long命令参数不足:格式应为!en L <Ticker> <Entry> <TakeProfit> <StopLoss>")
                return
            # 使用f-string格式化,代码更清晰
            message = (
                f"**Type: Long **\n"
                f"**Ticker: **{args[2]}\n"
                f"**Entry Price:** {args[3]}\n"
                f"**Take Profit: **{args[4]}\n"
                f"**Stop Loss: ** {args[5]}"
            )
            print(message)
        elif args[1] == "S":
            # Short命令需要至少5个参数(!en S <Ticker> <Entry> <TakeProfit>)
            if len(args) < 5:
                print("Short命令参数不足:格式应为!en S <Ticker> <Entry> <TakeProfit>")
                return
            message = (
                f"**Type: Short **\n"
                f"**Ticker: **{args[2]}\n"
                f"**Entry Price:** {args[3]}\n"
                f"**Take Profit: **{args[4]}"
            )
            print(message)

修改说明

  • 用split()拆分命令,通过索引获取参数,不再依赖固定字符串位置,适配不同长度的参数值。
  • 增加参数数量检查,避免索引越界报错。
  • 使用f-string格式化字符串,可读性更高,同时用括号包裹多行字符串,避免拼接时的换行问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 19:15:45