【问题标题】:TypeError: object NoneType can't be used in 'await' expression discord.pyTypeError:对象 NoneType 不能在“等待”表达式 discord.py 中使用
【发布时间】:2020-12-30 00:41:17
【问题描述】:

尝试在 cog 中创建命令时出现以下错误 TypeError:对象 NoneType 不能在“等待”表达式中使用 使用代码并且功能很简单,当您启动命令时,它会向用户发送一组特定的问题,然后将他们给出的答案返回到启动命令的嵌入中

import discord
from discord.ext import commands



class Profile(commands.Cog):

    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener()
    async def on_ready(self):
        print("Profile Cog has been loaded\n-----")

    # @commands.command(aliases=['pm'])
    # async def dm(self, ctx, user: discord.User, *, message=None):
    #   message = message or "This message is sent via dm"
    #   await user.send(message)
    #   await ctx.message.delete()

    @commands.command()
    async def createprofile(self, ctx, member: discord.Member = None):
        userName = ""
        userAge = ""
        questions = [
            "Please input your name/nickname:",
            "Please input your age:"
        ]
        dmChannel = await ctx.author.send(
            "Yo will be receiving two questions. One involving your name and the other your age.")

        def check(message):
            return message.author == ctx.author and message.channel == dmChannel.channel

        async def askquestion(question):
            await ctx.author.send(question)
            print("Waiting for reply...")
            userReply = await commands.wait_for('message', check=check)
            print("User replied")
            return userReply.content

        userName = await askquestion(questions[0])
        userAge = await askquestion(questions[1])
        e = discord.Embed(title=str(userName) + "'s Profile", description=f"""
        Age: `{str(userAge)}`
        """)
        await ctx.send(embed=e)


def setup(bot):
    bot.add_cog(Profile(bot))
Traceback (most recent call last):
  File "C:\Users\dargo\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\dargo\Desktop\In-Depth\cogs\profile.py", line 42, in createprofile
    userName = await askquestion(questions[0])
  File "C:\Users\dargo\Desktop\In-Depth\cogs\profile.py", line 38, in askquestion
    userReply = await commands.wait_for('message', check=check)
TypeError: object NoneType can't be used in 'await' expression
``

【问题讨论】:

  • 您必须包含 full 堆栈跟踪,包括引发错误的实际代码行。理想情况下,您的代码应该是minimal reproducible example。如果我们无法运行您的代码或查看错误在哪里,您基本上是希望我们盯着您的代码并让错误以某种方式变得明显并跳出我们,我认为大多数人宁愿不这样做他们不必这样做。
  • 我提供了完整的错误,代码是用于 cog 没有其他错误出现

标签: python discord.py-rewrite


【解决方案1】:

不要在 args 中使用 member: discord.Member = None,而是执行以下操作:

member: discord.Member = ""之后在有效代码中写例如:

if member == "":
 #do something
else:
 #do something else

【讨论】:

  • 但问题是,我不需要其他任何东西。它发送问题并等待回复,然后将内容返回到嵌入中。但由于错误,我没有通过第一个问题
  • 是的,我使用了不同的功能,在 try/except/finally 下运行了等待,并且我在不同的 ide 和文本编辑器下运行了代码,在用户尝试回复并给出之前完全没有错误none 类型不能用于 await 函数错误
  • 你试过用self.client.wait_for替换commands.wait_for吗?
  • 非常感谢您确实有效,我不知道为什么 commands.wait_for 不适用并引发错误。
  • 很高兴我为你高兴 :)
猜你喜欢
  • 1970-01-01
  • 2021-09-28
  • 2021-02-26
  • 2021-06-02
  • 2021-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-06
相关资源
最近更新 更多