0

我的代码:

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)

在此处输入图像描述

当我导入不和谐时:不和谐不被访问 Playnce(灰色)

我该如何解决这个问题?

4

2 回答 2

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(这就是为什么它是灰色的)

于 2021-07-18T11:59:41.853 回答
0

您必须使用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”)
于 2021-07-19T21:09:34.493 回答