我想制造一个对我的世界服务器进行ping操作的discord机器人,并且:如果服务器响应,他将对其进行编辑 将语音通道名称锁定为“服务器状态-在线”。另外,他编辑频道名称:“服务器状态-脱机” 我正在使用mcstatus对服务器执行ping操作! 但是有时候,机器人会出现滞后,我不知道该如何解决。 P.s.对不起,我的英语不好。
from mcstatus import MinecraftServer
import discord
from discord.ext import commands
import time
TOKEN = "token here"
client = discord.Client()
@client.event
async def on_ready():
print("Bot Connected")
await client.wait_until_ready()
channel = client.get_channel(id)
while True:
time.sleep(30)
try:
server = MinecraftServer.lookup("26.51.174.109:25565")
status = server.status()
await channel.edit(name = "Server Status - Online")
except:
await channel.edit(name = "Server Status - Offline")
client.run( TOKEN )
UPD: 我重写了代码,但问题没有丢失。
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 )