说,send_message 和发送,在@bot 事件中不起作用怎么办?不和谐.py

Posted

技术标签:

【中文标题】说,send_message 和发送,在@bot 事件中不起作用怎么办?不和谐.py【英文标题】:Say, send_message and send, do not work in @bot event what to do? discord.py 【发布时间】:2018-12-02 12:35:26 【问题描述】:

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

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

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() 正确输出到控制台,但是发送到不和谐的聊天室很遗憾不起作用(

我也试过了:

    bot.say(member.name); bot.send_message(member.name) bot.send(member.name)

但始终发出错误“'Bot' object has no attribute 'say'”

请告诉我我做错了什么?

【问题讨论】:

【参考方案1】:

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

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

    使用Client.send_message(target, message)。这就是你 将用于on_message(message)

    await bot.send_message(message.channel, "I am responding to your message")     
    

    使用Bot.say(message)。这是一种简单的发送方式 消息返回到调用命令的通道。It only works in commands.

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

discord.py 1.0,“重写”分支,是最新的分支。它仍然被认为是实验性的,但完全可用。 One of the many changes is the way that message sending works.

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

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

我相信你使用的是 1.0 版

【讨论】:

非常感谢您提供如此详细的回复,我尝试发送不是来自事件 on_message 的消息。而从事件on_member_join我解决了这个问题,原来安装discrod.py的一半库,包括send_message(),都没有安装。决定我制作 virtualenv 并更新所有包。你的回答对我有帮助

以上是关于说,send_message 和发送,在@bot 事件中不起作用怎么办?不和谐.py的主要内容,如果未能解决你的问题,请参考以下文章

为啥 send_message 不存在? [复制]

如何获取用户名 Python

.send() 上的 discord.js 缺少权限错误

Discord BOT python,如何在特定用户编写命令时发送消息

当用户做出反应时,Discord Bot 不发送消息

如何向 Telegram bot API 发送请求?