gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 08:21:39 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com