如何修复我的代码,以便我可以获取输入命令的用户 ID?

时间:2020-12-24 20:15:05

标签: python mongodb discord discord.py

嗨,我目前正在尝试使用 MongoDB 在线服务为我的 Discord Bot 制作一个带有经济系统的机器人,但是我似乎无法使用 !balance 或 !work 命令使我的代码正常工作不断出现以下错误:

错误:

2020-12-24T20:04:16.360587+00:00 app[worker.1]: Ignoring exception in command balance:
2020-12-24T20:04:16.362250+00:00 app[worker.1]: Traceback (most recent call last):
2020-12-24T20:04:16.362335+00:00 app[worker.1]: File "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", line 85, in wrapped
2020-12-24T20:04:16.362336+00:00 app[worker.1]: ret = await coro(*args, **kwargs)
2020-12-24T20:04:16.362369+00:00 app[worker.1]: File "bot.py", line 43, in balance
2020-12-24T20:04:16.362369+00:00 app[worker.1]: await open_account(ctx.author)
2020-12-24T20:04:16.362445+00:00 app[worker.1]: File "bot.py", line 79, in open_account
2020-12-24T20:04:16.362447+00:00 app[worker.1]: discorduserid = ctx.author
2020-12-24T20:04:16.362498+00:00 app[worker.1]: AttributeError: 'Member' object has no attribute 'author'

代码:

@bot.command()
async def balance(ctx):
    await open_account(ctx.author)
    discorduserid = str(member.id)

    results = collection.find({"userid":discorduserid})

    for result in results:
        wallet_amt = result["points"]

    em = discord.Embed(title = f"{ctx.author.name}'s balance", colour = discord.Colour.red())
    em.add_field(name = "Wallet", value = wallet_amt)
    await ctx.send(embed = em)

@bot.command()
@commands.cooldown(1, 43200, commands.BucketType.user)
async def work(ctx):
    await open_account(ctx.author)

    discorduserid = ctx.message.author

    earnings = random.randrange(500)

    await ctx.send(f"You went to work and earned {earnings} coins!")

    results = collection.update_many({"userid":discorduserid}, {"$inc":{"points":earnings}})

@work.error
async def work_error(ctx, error):
    if isinstance(error, commands.CommandOnCooldown):
        timerem = str(timedelta(error.retry_after/28800)).split(".")[0]
        msg1 = 'This command is on cooldown, please try again in '
        msg2 = msg1 + timerem
        await ctx.send(msg2)
    else:
        raise error

async def open_account(ctx):
    discorduserid = ctx.author

    if collection.find({"userid":discorduserid}):
        return False
    else:
        newentry = {"userid":discorduserid, "points":0}
<块引用>

2020-12-25T01:19:42.580307+00:00 应用程序[worker.1]:无 2020-12-25T01:19:42.580471+00:00 app[worker.1]:忽略异常 on_message 2020-12-25T01:19:42.582406+00:00 应用程序[worker.1]:回溯 (最近一次通话):2020-12-25T01:19:42.582440+00:00 应用程序[worker.1]:文件 "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", 第 85 行,包裹在 2020-12-25T01:19:42.582440+00:00 app[worker.1] 中: ret = 等待 coro(*args, **kwargs) 2020-12-25T01:19:42.582464+00:00 app[worker.1]: 文件“bot.py”,第 57 行,工作中 2020-12-25T01:19:42.582464+00:00 应用程序[worker.1]:等待 open_account(ctx.author) 2020-12-25T01:19:42.582496+00:00 app[worker.1]:文件“bot.py”,第 91 行,在 open_account 2020-12-25T01:19:42.582497+00:00 应用程序[worker.1]: collection.insert_one(new_entry) 2020-12-25T01:19:42.582552+00:00 app[worker.1]: NameError: name 'new_entry' 未定义 2020-12-25T01:19:42.582599+00:00 应用程序[worker.1]: 2020-12-25T01:19:42.582600+00:00 app[worker.1]:上述异常 是以下异常的直接原因: 2020-12-25T01:19:42.582600+00:00 应用程序[worker.1]: 2020-12-25T01:19:42.582639+00:00 app[worker.1]:回溯(最近 最后调用):2020-12-25T01:19:42.582769+00:00 app[worker.1]:文件 "/app/.heroku/python/lib/python3.6/site-packages/discord/client.py", 第 333 行,在 _run_event 2020-12-25T01:19:42.582771+00:00 app[worker.1]: 等待 coro(*args, **kwargs) 2020-12-25T01:19:42.582817+00:00 应用程序[worker.1]:文件 "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/bot.py", 第 943 行,在 on_message 2020-12-25T01:19:42.582817+00:00 app[worker.1]: 等待 self.process_commands(message) 2020-12-25T01:19:42.582855+00:00 应用程序[worker.1]:文件 "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/bot.py", 第 940 行,在 process_commands 2020-12-25T01:19:42.582856+00:00 app[worker.1]: 等待 self.invoke(ctx) 2020-12-25T01:19:42.582897+00:00 应用程序[worker.1]:文件 "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/bot.py", 第 907 行,在调用 2020-12-25T01:19:42.582897+00:00 app[worker.1] 中: 等待 ctx.command.dispatch_error(ctx, exc) 2020-12-25T01:19:42.582932+00:00 应用程序[worker.1]:文件 "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", 第 424 行,在 dispatch_error 2020-12-25T01:19:42.582932+00:00 应用程序[worker.1]:等待注入(ctx,错误) 2020-12-25T01:19:42.582936+00:00 应用程序[worker.1]:文件 "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", 第 71 行,包裹在 2020-12-25T01:19:42.582937+00:00 app[worker.1]: ret = 等待 coro(*args, **kwargs) 2020-12-25T01:19:42.582976+00:00 app[worker.1]: 文件“bot.py”,第 79 行,在 work_error 中 2020-12-25T01:19:42.582977+00:00 应用程序[worker.1]:引发错误 2020-12-25T01:19:42.583009+00:00 应用程序[worker.1]:文件 "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/bot.py", 第 903 行,在调用 2020-12-25T01:19:42.583010+00:00 app[worker.1] 中: 等待 ctx.command.invoke(ctx) 2020-12-25T01:19:42.583047+00:00 应用程序[worker.1]:文件 "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", 第 859 行,在调用 2020-12-25T01:19:42.583047+00:00 app[worker.1] 中: 等待注入(*ctx.args,**ctx.kwargs) 2020-12-25T01:19:42.583076+00:00 应用程序[worker.1]:文件 "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", 第 94 行,包裹在 2020-12-25T01:19:42.583077+00:00 app[worker.1]: 从 exc 引发 CommandInvokeError(exc) 2020-12-25T01:19:42.583137+00:00 应用程序[worker.1]: discord.ext.commands.errors.CommandInvokeError:命令引发了 异常:NameError:未定义名称“new_entry” 2020-12-25T01:19:44.629823+00:00 应用程序[worker.1]:无 2020-12-25T01:19:44.630148+00:00 app[worker.1]:忽略异常 命令余额:2020-12-25T01:19:44.630460+00:00 应用程序[worker.1]: 回溯(最近一次通话):2020-12-25T01:19:44.630523+00:00 应用程序[worker.1]:文件 "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", 第 85 行,包裹在 2020-12-25T01:19:44.630524+00:00 app[worker.1]: ret = 等待 coro(*args, **kwargs) 2020-12-25T01:19:44.630556+00:00 app[worker.1]: 文件“bot.py”,第 42 行,平衡 2020-12-25T01:19:44.630557+00:00 应用程序[worker.1]:等待 open_account(ctx.author) 2020-12-25T01:19:44.630560+00:00 app[worker.1]:文件“bot.py”,第 91 行,在 open_account 2020-12-25T01:19:44.630560+00:00 应用程序[worker.1]: collection.insert_one(new_entry) 2020-12-25T01:19:44.630625+00:00 app[worker.1]: NameError: name 'new_entry' 未定义 2020-12-25T01:19:44.630677+00:00 应用程序[worker.1]: 2020-12-25T01:19:44.630678+00:00 app[worker.1]:上述异常 是以下异常的直接原因: 2020-12-25T01:19:44.630678+00:00 应用程序[worker.1]: 2020-12-25T01:19:44.630678+00:00 app[worker.1]: Traceback(最近 最后调用):2020-12-25T01:19:44.630749+00:00 app[worker.1]:文件 "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/bot.py", 第 903 行,在调用 2020-12-25T01:19:44.630749+00:00 app[worker.1] 中: 等待 ctx.command.invoke(ctx) 2020-12-25T01:19:44.630753+00:00 应用程序[worker.1]:文件 "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", 第 859 行,在调用 2020-12-25T01:19:44.630754+00:00 app[worker.1] 中: 等待注入(*ctx.args,**ctx.kwargs) 2020-12-25T01:19:44.630793+00:00 应用程序[worker.1]:文件 "/app/.heroku/python/lib/python3.6/site-packages/discord/ext/commands/core.py", 第 94 行,包裹在 2020-12-25T01:19:44.630794+00:00 app[worker.1]: 从 exc 引发 CommandInvokeError(exc) 2020-12-25T01:19:44.630843+00:00 应用程序[worker.1]: discord.ext.commands.errors.CommandInvokeError:命令引发了 异常:NameError:未定义名称“new_entry”

1 个答案:

答案 0 :(得分:0)

我阅读了你的代码,发现了一些问题,试试我修改过的代码

Optional.ofNullable(someVariables)
          .orElseThrow(
              () ->
                  new CustomException("Some message"));

我希望这会有所帮助。祝您有美好的一天!