76 lines
1.2 KiB
Go
76 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func sendMessage(user, message string) {
|
|
_, err := client.SendCommand(fmt.Sprintf("msg %s %s", user, message))
|
|
|
|
if err != nil {
|
|
sendMessage(user, message)
|
|
}
|
|
}
|
|
|
|
func serverMessage(message string) {
|
|
_, err := client.SendCommand("say " + message)
|
|
|
|
if err != nil {
|
|
serverMessage(message)
|
|
}
|
|
}
|
|
|
|
func onlineUsers() (int, int, []string, error) {
|
|
res, err := client.SendCommand("list")
|
|
|
|
if err != nil {
|
|
return -1, -1, nil, err
|
|
}
|
|
|
|
m := userRegexp.FindStringSubmatch(res)
|
|
|
|
if m == nil {
|
|
return -1, -1, nil, errors.New("unexpected response")
|
|
}
|
|
|
|
online, _ := strconv.Atoi(m[1])
|
|
max, _ := strconv.Atoi(m[2])
|
|
names := strings.Split(m[3], ", ")
|
|
|
|
return online, max, names, nil
|
|
}
|
|
|
|
func queryTime() (int, error) {
|
|
res, err := client.SendCommand("time query daytime")
|
|
|
|
if err != nil {
|
|
return -1, err
|
|
}
|
|
|
|
m := timeRegexp.FindStringSubmatch(res)
|
|
|
|
if m == nil {
|
|
return -1, errors.New("no time found")
|
|
}
|
|
|
|
return strconv.Atoi(m[1])
|
|
}
|
|
|
|
func addTime(ticks int) (int, error) {
|
|
res, err := client.SendCommand(fmt.Sprintf("time add %d", ticks))
|
|
|
|
if err != nil {
|
|
return -1, err
|
|
}
|
|
|
|
m := timeRegexp.FindStringSubmatch(res)
|
|
|
|
if m == nil {
|
|
return -1, err
|
|
}
|
|
|
|
return strconv.Atoi(m[1])
|
|
} |