Files
chatrebot_mcrcon_plug/src/process.py
2025-08-15 15:15:37 +08:00

51 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import threading
from src.modules.plugin_modules import BasePlugin, MessageContext
import time
from rcon import Client
import time
# 服务器IP、端口、RCON密码
def send_message(message,host,port,password):
"""
发送消息到Minecraft服务器
:param message: 要发送的消息列表
"""
try:
with Client(host, passwd = password, port = port) as mcr:
mcr.run('/say {}'.format(message))
except ConnectionRefusedError as e:
print("连接被拒绝请检查服务器是否运行和RCON配置是否正确。错误信息", e)
except TimeoutError:
print("连接超时,请检查服务器网络连接是否正常。")
except Exception as e:
print("发生未知错误:", e)
class Mc_rcon(BasePlugin):
def before_load(self):#加载消息前
config = self.config
gid = config.get("group").get("id")
config = config.get("mc_rcon")
if self.ctx.group is None:
return None
print(self.ctx.group.group_id)
if self.ctx.group.group_id == gid:#转发群消息至 Minecraft
if "CQ:image" in self.ctx.raw_message:
mesg = f"{self.ctx.user.nickname} 发送了一张图片"
print(f"转发消息至 Minecraft: {mesg}")
elif "CQ:video" in self.ctx.raw_message:
mesg = f"{self.ctx.user.nickname} 发送了一段视频"
print(f"转发消息至 Minecraft: {mesg}")
elif "CQ:record" in self.ctx.raw_message:
mesg = f"{self.ctx.user.nickname} 发送了一段语音"
print(f"转发消息至 Minecraft: {mesg}")
elif "CQ:file" in self.ctx.raw_message:
mesg = f"{self.ctx.user.nickname} 发送了一个文件"
print(f"转发消息至 Minecraft: {mesg}")
else:
print(f"转发消息至 Minecraft: {self.ctx.raw_message}")
mesg = f"{self.ctx.user.nickname}{self.ctx.raw_message}"
send_message(message = mesg,host = config.get("host"),port = config.get("port"),password = config.get("password"))