Python AttributeError: 'Client' 对象没有属性 'send_message' (Discord Bot)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48116872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:32:50  来源:igfitidea点击:

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

pythonpython-3.xdiscorddiscord.py

提问by cute

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

出于某种原因,send_message 在我的 Discord 机器人上无法正常工作,我无论如何都找不到修复它。

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'

回答by mental

You are probably running the rewrite version of discord.py, since the discord.Clientobject does not a have a send_messagemethod.

您可能正在运行 discord.py 的重写版本,因为该discord.Client对象没有send_message方法。

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