微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决无法从 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”)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。