python - Discord bot 停止发送消息,但不会引发错误或崩溃
问题描述
我一直在使用 pycharm 在 python 中开发一个不和谐的机器人,它创建一个 webhook,然后使用该 webhook 发送一条消息。它工作得很好,然后停止发送消息。我创建了一个日志来查看它是否甚至没有进入 on_message 事件,但它仍在进入它。我注意到如果我停止机器人更长时间,或者重新启动我的计算机,它通常会再次工作,但我无法找出它发生的原因或合法的解决方案。我将把代码放在下面,任何帮助将不胜感激。
import discord
TOKEN = 'Was not sure if I should put this on stack'
client = discord.Client()
@client.event
async def on_ready():
print('{0.user} is locked and loaded'.format(client))
@client.event
async def on_message(message):
username = str(message.author).split('#')[0]
user_message = str(message.content)
channel = str(message.channel.name)
print(f'{username}: {user_message} ({channel})')
hook = await message.channel.create_webhook(name="HookTest7033")
if message.author == client.user:
return
if user_message.lower() == '!hooktest':
await hook.send("Hello")
await hook.send("Bye")
hooks = await message.channel.webhooks()
for webhook in hooks:
await webhook.delete()
return
if message.author == await client.fetch_user(User ID goes here):
await hook.send("Test response")
hooks = await message.channel.webhooks()
for webhook in hooks:
await webhook.delete()
return
if message.author == await client.fetch_user(User ID goes here):
await hook.send("Test response")
hooks = await message.channel.webhooks()
for webhook in hooks:
await webhook.delete()
return
client.run(TOKEN)
解决方案
推荐阅读
- neo4j - 为什么在 Neo4j 中更新属性时出错
- json - WSO2 EI JSONBuilder 配置
- python - 在python中获取groupBy的最新字符串值
- java - 弹簧靴 | JSONB Postgres | 异常:无法加载类 [jsonb]
- jmeter - http2 插件不适用于摘要报告
- linux - 如何命名具有破折号的挂载单元文件
- android - 每 60*1000 毫秒调用一次 API
- oracle - TypeORM 从 PACKAGE 中的 STORED PROCEDURE 获取 DBMS_OUTPUT.PUT_LINE() 的值
- c# - 无法将 html 代码附加到现有元素中,得到 [object HTMLElement],不是真正的代码
- python - Pygame显示窗口立即打开和关闭