python - 命令不在 discord.py 2.0 中运行 - 没有错误,但在 discord.py 1.7.3 中运行

标签 python discord discord.py

我正在尝试了解如何从 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

  1. 启用意图

    Help_Image_Intents_Message_Content


  1. 将您的意图添加到机器人

    让我们添加 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)
    

  1. 放在一起

    代码现在应该是这样的。

    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/

相关文章:

python - 如何在 python 中通过 flask 发送双端队列集合

python - 为什么 python 2.6 不更新我的 numpy 数组?

javascript - 使用discord.js 编辑消息不起作用

python - 获取公会和成员图标discord.py

python - 从 discord 使用 FFmpegPCMAudio 的问题

python - 如何在我的 Discord 服务器中获取消息作者的时区? (不和谐.py)

php - 可扩展的异步图像大小调整

python - 在远程服务器上持续运行和重启 python 脚本

python - Nextcord:无法访问服务器上的 ffmpeg

python-3.x - Python asyncio - 循环退出,任务已被销毁,但正在挂起