问题描述
我的代码:
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(灰显)
我该如何解决这个问题?
解决方法
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:在这里,您不需要 client
和 discord
(这就是它变灰的原因)
您必须使用 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”)