这是代码:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
@bot.command()
async def sayhi(ctx, name):
await ctx.send(f"Hi {name}!")
bot.run("TOKEN")
这是我得到的错误:
ModuleNotFoundError: No module named 'discord.ext'; 'discord' is not a package
但我已经准备好在 cmd 中执行 pip install discord 命令了!
回答1
在我看来,如果你在 discord.py 中工作,你命名你的文件。此文件名已为您要导入的包保留。如果您现在将工作文件命名为由包保留的文件的名称,它将尝试导入自己,这显然会抛出错误代码。一个简单的解决方案是将文件重命名为 discord.py 以外的其他名称
回答2
尝试这个
bot = commands.Bot(command_prefix="!")
@bot.command()
async def sayhi(ctx, member):
await ctx.send(f"Hi {member.mention}!")
bot.run("TOKEN")
确保您在控制台中执行 pip install discord.py