How can make my Python Discord bot I check if a message was sent by the bot itself?(如何让我的 Python Discord 机器人检查消息是否由机器人本身发送?)
问题描述
我正在使用 Python (v. 3.6.1) 编写一个 Discord 机器人,它检测在一个频道中发送的所有消息并在同一频道中回复它们.但是,机器人会自行回复消息,从而导致无限循环.
I am writing a Discord bot using Python (v. 3.6.1) which detects all messages sent in a channel and replies to them in that same channel. However, the bot replies to messages by itself, causing an infinite loop.
@bot.event
async def on_message(message)
await bot.send_message(message.channel, message.content)```
我该如何解决这个问题?
How would I fix this?
推荐答案
message
类包含有关消息的 author
,您可以使用它来确定是否回复消息.作者
是 会员
对象(或其超类 User
如果频道是私有的),它具有 id
属性,但也支持用户之间的直接逻辑比较.
The message
class contains information on the message's author
, which you can utilize to determine whether or not to respond to the message. author
is a Member
object (or its superclass User
if the channel is private), which has an id
property but also supports direct logical comparisons between users.
例如:
@bot.event
async def on_message(message):
if message.author != bot.user:
await bot.send_message(message.channel, message.content)
应该按需要发挥作用
这篇关于如何让我的 Python Discord 机器人检查消息是否由机器人本身发送?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何让我的 Python Discord 机器人检查消息是否由机器人本身发送?


基础教程推荐
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 筛选NumPy数组 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01