首页 > 解决方案 > 说,send_message 和发送,在@bot 事件中不起作用怎么办?不和谐.py

问题描述

你好重读了discord.py上的所有文档,可惜没找到聊天中的on_member_join事件发送消息这样简单的东西?

我用的是非标准的构造,这样构造client=discord.Client(),但是据我了解new bot=commands.Bot(command_prefix='!')

import discord
from discord.ext import commands

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

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')

@bot.event
async def on_member_join(member):
   print(member.name)   
   bot.send(member.name);

print() 正确输出到控制台,但不幸的是发送到不和谐的聊天室不起作用(

我也试过:

  1. bot.say(member.name);
  2. bot.send_message(member.name)
  3. bot.send(member.name)

但总是发出错误“'Bot'对象没有属性'say'”

请告诉我我做错了什么?

标签: pythondiscorddiscord.py

解决方案


您使用的版本discord.py将改变您发送消息的方式。

discord.py0.16,“异步”分支,是当前的稳定版本。它有两种发送消息的方式。(在下面,注意它Bot是 的子类Client,所以 everyBot也可以访问所有Client方法)

  1. 使用Client.send_message(target, message). 这就是您将使用的on_message(message)

    await bot.send_message(message.channel, "I am responding to your message")     
    
  2. 使用Bot.say(message). 这是一种将消息发送回调用命令的通道的简单方法。 适用于命令。

    await bot.say("I am responding to your command")
    

discord.py1.0,“重写”分支,是最新的分支。它仍然被认为是实验性的,但完全可用。许多变化之一是消息发送的工作方式。

现在,我们不再在客户端上使用方法来发送消息,而是在接收消息的事物上使用方法来向它们发送消息。这些都实现了Messageable抽象基类,并有一个send方法。在您的on_message中,这看起来像

await message.channel.send("I am responding to your message")

我相信您使用的是 1.0 版


推荐阅读