python - 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')
有谁知道如何解决这个问题?
解决方案
是的,您必须导入 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"))
推荐阅读
- javascript - 使用 PhoneGap 将文件下载到应用程序的代码不起作用
- javascript - JavaScript中几个方法异步执行的顺序
- r - `space = "free"` 没有消除 ggplot 刻面中的死空间
- arrays - 将 unicode 代码点数组转换为字符串
- javascript - 冻结没有丑陋循环和固定宽度/左值的动态表列?
- c# - 如何在 .NET 核心中使用 MagickImage 向图像添加填充
- plotly - 如何在 Plotly 中设置非零零线
- python - How to call my_function in main & prompt user for input in main method; my_function accepts user input & concatenates in reverse order (Python 3)
- prolog - Prolog中实数的非线性约束
- php - 为什么 TCPDF 显示?代替 Č 从 word 文件转换文本时