python - 说,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() 正确输出到控制台,但不幸的是发送到不和谐的聊天室不起作用(
我也试过:
- bot.say(member.name);
- bot.send_message(member.name)
- bot.send(member.name)
但总是发出错误“'Bot'对象没有属性'say'”
请告诉我我做错了什么?
解决方案
您使用的版本discord.py
将改变您发送消息的方式。
discord.py
0.16,“异步”分支,是当前的稳定版本。它有两种发送消息的方式。(在下面,注意它Bot
是 的子类Client
,所以 everyBot
也可以访问所有Client
方法)
使用
Client.send_message(target, message)
. 这就是您将使用的on_message(message)
await bot.send_message(message.channel, "I am responding to your message")
使用
Bot.say(message)
. 这是一种将消息发送回调用命令的通道的简单方法。 它仅 适用于命令。await bot.say("I am responding to your command")
discord.py
1.0,“重写”分支,是最新的分支。它仍然被认为是实验性的,但完全可用。许多变化之一是消息发送的工作方式。
现在,我们不再在客户端上使用方法来发送消息,而是在接收消息的事物上使用方法来向它们发送消息。这些都实现了Messageable
抽象基类,并有一个send
方法。在您的on_message
中,这看起来像
await message.channel.send("I am responding to your message")
我相信您使用的是 1.0 版
推荐阅读
- swift - 使用 UUID 的 Swift 5 CoreData 谓词
- node.js - NestJS:如何处理自定义导向器错误
- python - 如何在python模块中导入图形
- python - 安装 pyspark 后 Jupyter 笔记本无法运行
- node.js - NodeJS + MongoDB:insertOne() - 从 result.ops 中获取插入的文档
- html - 百里香背景图片
- node.js - 如何正确地只允许使用 Mongoose 在 MongoDB 中访问用户帐户的文档
- microsoft-graph-api - 如何使用 API 将 CSV 文件上传到 Onedrive
- azure-data-factory-2 - 为什么 ADF 的复印活动如此缓慢并最终超时
- android - 无法在 react native CLI 中运行 npx react-native init MyApp