add remote mc

This commit is contained in:
SiHuan 2020-12-18 21:03:30 +08:00
parent a6fba1675e
commit b9b7d96e85
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

@ -6,4 +6,6 @@ AUTHORIZATION = ''
HOST = '0.0.0.0'
PORT = 5580
PORT = 5580
REMOTE_MC_URL = ''

View file

@ -1,7 +1,9 @@
from zzcore import StdAns
import re
import re, requests
from subprocess import getoutput,call
from config import REMOTE_MC_URL
class Ans(StdAns):
AllowGroup = [959613860, 125733077, 204097403, 1140391080]
AllowUser = [1318000868]
@ -13,39 +15,65 @@ class Ans(StdAns):
if cmd in AllowCmd:
if cmd == 'status':
output = getoutput('spigot status')
p = re.compile(r'processes = ([0-9]*) \(')
prsnum = re.findall(p,output)[0]
p = re.compile(r' \((.*?)\)',re.S)
prsnames = re.findall(p,output)[0].split(', ')
p = re.compile(r'Total memory usage = (.*)$')
memory = re.findall(p,output)[0]
msg = '咱的MC服务器现在有 '
for prsname in prsnames:
msg = msg + prsname + ' '
msg = msg + '' + prsnum +'个进程,\n一共占用了' + memory +'内存呢。'
msg = getStatus()
elif cmd == 'list':
output = getoutput('spigot command list')
p = re.compile(r'There are (.*?)[ of a max]', re.S)
online = re.findall(p,output)[0]
if online == '0':
msg = '咱看着没有人在线哎\n_(-ω-`_)⌒)_'
else:
msg = '' + online + '个小伙伴在线!'
p = re.compile(r'online: (.*?)[\n>]', re.S)
players = re.findall(p,output)[0].split(', ')
for player in players:
msg = msg + '\n' + player
msg = getList()
elif cmd == 'say':
saywhat = self.raw_msg['message'][8:]
if not saywhat:
return '汝让咱say whato(≧口≦)o'
shellcmd = ['spigot','command','say',saywhat]
if call(shellcmd) == 0:
msg = '咱已经把消息传过去了。'
else:
msg = '٩(ŏ﹏ŏ、)۶竟然失败了,汝是不是让我发送奇怪的话过去!'
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]*) \(')
prsnum = re.findall(p,output)[0]
p = re.compile(r' \((.*?)\)',re.S)
prsnames = re.findall(p,output)[0].split(', ')
p = re.compile(r'Total memory usage = (.*)$')
memory = re.findall(p,output)[0]
msg = '咱的MC服务器现在有 '
for prsname in prsnames:
msg = msg + prsname + ' '
msg = msg + '' + prsnum +'个进程,\n一共占用了' + memory +'内存呢。'
return msg
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)
online = re.findall(p,output)[0]
if online == '0':
msg = '咱看着没有人在线哎\n_(-ω-`_)⌒)_'
else:
msg = '' + online + '个小伙伴在线!'
p = re.compile(r'online: (.*?)[\n>]', re.S)
players = re.findall(p,output)[0].split(', ')
for player in players:
msg = msg + '\n' + player
return msg
def say(saywhat):
if not saywhat:
return '汝让咱say whato(≧口≦)o'
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 = '咱已经把消息传过去了。'
else:
msg = '٩(ŏ﹏ŏ、)۶竟然失败了,汝是不是让我发送奇怪的话过去!'
return msg