Sammy
Sammy

Reputation: 57

How to return message.content as a string in discord.py

I'm trying to build a simple bot with discord.py that stores the message author and content to a variable and then prints the variable to a shell. It's returning that there is an unsupported operand type, meaning it returned a different type than a string, I have tried adding the str value in front of that but the same error comes up.

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix='+')

@bot.event
async def on_message(message):
                botMessage = (message.author, 'just said', message.content)
                print(botMessage)

Upvotes: 2

Views: 4195

Answers (2)

Edikku
Edikku

Reputation: 13

    botMessage = (str(message.author), ' just said ', str(message.content))

This should work. Essentially just applying the STR modifier to convert it to callable characters instead of calling and storing another variable.

Upvotes: 1

Thunder
Thunder

Reputation: 67

It seems the issue is the message.author variable. That object is a member type, not a string type. If you want the author's name, just append .name, like message.author.name. Then you should be fine.

Usually, casting to str works fine. But in this case, I think it was throwing an unsupported operand error because Python didn't know how to put 1 discord.py member object and 2 strings together in one variable. If you cast your botMessage object earlier, like botMessage = str(message.author, 'just said', message.content), it'll return a Python string, with the caveat that you'll see the message.author's object type as part of the string, and not the name.

Upvotes: 0

Related Questions