为何‘import discord’语句中的discord被下划线标注?
解决Discord机器人import discord下划线问题
给你几个最实用的解决步骤,按顺序来就行:
先确认安装了discord.py库
打开电脑终端(Windows用命令提示符/PowerShell,Mac/Linux直接开终端),输入命令安装:pip install discord.py
如果系统里Python3和Python2共存,换成pip3 install discord.py。要是之前装过,也可以更新到最新版:pip install --upgrade discord.py检查编辑器的Python环境
像VS Code这类编辑器,有时候会自动选到没装discord.py的Python环境。比如你用了虚拟环境,得确保编辑器用的是你装了库的那个环境。以VS Code为例,左下角能看到当前的Python解释器版本,点一下切换到正确的环境就行。验证安装是否有效
打开终端输入python(或python3)进入交互模式,输入import discord,如果没报错,说明安装没问题,那就是编辑器没识别到,重启编辑器就能消掉下划线。
另外提醒下:你代码里直接把机器人token写在代码里很不安全,以后最好用环境变量或者配置文件存token,别直接暴露出来。
你的代码:
import discord client = discord.Client() @client.event async def on_ready(): print('We have logged in as {0.user}'.format(client)) @client.event async def on_message(message): if message.author == client.user: return if message.content.startswith('/hello'): await message.channel.send('YOO THAT IS SO FIRE') if message.content.startswith('/die'): await message.channel.send('bro I think you shouldnt do that') client.run('建议替换为安全存储的token内容')
内容的提问来源于stack exchange,提问作者Chromablade97
相关产品推荐
相关产品推荐

