命令无法在 discord.py 2.0 中运行 - 没有错误,但可以在 discord.py 1.7.3 中运行
- 2024-12-09 08:30:00
- admin 原创
- 138
问题描述:
我试图了解如何从 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 中,机器人打印“机器人已准备好”,我可以执行命令$test1
。
在 discord.py 2.0 中,机器人打印“机器人已准备就绪”,但我无法执行该命令,并且当我尝试执行该命令时控制台中没有任何错误消息。
为什么会发生这种情况?我该如何在我的机器人中恢复 1.7.3 版本的行为?
解决方案 1:
将 Intent 与 discord.py 结合使用
启用 Intent
* 在Discord 开发者门户上
* 选择您的应用程序
* 点击“机器人”部分
* 并检查`MESSAGE CONTENT INTENT`
向机器人添加你的意图
现在让我们添加message_content Intent。
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)
相关推荐
热门文章
项目管理软件有哪些?
热门标签
云禅道AD