AttributeError:“客户端”对象没有属性“ send_message”(Discord Bot) [英] AttributeError: 'Client' object has no attribute 'send_message' (Discord Bot)

查看:101
本文介绍了AttributeError:“客户端”对象没有属性“ send_message”(Discord Bot)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,send_message在我的Discord机器人上无法正常工作,而且我也找不到修复它的方法。

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'


推荐答案

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

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

这篇关于AttributeError:“客户端”对象没有属性“ send_message”(Discord Bot)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆