add remote mc

This commit is contained in:
SiHuan 2020-12-18 21:03:30 +08:00
parent f685397dcd
commit 7e7da55bdb
4 changed files with 128 additions and 31 deletions

3
data/mc/rmc/go.mod Normal file
View file

@ -0,0 +1,3 @@
module rmc
go 1.15

64
data/mc/rmc/main.go Normal file
View file

@ -0,0 +1,64 @@
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"os/exec"
)
func checkErr(err error) {
if err != nil {
fmt.Println(err)
}
}
func handleStatus(writer http.ResponseWriter, request *http.Request) {
cmd := exec.Command("papermc", "status")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
fmt.Fprintf(writer, "Error")
return
}
fmt.Fprintf(writer, out.String())
}
func handleList(writer http.ResponseWriter, request *http.Request) {
cmd := exec.Command("papermc", "command", "list")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
fmt.Fprintf(writer, "Error")
return
}
fmt.Fprintf(writer, out.String())
}
func handleSay(writer http.ResponseWriter, request *http.Request) {
saywhat, _ := ioutil.ReadAll(request.Body)
cmd := exec.Command("papermc", "command", "say", string(saywhat))
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
fmt.Fprintf(writer, "Error")
return
}
fmt.Fprintf(writer, "0")
}
func main() {
http.HandleFunc("/status", handleStatus)
http.HandleFunc("/list", handleList)
http.HandleFunc("/say", handleSay)
fmt.Println("Running at port 58941 ...")
err := http.ListenAndServe("172.26.66.2:58941", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err.Error())
}
}

View file

@ -7,3 +7,5 @@ AUTHORIZATION = ''
HOST = '0.0.0.0' HOST = '0.0.0.0'
PORT = 5580 PORT = 5580
REMOTE_MC_URL = ''

View file

@ -1,7 +1,9 @@
from zzcore import StdAns from zzcore import StdAns
import re import re, requests
from subprocess import getoutput,call from subprocess import getoutput,call
from config import REMOTE_MC_URL
class Ans(StdAns): class Ans(StdAns):
AllowGroup = [959613860, 125733077, 204097403, 1140391080] AllowGroup = [959613860, 125733077, 204097403, 1140391080]
AllowUser = [1318000868] AllowUser = [1318000868]
@ -13,7 +15,23 @@ class Ans(StdAns):
if cmd in AllowCmd: if cmd in AllowCmd:
if cmd == 'status': if cmd == 'status':
output = getoutput('spigot status') msg = getStatus()
elif cmd == 'list':
msg = getList()
elif cmd == 'say':
saywhat = self.raw_msg['message'][8:]
msg = say(saywhat)
else:
msg = '汝是不是在mc后面添加了奇怪的参数咱可只知道 status list 和 say。'
return msg
def getStatus():
if REMOTE_MC_URL:
output = requests.post(f'{REMOTE_MC_URL}/status')
else:
output = getoutput('papermc status')
p = re.compile(r'processes = ([0-9]*) \(') p = re.compile(r'processes = ([0-9]*) \(')
prsnum = re.findall(p,output)[0] prsnum = re.findall(p,output)[0]
p = re.compile(r' \((.*?)\)',re.S) p = re.compile(r' \((.*?)\)',re.S)
@ -24,8 +42,15 @@ class Ans(StdAns):
for prsname in prsnames: for prsname in prsnames:
msg = msg + prsname + ' ' msg = msg + prsname + ' '
msg = msg + '' + prsnum +'个进程,\n一共占用了' + memory +'内存呢。' msg = msg + '' + prsnum +'个进程,\n一共占用了' + memory +'内存呢。'
elif cmd == 'list': return msg
output = getoutput('spigot command list')
def getList():
if REMOTE_MC_URL:
output = requests.post(f'{REMOTE_MC_URL}/list')
else:
output = getoutput('papermc command list')
p = re.compile(r'There are (.*?)[ of a max]', re.S) p = re.compile(r'There are (.*?)[ of a max]', re.S)
online = re.findall(p,output)[0] online = re.findall(p,output)[0]
if online == '0': if online == '0':
@ -36,16 +61,19 @@ class Ans(StdAns):
players = re.findall(p,output)[0].split(', ') players = re.findall(p,output)[0].split(', ')
for player in players: for player in players:
msg = msg + '\n' + player msg = msg + '\n' + player
elif cmd == 'say': return msg
saywhat = self.raw_msg['message'][8:]
def say(saywhat):
if not saywhat: if not saywhat:
return '汝让咱say whato(≧口≦)o' return '汝让咱say whato(≧口≦)o'
shellcmd = ['spigot','command','say',saywhat]
if call(shellcmd) == 0: if REMOTE_MC_URL:
code = requests.post(f'{REMOTE_MC_URL}/say',data=saywhat)
else:
shellcmd = ['papermc','command','say',saywhat]
code = call(shellcmd)
if code == 0:
msg = '咱已经把消息传过去了。' msg = '咱已经把消息传过去了。'
else: else:
msg = '٩(ŏ﹏ŏ、)۶竟然失败了,汝是不是让我发送奇怪的话过去!' msg = '٩(ŏ﹏ŏ、)۶竟然失败了,汝是不是让我发送奇怪的话过去!'
else:
msg = '汝是不是在mc后面添加了奇怪的参数咱可只知道 status list 和 say。'
return msg return msg