Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
793 views
in Technique[技术] by (71.8m points)

python - AttributeError: 'Client' object has no attribute 'send_message' (Discord Bot)

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:UsersinditAppDataRoamingPythonPython36site-packagesdiscordclient.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'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...