我有下面的脚本,我在其中使用机器人将消息发送到不和谐频道。这个脚本运行良好,这意味着每次我们进入 IF 函数时都会发送消息,但同时在 WHILE 循环中,我会不断收到如下所示的警告和错误。
感谢任何帮助解决警告和错误消息。
import discord
import asyncio
from datetime import datetime
import time
# DISCORD logging in
TOKEN = 'MTEwMjMyODMyMDMxNTYyOTY0OA.Gj3a4Z.AMIJr3OucWmmoeBkGE7P-0fTzH6rKI30kuQhMI'
channel_id = 1102366032385032292
intents = discord.Intents.default()
intents.typing = False
intents.presences = False
client = discord.Client(intents=intents)
@client.event
async def on_ready():
print(f'We have logged in as {client.user}')
await send_message_in_loop()
async def send_message_in_loop():
channel = client.get_channel(channel_id)
while True:
# ==================================================
# DOING SOME STUFF HERE INCDLUGING SOME "time.sleep"
# ==================================================
sec = datetime.utcnow().second
print(sec)
# I want to enter the loop every 60 seconds
if sec==0:
# ==================================================
# DOING SOME STUFF HERE INCDLUGING SOME "time.sleep"
# ==================================================
message='last check: '+str(sec)
task = asyncio.get_event_loop().create_task(channel.send(message))
await asyncio.gather(task)
time.sleep(1)
client.run(TOKEN)
警告信息:
WARNING discord.gateway Shard ID None heartbeat blocked for more than 10 seconds.
Loop thread traceback (most recent call last):
错误信息:
ERROR discord.client Attempting a reconnect in 3.99s
Traceback (most recent call last):
File "C:\Users\lt13485\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\client.py",
line 659, in connect
await self.ws.poll_event()
File "C:\Users\lt13485\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\gateway.py", line 646, in poll_event
raise ConnectionClosed(self.socket, shard_id=self.shard_id, code=code) from None
discord.errors.ConnectionClosed: Shard ID None WebSocket closed with 1000
我正在尝试删除终端中打印机的警告和错误消息。
导入警告
warnings.filterwarnings(“忽略”)
它会抑制所有警告,所以要小心。