【问题标题】:Problems when saving an argument to a variable将参数保存到变量时出现问题
【发布时间】:2021-01-08 17:48:54
【问题描述】:

我有一些代码基本上只是从命令中获取 arg 并在 if 语句中将其与一些文本进行比较。我不知道为什么,但是当它尝试将 arg 保存到变量时,我不断收到此错误:

Ignoring exception in command raceinfo:
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\User\Desktop\DiscordMMO\Scripts\Character_Creator.py", line 66, in some_crazy_function_name
    raceInfo = ctx.arg
AttributeError: 'Context' object has no attribute 'arg'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 855, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Context' object has no attribute 'arg'

这是代码的sn-p:

@bot.command(name="raceinfo")
async def some_crazy_function_name(ctx, arg):
        raceInfo = ctx.arg
        if raceInfo == "Human" or "human":

【问题讨论】:

  • 您可以尝试添加您当前拥有的代码吗?这将使您更容易使用它来帮助您。
  • 能否请您添加一段实际的代码?
  • 对不起,忘记添加代码,现在添加它
  • 嗯,这个错误是不言自明的。 ctx 内部没有元素 arg
  • AttributeError: 'Context' object has no attribute 'arg' 似乎对您来说应该是不言自明的,并且对于其他对相关对象一无所知的人来说完全不透明。

标签: python python-3.x discord.py


【解决方案1】:

ctx 中没有名为 arg 的元素。如果要将arg 存储在变量中:raceInfo,只需执行raceInfo = arg。或者,不要将arg 作为参数,而是这样写:

@bot.command(name="raceinfo")
async def some_crazy_function_name(ctx, raceInfo):
    if raceInfo == "Human" or raceInfo == "human":
        print(raceInfo)  # Add whatever code you would like

【讨论】:

  • 谢谢,我想我必须用 ctx 指定它。我是处理参数的新手
  • @Tazgirl 没问题!每个人都从某事开始。
  • 你今年排名前 2%。这还不错。我没那么好。
  • 对不起。我已经看到人们几次在 SO 上迅速获得声誉......
  • 这是否意味着我做错了什么?恐怕我正在做一些违反 SO 政策的事情,因为我现在正在迅速赢得声誉。
猜你喜欢
  • 2021-04-29
  • 2016-02-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
相关资源
最近更新 更多