【发布时间】: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 没有其他错误出现