I’m encountering an issue with my Discord bot where it fails to send messages. The error states that the Client object is missing the send_message method. Below is my code:
import asyncio
import discord
bot = discord.Client()
@bot.event
async def on_message(msg):
user = msg.author
if msg.content.startswith('!test'):
print('test command triggered')
await reply(user, msg)
async def reply(user, msg):
print('processing reply')
await bot.send_message(msg.channel, 'Hey %s, I noticed your message.' % user)
bot.run("your_token")
The traceback I received is:
test command triggered
processing reply
Ignoring exception in on_message
Traceback (most recent call last):
File "discord/client.py", line 223, in _run_event
yield from coro(*args, **kwargs)
File "mybot.py", line 15, in on_message
await reply(user, msg)
File "mybot.py", line 21, in reply
await bot.send_message(msg.channel, 'Hey %s, I noticed your message.' % user)
AttributeError: 'Client' object has no attribute 'send_message'
I’m unsure how to resolve this issue. Any suggestions would be greatly appreciated.
This issue is commonly encountered when using an outdated tutorial for more recent versions of discord.py. The send_message method has been replaced by the send method starting from version 1.0. To resolve your error, replace await bot.send_message(msg.channel, 'Hey %s, I noticed your message.' % user) with await msg.channel.send('Hey %s, I noticed your message.' % user). This should eliminate the AttributeError. I experienced a similar problem after transitioning to the latest version and overlooked the API changes.
That error occurs due to the use of deprecated discord.py syntax. The send_message method was removed in version 1.0 of the library. To fix the issue, change bot.send_message(msg.channel, 'Hey %s, I noticed your message.' % user) to await msg.channel.send('Hey %s, I noticed your message.' % user). The channel object now takes care of sending messages. I’ve faced a similar issue while updating an old bot, so it’s essential to refer to the latest discord.py documentation.
yeah, you’re using old discord.py syntax. send_message got removed ages ago - use msg.channel.send() instead. had the same headache upgrading my bot last year lol