无法从 Discord.ext 导入“命令”

问题描述

我的代码

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)

enter image description here

当我导入 discord 时:不访问 discord Playnce(灰显)

我该如何解决这个问题?

解决方法

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(这就是它变灰的原因)

,

您必须使用 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”)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...