我从别人那里看到了这段代码,我想试一试,但它不能很好地工作了,当我的“我的世界”服务器离线时,它会改变频道,但当“我的世界”服务器重新上线时,它不会改变,而是会卡住。有人能帮我解决这个问题吗?机器人是用Python编写的。
问候ItsJeBoyGoogle
另外,对于我糟糕的英文._,我很抱歉。
import discord
import asyncio
import time
import socket
TOKEN = "token"
client = discord.Client()
@client.event
async def on_ready():
print("Bot Connected")
await client.wait_until_ready()
channel = client.get_channel(id)
def ping(ip, port):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip, int(port)))
return True
except:
return False
while True:
online = ping("26.51.174.109", "25565")
if online == True:
print("server online")
await channel.edit(name = "Server Status - Online")
else:
print("server offline")
await channel.edit(name = "Server Status - Offline")
await asyncio.sleep(5)
client.run( TOKEN )发布于 2021-06-01 06:00:54
一些服务器可能会阻塞套接字,因此最好设置超时。关闭套接字也可以。
通常情况下,最好显示异常以查看问题所在。
def ping(ip, port):
try:
s = socket.socket() # standard values are `socket.AF_INET, socket.SOCK_STREAM` so you don't have to write them
s.settimeout(5)
s.connect((ip, int(port)))
s.close()
return True
except Exception as ex:
print('[Exception]', ex)
return False我用于测试的代码
import discord
import asyncio
import time
import socket
import os
TOKEN = os.getenv('DISCORD_TOKEN')
client = discord.Client()
def ping(ip, port):
try:
s = socket.socket() # standard values are `socket.AF_INET, socket.SOCK_STREAM` so you don't have to write them
s.settimeout(2)
s.connect((ip, int(port)))
s.close()
return True
except Exception as ex:
print('[Exception]', ex)
return False
@client.event
async def on_ready():
print("Bot Connected")
await client.wait_until_ready()
#channel = client.get_channel(id)
while True:
online = ping("26.51.174.109", "25565")
#online = ping("192.168.1.101", "8081")
if online:
print("server online")
#await channel.edit(name="Server Status - Online")
else:
print("server offline")
#await channel.edit(name="Server Status - Offline")
await asyncio.sleep(5)
client.run(TOKEN)编辑:
我用nmap - nmap -Pn 26.51.174.109 -p 25565测试了端口,它显示端口被过滤了,所以我认为当它提供timeout时,你可以假设它是online,因为它不拒绝连接。
def ping(ip, port):
try:
s = socket.socket() # TCP - standard values are `socket.AF_INET, socket.SOCK_STREAM` so you don't have to write them
s.settimeout(2)
print('[DEBUG] connect')
s.connect((ip, int(port)))
#result = s.connect_ex((ip, int(port)))
#print('result:', result)
return True
except socket.timeout as ex:
print('[DEBUG] timeout')
return True
except Exception as ex:
print('[Exception]', ex)
return False
finally:
print('[DEBUG] close')
s.close()编辑:
你必须创造价值
id = ...channel number...因为有一个内置的函数id(),它可以使用这个函数作为频道号。
检查
print(id, type(id))你应该看到
<built-in function id> <class 'builtin_function_or_method'>发布于 2021-06-01 21:28:42
我有个自动取款机
import asyncio
import time
import socket
import os
TOKEN = os.getenv('DISCORD_TOKEN')
id = 848949245557473350
client = discord.Client()
def ping(ip, port):
try:
s = socket.socket() # TCP - standard values are `socket.AF_INET, socket.SOCK_STREAM` so you don't have to write them
s.settimeout(2)
print('[DEBUG] connect')
s.connect((ip, int(port)))
#result = s.connect_ex((ip, int(port)))
#print('result:', result)
return True
except socket.timeout as ex:
print('[DEBUG] timeout')
return True
except Exception as ex:
print('[Exception]', ex)
return False
finally:
print('[DEBUG] close')
s.close()
@client.event
async def on_ready():
print("Bot Connected")
await client.wait_until_ready()
channel = client.get_channel(id)
print(id, type(id))
while True:
online = ping("frfr1.quackhost.uk", "20960")
#online = ping("192.168.1.101", "8081")
if online:
print("server online")
await channel.edit(name = "Server Status - ")
else:
print("server offline")
await channel.edit(name = "Server Status - ")
await asyncio.sleep(5)
client.run("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")https://stackoverflow.com/questions/67779870
复制相似问题