【问题标题】:Cannot import 'commands' from Discord.ext无法从 Discord.ext 导入“命令”
【发布时间】:2021-07-18 11:40:23
【问题描述】:

我的代码:

import discord
from discord import commands
from discord import client

client = commands.Bot(command_prefix="!")

@client.event
async def on_ready():
    print("Ready")

@client.command()
async def ping(ctx):
    await ctx.send("Pong")


client.run("My_Token")

错误输出:

Traceback (most recent call last):
  File "c:\Uers\ Username \Desktop\discordbot\bot.py", line 2, in <module>
    from discord import commands
ImportError: cannot import name 'commands' from 'discord' (C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\__init__.py)

当我导入 discord 时:Discord 无法访问 Playnce(灰显)

我该如何解决这个问题?

【问题讨论】:

  • 您不是从discord.ext 而是从discord 导入命令扩展。

标签: python discord.py pylance


【解决方案1】:

commands 扩展来自discord.ext,而不仅仅是discord

from discord.ext import commands

client = commands.Bot(command_prefix="!")

@client.event
async def on_ready():
    print("Ready")

@client.command()
async def ping(ctx):
    await ctx.send("Pong")

client.run("My_Token")

PS:在这里,您不需要clientdiscord(这就是它变灰的原因)

【讨论】:

  • 如果您的问题已解决,请考虑接受答案 :)
【解决方案2】:

您必须使用discord.ext 来导入commands 扩展。而且我相信您不需要为此代码导入“客户端”

代码:

import discord
from discord.ext import commands

client = commands.Bot(command_prefix=“!”)

@client.event
async def on_ready():
   print(“Ready”)

@client.command()
async def ping(ctx):
   await ctx.send(“Pong!”)

client.run(“My_Token”)

【讨论】:

    猜你喜欢
    • 2022-01-24
    • 2013-06-08
    • 1970-01-01
    • 2014-12-08
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 2015-06-02
    相关资源
    最近更新 更多