Вопрос-Ответ

Commands don't run in discord.py 2.0 - no errors, but run in discord.py 1.7.3

Команды не выполняются в версии discord.py 2.0 - ошибок нет, но выполняются в версии discord.py 1.7.3

Я пытаюсь понять, как работает миграция с 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

Используйте Intents с discord.py


  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)


2023-10-14 09:45 python