用python编写一个用我的世界rcon检测到背包为指定物品时触发指定命令的程序
实现效果:
首先,安装库:
pip3 install rcon
导入库:
from rcon.source import Client
import time
连接服务器
with Client('你的服务器地址', rcon端口, passwd='rcon密码') as client:
连续获取随机玩家数据
while True:
with Client('server.mcfriends666.com', 25575, passwd='7300') as client:
response = client.run('data get entity @r')
笨办法分割返回
sp = response.split(" has the following entity data:")
背包内包含指定物品的字符串(不好勿喷)
n = ('''[{count: 1, Slot: 9b, id: "minecraft:dirt"}, {count: 2, Slot: 10b, id: "minecraft:dirt"}, {count: 3, Slot: 11b, id: "minecraft:dirt"}, {count: 3, Slot: 12b, id: "minecraft:dirt"}''')
判断返回信息是否包含指定内容(笨)
if n in str(response):
获取玩家名
player = sp[0]
去除玩家名中的空格,以防报错
player.replace(" ","")
执行一系列命令
cmd = "op " + str(player)
client.run(cmd)
time.sleep(0.1) #确保命令执行完毕
cmd = "gamemode creative " + player #不知道为什么不能是 client.run(f'gamemode creative {player}') 会报错。我研究2个小时才试出来
client.run(cmd)
time.sleep(0.1)
cmd = "w " + player + " 你触发条件,操作员!"
client.run(cmd)
完整代码
from rcon.source import Client
import time
while True:
with Client('你的服务器地址', rcon端口, passwd='rcon密码') as client:
response = client.run('data get entity @r')
#print(response)
sp = response.split(" has the following entity data:")
n = ('''[{count: 1, Slot: 9b, id: "minecraft:dirt"}, {count: 2, Slot: 10b, id: "minecraft:dirt"}, {count: 3, Slot: 11b, id: "minecraft:dirt"}, {count: 3, Slot: 12b, id: "minecraft:dirt"}''')
#print(n in str(response))
if n in str(response):
player = sp[0]
player.replace(" ","")
#print(player)
#player = list(player)
cmd = "op " + str(player)
#print(cmd)
client.run(cmd)
time.sleep(1)
cmd = "gamemode creative " + player
#print(client.run(cmd))
time.sleep(1)
cmd = "w " + player + " 你触发条件,操作员!"
maill(player)
client.run(cmd)
#print("db")
time.sleep(0.5)
如果你想加入邮箱通知:
具体过程不再细述,完整代码:
from rcon.source import Client
import time
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# 邮件发送者和接收者
def maill(players):
sender_email = "发件邮箱(qq,不然要改下面标记的位置)"
receiver_email = "收件邮箱(不限)"
# 创建邮件对象和设置邮件内容
message = MIMEMultipart("alternative")
message["Subject"] = "服务器操作员通知!"
message["From"] = sender_email
message["To"] = receiver_email
# 创建邮件正文
text = (f"玩家 {players} 成为服务器操作员通知")
html = """\
<html>
<body>
<p>成为服务器操作员通知</p>
</body>
</html>
"""
# 添加文本和HTML的部分
part1 = MIMEText(text, "plain")
#part2 = MIMEText(html, "html")
# 添加正文到邮件对象中
message.attach(part1)
#message.attach(part2)
# |
# 发送邮件 \/
try:
# 创建SMTP服务器连接 不是qq改这里“smtp.qq.com”改成你用的邮箱smtp服务器!!!!!
with smtplib.SMTP_SSL("smtp.qq.com", 465) as server:
# 登录到邮件服务器
server.login(sender_email, "授权码")#如何获取见https://consumer.huawei.com/cn/support/content/zh-cn15872097/
# 发送邮件
for i in range(1):
server.sendmail(sender_email, receiver_email, message.as_string())
except Exception as e:
print(f"Error: {e}")
else:
print("Email sent successfully!")
while True:
with Client('你的服务器地址', rcon端口, passwd='rcon密码') as client:
response = client.run('data get entity @r')
#print(response)
sp = response.split(" has the following entity data:")
n = ('''[{count: 1, Slot: 9b, id: "minecraft:dirt"}, {count: 2, Slot: 10b, id: "minecraft:dirt"}, {count: 3, Slot: 11b, id: "minecraft:dirt"}, {count: 3, Slot: 12b, id: "minecraft:dirt"}''')
#print(n in str(response))
if n in str(response):
player = sp[0]
player.replace(" ","")
#print(player)
#player = list(player)
cmd = "op " + str(player)
#print(cmd)
client.run(cmd)
time.sleep(1)
cmd = "gamemode creative " + player
#print(client.run(cmd))
time.sleep(1)
cmd = "w " + player + " 你触发条件,操作员!"
maill(player)
client.run(cmd)
#print("db")
time.sleep(0.5)