用python编写一个用我的世界rcon检测到背包为指定物品时触发指定命令的程序(原创)

用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)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值