我正在使用 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 机器人检查消息是否由机器人本身发送?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何制作一个在 Python 中提供角色的不和谐机器人How to make a discord bot that gives roles in Python?(如何制作一个在 Python 中提供角色的不和谐机器人?)
Discord 机器人没有响应命令Discord bot isn#39;t responding to commands(Discord 机器人没有响应命令)
你能得到“关于我"吗?Discord 机器人的功能?Can you Get the quot;About mequot; feature on Discord bot#39;s? (Discord.py)(你能得到“关于我吗?Discord 机器人的功能?(不和谐.py))
message.channel.id Discord PYmessage.channel.id Discord PY(message.channel.id Discord PY)
如何在 heroku 上托管我的 discord.py 机器人?How do I host my discord.py bot on heroku?(如何在 heroku 上托管我的 discord.py 机器人?)
discord.py - 自动更改角色颜色discord.py - Automaticaly Change an Role Color(discord.py - 自动更改角色颜色)