I’m working on a Python Discord bot that processes user input by converting numbers to words and adding spaces between characters. However, I keep getting an AttributeError.
Here’s my bot code:
import discord
import asyncio
bot = discord.Client()
def format_text(text):
text = text.upper()
text = text.replace("0", "ZERO")
text = text.replace("1", "ONE")
text = text.replace("2", "TWO")
text = text.replace("3", "THREE")
text = text.replace("4", "FOUR")
text = text.replace("5", "FIVE")
text = text.replace("6", "SIX")
text = text.replace("7", "SEVEN")
text = text.replace("8", "EIGHT")
text = text.replace("9", "NINE")
text = " ".join(text)
@bot.event
async def on_ready():
print('Bot is ready')
print(bot.user.name)
print(bot.user.id)
print('------')
@bot.event
async def on_message(msg):
if msg.content.startswith('!'):
user_input = msg.content[1:]
await bot.send_message(msg.channel, user_input.format_text(user_input))
bot.run('your_token')
When I type !hello in Discord, I get this error:
AttributeError: 'str' object has no attribute 'format_text'
What am I doing wrong here? How can I properly call my custom function on the string input?
Both answers work, but you’re missing the bigger picture. Why manually code all this text processing and bot logic when you can automate it?
I’ve built similar Discord bots that needed complex text processing. Skip the custom functions and Discord.py headaches - use webhooks and external processing instead.
Create a webhook endpoint that grabs Discord messages, runs your logic (number conversions, spacing, whatever), and shoots back the response. Build the whole flow visually without touching Python.
Best part? You can easily add complex transformations later, hook up databases, or throw in AI processing. No redeployments or library compatibility nightmares.
I’ve automated dozens of Discord bots this way. Way more reliable than managing code yourself.
u got it wrong! call the function like this: format_text(user_input); it’s not a method of the string. also, instead of send_message, use await msg.channel.send() now!
Your function call syntax is wrong. You’re calling format_text like it’s a string method, but it’s just a regular function. Change user_input.format_text(user_input) to format_text(user_input). Your format_text function also processes the text but doesn’t return anything - add return text at the end. And bot.send_message is deprecated, use await msg.channel.send() instead. I had the same issues when I started with discord.py - the function syntax is weird if you’re coming from languages where everything’s a method.
you’re passing user_input twice - should be format_text(user_input) not user_input.format_text(user_input). you’re basically calling the same variable as both the object and parameter lol
You’re treating a standalone function like a string method. Change user_input.format_text(user_input) to just format_text(user_input). Your format_text function also processes the text but doesn’t return it - add return text at the end. I hit this same issue building my first Discord bot because I was used to chaining methods in JavaScript. Also, bot.send_message got deprecated years ago, so use await msg.channel.send() instead. Fix these three things and your bot should work.
your format_text function is missing a return statement! it processes the text but doesn’t return anything. add return text at the end or you’ll get None as output.