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

使用discord.Embed遇TypeError错误,求问题原因及解决办法

问题解决:Discord机器人!sell命令触发TypeError错误

错误根源

你遇到的TypeError: Embed.__init__() got an unexpected keyword argument 'value',核心问题是**discord.Embed的构造函数不支持value和inline参数**——这两个参数是add_field()方法的专属参数,你错误地把它们放到了Embed初始化语句里。

另外你的代码还有两个隐性问题:

  1. on_message(message)定义末尾缺失冒号:,会触发语法错误
  2. with open("income.json", "r+") as f:块内的代码缩进错误,导致文件读写逻辑不在上下文管理器范围内,可能引发文件操作异常

修正后的完整代码

@client.event
async def on_message(message):  # 补上缺失的冒号
    if "!sell" in message.content:
        tip = random.randrange(1, 500)
        k = f"| {message.author.name} "
        val = random.randrange(1000, 5000)
        # 打开文件进行读写操作
        with open("income.json", "r+") as f:
            incomes = json.load(f)

            # 更新用户余额:存在则累加,不存在则新建条目
            try:
                incomes[k] += val + tip
            except KeyError:
                incomes[k] = val + tip

            # 定位到文件开头并写入更新后的数据
            f.seek(0)
            json.dump(incomes, f, indent=4)
            # 截断文件,避免原文件内容过长残留
            f.truncate()

        # 修正Embed初始化:移除错误参数,保留合法参数
        embedVar = discord.Embed(title=f"{message.author.name} Distributed", color=0xfc3503)
        # 如果需要添加主描述,可使用description参数
        # embedVar = discord.Embed(title=f"{message.author.name} Distributed", description="交易完成通知", color=0xfc3503)
        
        # 正确调用add_field添加字段
        embedVar.add_field(name='﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉', value=f"You were tipped {tip} by the buyer", inline=False)
        embedVar.add_field(name='﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉﹉', value=f"You Earned {val} Dollars from distributing", inline=False)
        
        # 发送Embed到频道(原代码缺失此步骤)
        await message.channel.send(embed=embedVar)

关键修正说明

  • 移除discord.Embed()初始化语句中的value=None和inline=False参数,Embed构造函数的合法参数包括title、description、color等
  • 修复on_message函数定义的语法错误(补充冒号)
  • 修正with代码块的缩进,确保所有文件操作都在上下文管理器的管控范围内
  • 添加f.truncate()语句,避免更新后的内容比原文件短时残留旧数据
  • 补充发送Embed的代码,否则生成的Embed不会展示到Discord频道中

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 04:05:18