【问题标题】:Error while trying to make eval command [discord python]尝试制作 eval 命令时出错 [discord python]
【发布时间】:2021-02-14 06:39:37
【问题描述】:

我正在尝试创建一个 eval 命令,例如:用户键入“!eval print('foo')”,然后机器人将在此发送给定代码的输出案例:“foo” 这是我的代码:

async def eval(args):
    res = eval(' '.join(args))
    await send(await res)

它给了我这个错误:

File ".py", line 47, in eval
    await message.channel.send(await res)
TypeError: object NoneType can't be used in 'await' expression

有人可以帮忙吗?

【问题讨论】:

  • res 是否包含任何内容?
  • 是的,确实如此,“args”参数是命令名称在每个空格处拆分后出现的所有内容,所以这里应该是“[print('foo')]”,但我测试过查看,当我打印 res 时,它会打印 None
  • 另外,通过声明 res 变量,我在输出中看到“foo”。

标签: python discord discord.py discord.py-rewrite


【解决方案1】:

使用您的实际代码,您正在重新定义内置方法eval。另外,您创建命令的方式是错误的:

from discord.ext import commands

bot = commands.Bot(command_prefix='!')

@bot.command(name='eval')
async def my_command(ctx, *, arg):
    result = eval(arg)
    await ctx.send(arg)

如果您想了解有关创建命令的更多信息,请访问 discord.py 的 commands 扩展名的链接:https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html?highlight=commands

【讨论】:

  • 我之前尝试过使用命令扩展,但它对我不起作用。我现在尝试再次使用它,它仍然不起作用,当用户输入命令时没有任何反应。
  • 您有任何错误信息吗?另外,您的 discord.py 版本是什么(您的 cmd 中的pip show discord.py)?
  • 您应该升级到 discord.py 1.5.0 并允许 Intents,如 here 所述。我不知道它是否能解决问题,但无论如何你都应该这样做:)
  • ive 升级到 1.5.1 并且我有这个:intents = discord.Intents.all(); bot = commands.Bot(command_prefix='v!', intents=intents) 在我的代码的开头。我还在我的 discord bot 应用程序中启用了“Presence Intent”和“Server Members Intent”。还是不行
  • 出了什么问题?
猜你喜欢
  • 1970-01-01
  • 2021-01-31
  • 2021-02-25
  • 2010-09-22
  • 2018-11-05
  • 2019-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多