python - 多个游戏服务器的 discord.py 和 UDP 侦听器
问题描述
我有这个 UDP 套接字侦听来自游戏服务器的数据包,以便在 Discord 上进行实时聊天和终止消息。它工作正常。但是当其中一个服务器上正在进行活动时,来自其他服务器的消息也会延迟(discord.py 处理速率限制)。我知道我可以轻松地为每台服务器创建一个单独的脚本和一个侦听器,因此一台服务器的 webhook 速率限制不会影响另一台服务器。但问题是我可以在这个脚本中完成它并让它监听这个 UDP 端口吗?
#!/usr/bin/python3
from discord import Webhook, RequestsWebhookAdapter
import socket
UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
UDPServerSocket.bind(("0.0.0.0", 10444))
while(True):
bytesAddressPair = UDPServerSocket.recvfrom(1024)
message = bytesAddressPair[0]
message = message.decode("utf-8", errors="ignore")
server = message.split(';', 1)
server = server[0]
if server == "eu1":
webhookurl = "https://discordapp.com/api/webhooks/redacted"
elif server == "eu2":
webhookurl = "https://discordapp.com/api/webhooks/redacted"
elif server == "us":
webhookurl = "https://discordapp.com/api/webhooks/redacted"
elif server == "brasil":
webhookurl = "https://discordapp.com/api/webhooks/redacted"
elif server == "chile":
webhookurl = "https://discordapp.com/api/webhooks/redacted"
else:
webhookurl = ""
if webhookurl != "":
message = message.partition(";")[2]
if message != "":
webhook = Webhook.from_url(webhookurl, adapter=RequestsWebhookAdapter())
webhook.send(message)
解决方案
推荐阅读
- python - 如何计算 str.replace 执行的操作数?
- google-cloud-platform - 为什么 Cloudproc Juyterhub 经常断开连接?
- arrays - 将数组类型输出为 vhdl 中的端口时无法推断寄存器错误
- ios - 如何使启动屏幕仅纵向不支持其他方向
- django - 如何在 Wagtail 中对发布执行操作?
- python - 计算列表列表中字符串的出现次数
- java - 我可以按键设置@Cacheable 的 TTL 吗?
- excel - 根据另一列的值从相邻列中获取立即值
- javascript - Cheerio 没有正确解析 HTML
- java - 从 Spring 表单中获取 Post 请求中的 null Set 值