用于多个游戏服务器的discord.py和UDP侦听器

2024-05-19 14:00:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个UDP套接字,监听来自游戏服务器的数据包,以便进行实时聊天并消除不和谐消息。它工作正常。但当其中一台服务器上正在进行活动游戏时,来自其他服务器的消息也会延迟(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)

Tags: https服务器comapimessageifserverwebhook