T
T
TheMixRay2020-11-23 20:40:11
Python
TheMixRay, 2020-11-23 20:40:11

How to solve discord.py error?

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(Мой токен)

Gives an error message:
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'

As I understand it, he says that message has no content, but the documentation says something else - https://discordpy.readthedocs.io/en/latest/api.htm...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Nevzorov, 2020-11-23
@TheMixRay

The first argument to the command is not discord.Message, but 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)))

By the way, this command can be done much
easier:
@bot.command()
async def say(ctx: commands.Context, *, content: str):
    await ctx.send(content)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question