python - 我的 discord 代码不起作用!当我运行程序时它只显示错误

这是代码:

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

相似文章

html - CSS 响应式容器到文本

我在编程方面仍然相对缺乏经验,这就是我问这个问题的原因。我有一个带有backgroundimage的部分,其中包含文本;文本也占用了它需要的空间,但不幸的是backgroundimage与文本不匹配,...

随机推荐

最新文章