首页 > 解决方案 > 多个游戏服务器的 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)

标签: pythondiscorddiscord.pybots

解决方案


推荐阅读