Pycord,无法从 discord.ext 导入 "commands"

Pycord, can't import "commands" from discord.ext

我正在尝试向我的 discord 机器人添加斜线命令,但是当我尝试使用 discord.app 添加 Option 时,代码显示 Import "discord" could not be resolved Pylance(reportMissingImports),当我 运行 它说 ImportError: cannot import name 'commands' from 'discord.ext' (unknown location) 这是我的代码:

import discord
from discord.ext import commands
from discord import Option

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

@bot.slash_command()
async def hello(ctx, user:Option(discord.Member, 'User to say hello to', required=True, default=None)):
    await ctx.send(f'Hello {user}!')

我该如何解决这个问题?

编辑:我通过 discord 修复了 vscode 错误,但我仍然没有修复 ImportError

编辑 2:我通过使用 pip install py-cord 而不是 pip install pycord 修复了 ImportError,但是有一个新错误:line 3, in <module> from discord import Option ImportError: cannot import name 'Option' from 'discord'

您必须安装开发人员版本的 pycord (2.0.0) 才能获得 Options 属性。你可以用 pip install git+https://github.com/Pycord-Development/pycord.

安装它

您需要 git 才能执行此操作。

应该是你没有正确安装discord。py/pycord。

您应该尝试重新安装该模块。

对于 discord.py 是:

pip install discord

对于 pycord 是:

pip install pycord