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

Discord机器人work命令无响应不向用户钱包加余额且无报错如何解决

问题排查&修复步骤

1. 优先检查Discord权限配置

Discord.py 2.0及以上版本默认禁用普通消息内容读取,前缀命令(如!work)要正常触发必须手动开启对应权限:

  • 打开Discord开发者后台,进入你的机器人应用管理页
  • 找到「Bot」选项卡,下拉到「Privileged Gateway Intents」板块
  • 开启Message Content Intent开关
  • 代码初始化bot时同步添加intent配置:
import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.message_content = True # 必须开启消息内容读取权限
bot = commands.Bot(command_prefix='!', intents=intents) # 替换成你自己设定的命令前缀

2. 修复work命令的文件写入错误

你当前的work命令中保存json的代码没有指定写模式,默认只读模式下执行写入操作会直接抛出异常,你没看到报错大概率是没有开启全局异常捕获:
原错误代码:

with open("bank.json") as f:
    json.dump(users, f, indent=4)

修改为:

with open("bank.json", "w") as f:
    json.dump(users, f, indent=4)

3. 优化代码避免重复逻辑与读写冲突

你已经封装了update_bank函数,不需要在work命令里重复读写json,修改后的代码更简洁,也不会出现多用户同时调用时的文件读写冲突:

@bot.command()
async def work(ctx):
    member = ctx.author
    await open_account(member)
    money = random.randrange(0, 30)
    # 直接调用封装好的余额更新函数,不需要手动操作json文件
    await update_bank(member, money)

    embed=discord.Embed(title="Work", description=f"{ctx.author.name} went to work!", color=0x00FFFF)
    embed.add_field(name="You Earned:", value=f"{money} Ulti Coins", inline=False)
    # Discord.py v2.x 已废弃avatar_url属性,替换为avatar.url避免报错
    embed.set_thumbnail(url=member.avatar.url)
    embed.set_footer(text="Your total amount is saved across all servers that I'm in!")
    await ctx.send(embed=embed)

4. 补充异常捕获排查隐藏问题

如果修改后命令还是无响应,给work命令添加异常捕获打印具体报错信息:

@bot.command()
async def work(ctx):
    try:
        # 把上面修改后的work命令完整逻辑放到这里
    except Exception as e:
        print(f"work命令执行报错:{e}")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.20 08:54:02