11

For some reason send_message isn't working properly on my Discord bot and I can't find anyway to fix it.

import asyncio
import discord

client = discord.Client()

@client.async_event
async def on_message(message):
    author = message.author
   if message.content.startswith('!test'):
        print('on_message !test')
        await test(author, message)
async def test(author, message):
    print('in test function')
    await client.send_message(message.channel, 'Hi %s, i heard you.' % author)
client.run("key")
on_message !test
in test function
Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\indit\AppData\Roaming\Python\Python36\site-packages\discord\client.py", line 223, in _run_event
    yield from coro(*args, **kwargs)
  File "bot.py", line 15, in on_message
    await test(author, message)
  File "bot.py", line 21, in test
    await client.send_message(message.channel, 'Hi %s, i heard you.' % author)
AttributeError: 'Client' object has no attribute 'send_message'
2
  • 1
    Are you on async or rewrite branch? Commented Jan 5, 2018 at 16:15
  • After correcting bad indentation and using my own token it worked just fine on my Linux machine. Commented Jan 5, 2018 at 17:59

3 Answers 3

15

You are probably running the rewrite version of discord.py, since the discord.Client object does not a have a send_message method.

To fix your problem you can just have it as:

async def test(author, message):
    await message.channel.send('I heard you! {0.name}'.format(author))

but for what i see you doing I reccomend using the commands extension

This makes creating a bot and commands for the bot much simpler, for example here is some code that does exactly the same as yours

from discord.ext import commands

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

@bot.command()
async def test(ctx):
    await ctx.send('I heard you! {0}'.format(ctx.author))

bot.run('token')
Sign up to request clarification or add additional context in comments.

3 Comments

For some reason if you want to install the rewrite version use this python3 -m pip install -U github.com/Rapptz/discord.py/archive/rewrite.zip
Actually its better to use this command pip install -U git+https://github.com/Rapptz/discord.py@rewrite#egg=discord.py[voice]
Now that rewrite (v1.0.0) has officially been released this version should now be installed through a simple pip install discord.py
1

As per discord.py documentation, migration to v1.0 brought a lot of major changes in the API, including the fact that a lot of functionality was moved out of discord.Client and put into their respective model.

E.g. in your specific case: Client.send_message(abc.Messageable) --> abc.Messageable.send()

So your code would be restyled from this:

await client.send_message(message.channel, 'I heard you! {0}'.format(message.author))

to this:

await message.channel.send('I heard you! {0}'.format(message.author))

This makes the code much more consistent and more accurate in depicting the actual discord entities, i.e. it's intuitive that a discord Message is sent to a Channel (subclass of abc.Messageable) and not to the Client itself.

There is a quite large list of migrations in this sense; you can find them all here under the Models are Stateful section.

Comments

0

The discord.py can't be used for the send_message() function anymore, I think. But this doesn't mean we can not send messages. Another way of sending messages into the channel do exist.

You can use:

await message.channel.send("")

for this purpose.

So let's apply this into your code now.

import asyncio
import discord

client = discord.Client()

@client.async_event
async def on_message(message):
    author = message.author
   if message.content.startswith('!test'):
        print('on_message !test')
        await test(author, message)
async def test(author, message):
    print('in test function')
    await message.channel.send(F'Hi {author}, I heard you.')

client.run("key")

Now this would fix your issue and you'll be able to use your bot to send messages easily.

Thank You! :D

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.