@TheMixRay

Как решить ошибку discord.py?

print('Loading...')
import discord, asyncio, config, os
from discord.ext import commands
from discord.ext.commands import Bot

prefix = config.prefix
bot = commands.Bot(command_prefix = prefix)

@bot.event
async def on_ready():
    os.system('cls')
    print('Complete!\n')

async def on_message(message):
    print('{0.author}\n     {0.content}\n'.format(message))

@bot.command()
async def say(message):
    args = message.content
    await message.channel.send(args.lstrip('{}say'.format(prefix)))

bot.run(Мой токен)

Выдает ошибку:
Ignoring exception in command say:
Traceback (most recent call last):
  File "C:\Users\         \AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "D:\         \         \         \bot.py", line 19, in say
    args = message.content
AttributeError: 'Context' object has no attribute 'content'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\         \AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\         \AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\         \AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Context' object has no attribute 'content'

Как я понял, он говорит, что у message нет content, но в документации пишется другое - https://discordpy.readthedocs.io/en/latest/api.htm...
  • Вопрос задан
  • 804 просмотра
Решения вопроса 1
В команду первым аргументом передаётся не discord.Message, а discord.Context: https://discordpy.readthedocs.io/en/stable/ext/com..."
A command must always have at least one parameter, ctx, which is the Context as the first one.


@bot.command()
async def say(ctx: commands.Context):
    args = ctx.message.content
    await ctx.send(args.lstrip('{}say'.format(prefix)))


К слову, эту команду можно сделать намного
проще:
@bot.command()
async def say(ctx: commands.Context, *, content: str):
    await ctx.send(content)
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы