1

我目前正在创建游戏。

在这里找到:https ://docs.google.com/document/d/1zxC9Mo9LtcMf3jXA3z4xdz7Lj3G3EVGA4_CQzjoeyOA/edit?usp=sharing

我正在努力做到这一点,以便在有人新加入时收到消息。我当前的方法是让站点通过某个端口连接到我的计算机,然后触发以下 python 脚本:

import discord
import asyncio
import websockets
import threading
f = open('runs.txt', 'r')
runs = int(f.readline())
f.close()
def write(input):
    f = open( 'runs.txt', 'w' )
    f.write( str(input) )
    f.close()
username = '<Bot Username>'
password = '<Bot Password>'
client = discord.Client()
client.login(username, password)
chan =  discord.PrivateChannel('<Username>', <Channel ID>)
@asyncio.coroutine
def hello(websocket, path):
    global runs
    input = yield from websocket.recv()
    print(input)
    if input == 'started':
        runs += 1
        write(runs)
        client.send_message(chan, 'User has played Daedalus')
start_server = websockets.serve(hello, '<My public IP>', <Forwarded Port>)


@client.event
def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')


@client.event
def on_message(message):
    if message.content.lower().startswith('check'):
        client.send_message(message.author, 'There are now '+ str(runs) + ' computers that have played daedalus')


def asyncBegin():
    asyncio.get_event_loop().run_until_complete(start_server)
    asyncio.get_event_loop().run_forever()

disco = threading.Thread(target=client.run)
disco.start()

asy = threading.Thread(target=asyncBegin())
asy.start()

然后,该程序使用在线应用程序 Discord 通过我创建的机器人向我发送消息。提到的文本文件只是为了保持运行次数。在游戏中,它被编程为立即使用正确的端口号连接到我的公共 IP,这应该运行程序。

我发现在本地主机上它工作正常,但是一旦我尝试通过网络连接它就不起作用。

当我查看连接状态时,它只是在关闭连接之前尝试连接一下。我在测试时没有任何防火墙或网络安全。什么可能导致它断开连接?

4

0 回答 0