python - 当用户发送特定消息时,如何使用 Discord py 通过 webhook 在不和谐上发送消息
问题描述
我正在尝试创建一个模仿该消息指定的用户的基本机器人。
我还不明白如何获取用户 ID,然后是该用户的昵称和个人资料图片。我做了一个演示,只是为了模仿调用机器人命令的用户,但它会导致机器人不断重复消息的无限循环。
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content=='!test':
webhook = Webhook.from_url('webhook url here', adapter=RequestsWebhookAdapter())
a=message.content
webhook.send(content=a, username=message.author.display_name,
avatar_url=message.author.avatar_url,wait=True)
解决方案
很简单,检查消息是否由 webhook 发送。
async def on_message(message):
if message.webhook_id:
return
# other stuff here
参考:
- webhook_id是一个可选整数,普通消息将具有
webhook_id
None。
推荐阅读
- reactjs - 基于上下文动态加载组件的侧面板
- python - PyInstaller 和 OpenGL 错误。ImportError:无法从“PyInstaller.utils.hooks”导入名称“opengl_arrays_modules”
- elasticsearch - Elasticsearch Lucene 查询语法在时间字段中加减 N 分钟?
- python-3.x - Python Tornado 从另一个线程发送 WebSocket 消息
- image - Flutter:如何使用集合内的 Firestore 字段在应用程序中上传图像
- django - 为什么 api 调用在 django 中不通过?
- flutter - Flutter:带有 viewportFraction 的水平列表视图
- reactjs - 仅当在 reactJS 中单击标记时才显示 InfoWindow
- javascript - 如何用数组对象映射数组
- xamarin.ios - Xamarin IOS - UserInfo 自定义数据字典始终为空