AttributeError:“Client”对象没有“send_message”属性(Discord Bot)

2024-05-18 23:44:20 发布

您现在位置:Python中文网/ 问答频道 /正文

由于某种原因,发送消息在我的不和谐机器人上无法正常工作,我也找不到任何方法来修复它。

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'

Tags: inpytestimportclientsendmessageasync
1条回答
网友
1楼 · 发布于 2024-05-18 23:44:20

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

要解决你的问题,你可以这样做:

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

但对于我看到的你所做的,我建议使用commands extension

这使得为bot创建bot和为bot创建命令更加简单,例如,下面的一些代码与您的代码完全相同

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

相关问题 更多 >

    热门问题