Discord High-low command



我正在为我的discord bot编写一个高低命令,但它根本不起的作用

当我运行它时,

我得到这个错误:

File "C:UsersSHAMANAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesnextcordapplication_command.py", line 863, in invoke_callback_with_hooks
await self(interaction, *args, **kwargs)
File "c:UsersSHAMANOneDriveCloudy-Maincloudy.py", line 246, in guess
guess = await client.wait_for('message', check=check)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.2032.0_x64__qbz5n2kfra8p0libasynciotasks.py", line 408, in wait_for
return await fut
File "C:UsersSHAMANAppDataLocalPackagesPythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0LocalCachelocal-packagesPython310site-packagesnextcordclient.py", line 535, in dispatch
result = condition(*args)
File "c:UsersSHAMANOneDriveCloudy-Maincloudy.py", line 244, in check
return m.author  == ctx.user and m.channel == ctx.message.channel
AttributeError: 'NoneType' object has no attribute 'channel'
The above exception was the direct cause of the following exception:
nextcord.errors.ApplicationInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'channel'

下面是我的代码

@client.slash_command(name="highlow", description="Guess a random number!")
async def guess(ctx, *, maxrange:int=10):
MAX_GUESSES = 5
number = random.randint(1, maxrange)
await ctx.send(f"The highlow game has started.nPlease guess a number between **1-{maxrange}**!nYou have **{MAX_GUESSES}** guesses.",)
def check(m):
return m.author  == ctx.user and m.channel == ctx.message.channel
for i in range(MAX_GUESSES):
guess = await client.wait_for('message', check=check)
try:
int(guess.content)
if guess.content == str(number):
await ctx.send(f"You guessed correctly! It took you **{i+1} tries.")
elif guess.content >- str(number):
await ctx.send(f"Lower!")
elif guess.content <- str(number):
await ctx.send(f"Higher!")
except:
await ctx.send("That isnt even a number :skull:")
else:
await ctx.send(f"You lost! You have ran out of tries.nYou only get **{MAX_GUESSES} guesses.. Good luck next time!")

任何帮助都将不胜感激。谢谢

首先,我看到你刚刚从discord.py迁移到Nextcord,或者至少从Post#TAGS迁移到Nextcord

如果您想使用斜杠命令,您需要将ctx更改为交互,因此最终代码将如下所示:

@client.slash_command(name="highlow", description="Guess a random number!")
async def guess(interaction: nextcord.Interaction, *, maxrange: int = 10):
MAX_GUESSES = 5
number = random.randint(1, maxrange)
await interaction.send(f"The highlow game has started.nPlease guess a number between **1-{maxrange}**!nYou have **{MAX_GUESSES}** guesses.",)
def check(m):
return m.author  == interaction.user and m.channel == interaction.guild.channel
for i in range(MAX_GUESSES):
guess = await client.wait_for('message', check=check)
try:
int(guess.content)
if guess.content == str(number):
await interaction.send(f"You guessed correctly! It took you **{i+1}** tries.")
elif guess.content >- str(number):
await interaction.send(f"Lower!")
elif guess.content <- str(number):
await interaction.send(f"Higher!")
except:
await interaction.send("That isn't even a number :skull:")
else:
await interaction.send(f"You lost! You have ran out of tries.nYou only get **{MAX_GUESSES} guesses.. Good luck next time!")

最新更新