首页 > 解决方案 > NameError:名称“不和谐”未定义

问题描述

VS 代码让我失望了,所以我改用 PyCharm。我正确安装了 Discord.py,并且导入了它。但是尝试为我的机器人创建自定义状态会产生错误:

File "C:\Users\me\Desktop\Projects\TooB\TooB.py", line 8, in on_ready
discord.Activity(name="message", type=0) NameError: name 'discord' is not defined

到目前为止,这是我的全部内容:

from discord.ext import commands

bot = commands.Bot(command_prefix = "toob!")

@bot.event
async def on_ready():
    print("Bot online.")
    discord.Activity(name="message", type=0)




bot.run('TOKEN')

有谁知道如何解决这个问题?

标签: pythonpycharmdiscorddiscord.py

解决方案


是的,您必须导入 Discord 模块。要做到这一点:

import discord

这需要添加到代码的顶部。此外,需要正确设置状态。如何做到这一点:

# Setting `Playing` status
await bot.change_presence(activity=discord.Game(name="a game"))

# Setting `Streaming` status
await bot.change_presence(activity=discord.Streaming(name="My Stream", url=my_twitch_url))

# Setting `Listening` status
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name="a song"))

# Setting `Watching` status
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="a movie"))

推荐阅读