我正在尝试了解如何从 discord.py 版本 1.7.3 迁移到 2.0。特别是,这是我正在使用的测试代码:
from discord.ext import commands
with open('token.txt', 'r') as f:
TOKEN = f.read()
bot = commands.Bot(command_prefix='$', help_command=None)
@bot.event
async def on_ready():
print('bot is ready')
@bot.command()
async def test1(ctx):
print('test command')
bot.run(TOKEN)
在 discord.py 1.7.3 中,bot 打印“bot is ready”,我可以执行命令 $test1
。
在 discord.py 2.0 中,bot 打印“bot is ready”,但我无法执行命令,并且当我尝试执行命令时控制台中没有任何错误消息。
为什么会发生这种情况,如何在我的机器人中恢复版本 1.7.3 的行为?
最佳答案
在 discord.py 中使用 Intents
启用意图
- 关于 Discord Developer Portal
- 选择您的应用
- 点击“机器人”部分
- 并检查
MESSAGE CONTENT INTENT
将您的意图添加到机器人
让我们添加 message_content现在的意图。
import discord from discord.ext import commands intents = discord.Intents.default() intents.message_content = True bot = commands.Bot(command_prefix='$', intents=intents, help_command=None)
放在一起
代码现在应该是这样的。
import discord from discord.ext import commands with open('token.txt', 'r') as f: TOKEN = f.read() # Intents declaration intents = discord.Intents.default() intents.message_content = True bot = commands.Bot(command_prefix='$', intents=intents, help_command=None) @bot.event async def on_ready(): print('bot is ready') # Make sure you have set the name parameter here @bot.command(name='test1', aliases=['t1']) async def test1(ctx): print('test command') bot.run(TOKEN)
关于python - 命令不在 discord.py 2.0 中运行 - 没有错误,但在 discord.py 1.7.3 中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71553296/