Skip to content
Advertisement

ctx.content in command and event difference (discord.py)

In discord.py is there some option how i can get access to msg.content (or ctx.content how everyone uses it) under a command ? Below you can see 2 examples of what i mean. The first one is event and i simply copy the message and let the bot to send it back. The second is command but there the msg.content doesnt work. My problem is that i dont want to use events so much and have everything under a command.

@bot.event  
async def on_message(msg):
    chat = bot.get_channel(797224597443051611)
    if msg.channel.id != channel:
        return
    if msg.content.startswith("!rps"):
        message = str(msg.content)
        await chat.send(message)

Someone types !rps hello. Outpup in discord is !rps hello

@bot.command()
async def rps(msg):
    if msg.channel.id != channel:
        return
    message = str(msg.content)
    await msg.send(message)

Someone types !rps hello (my prefix is !). Output is error in console:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: ‘Context’ object has no attribute ‘content’

Advertisement

Answer

In order to get the rest of the message of a command, you need to pass another argument. This argument will include all the messages that’s sent after !rps. Also, using ctx instead of msg is better in commands.

@bot.command()
async def rps(ctx, *, args):
    if ctx.channel.id != channel:
        return
    await ctx.send(args)

In this code, args argument includes all the messages after the !rps.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement