【问题标题】:NameError: name 'discord' is not defined [duplicate]NameError:名称“不和谐”未定义[重复]
【发布时间】:2021-04-12 17:14:39
【问题描述】:

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')

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

【问题讨论】:

  • Pycharm 应该已经警告过你该行并提供了解决方案。
  • @Selcuk 它确实警告了我,这是我发布的,但没有提供任何解决方案。
  • 这不是 Pycharm 警告,而是 Python 错误。警告直接显示在源代码上。单击带红色下划线的单词discord,然后单击感叹号。它会为您提供导入该名称。

标签: python pycharm discord discord.py


【解决方案1】:

是的,您必须导入 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"))

【讨论】:

  • 如果可行,请点赞并接受!
  • 它没有给出错误,但自定义状态不起作用,所以很糟糕。 ://
  • 您创建的状态不正确。我在上面编辑了我的帖子,看看是否有帮助。
  • 谢谢,你也可以投票吗? (抱歉问)
猜你喜欢
  • 2018-08-01
  • 2020-06-17
  • 2016-07-06
  • 2015-10-22
  • 2016-05-12
  • 2011-11-19
  • 1970-01-01
  • 1970-01-01
  • 2018-02-04
相关资源
最近更新 更多