Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
60d6572331 | ||
|
f0c2083bd7 |
126
.drone.yml
126
.drone.yml
@ -1,126 +0,0 @@
|
||||
kind: pipeline
|
||||
name: build-and-publish
|
||||
|
||||
steps:
|
||||
- name: download modules
|
||||
image: git.vaala.cloud/vaalacat/golang:1.20.0-alpine3.17
|
||||
commands:
|
||||
- sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
|
||||
- apk update --no-cache && apk add --no-cache tzdata git
|
||||
- GOPRIVATE=git.vaala.cloud CGO_ENABLED=0 GOPROXY=https://goproxy.cn,https://proxy.golang.org,direct go mod download
|
||||
- mkdir -p etc
|
||||
- cp /etc/ssl/certs/ca-certificates.crt ./etc/ca-certificates.crt
|
||||
- cp /usr/share/zoneinfo/Asia/Shanghai ./etc/Shanghai
|
||||
volumes:
|
||||
- name: gocache
|
||||
path: /go/pkg/mod
|
||||
- name: build
|
||||
path: /tmp/app
|
||||
when:
|
||||
event:
|
||||
- pull_request
|
||||
- promote
|
||||
- rollback
|
||||
- name: build - amd64
|
||||
image: git.vaala.cloud/vaalacat/golang:1.20.0-alpine3.17
|
||||
commands:
|
||||
- GOOS=linux GOARCH=amd64 GOPRIVATE=git.vaala.cloud CGO_ENABLED=0 GOPROXY=https://goproxy.cn,https://proxy.golang.org,direct go build -ldflags="-s -w" -o mcbot-amd64 main.go
|
||||
volumes:
|
||||
- name: gocache
|
||||
path: /go/pkg/mod
|
||||
- name: build
|
||||
path: /tmp/app
|
||||
depends_on:
|
||||
- download modules
|
||||
when:
|
||||
event:
|
||||
- pull_request
|
||||
- promote
|
||||
- rollback
|
||||
- name: build - arm64
|
||||
image: git.vaala.cloud/vaalacat/golang:1.20.0-alpine3.17
|
||||
commands:
|
||||
- GOOS=linux GOARCH=arm64 GOPRIVATE=git.vaala.cloud CGO_ENABLED=0 GOPROXY=https://goproxy.cn,https://proxy.golang.org,direct go build -ldflags="-s -w" -o mcbot-arm64 main.go
|
||||
volumes:
|
||||
- name: gocache
|
||||
path: /go/pkg/mod
|
||||
- name: build
|
||||
path: /tmp/app
|
||||
depends_on:
|
||||
- download modules
|
||||
when:
|
||||
event:
|
||||
- pull_request
|
||||
- promote
|
||||
- rollback
|
||||
|
||||
- name: publish - amd64
|
||||
image: git.vaala.cloud/vaalacat/drone-docker-buildx:24
|
||||
privileged: true
|
||||
environment:
|
||||
HTTP_PROXY:
|
||||
from_secret: HTTP_PROXY
|
||||
HTTPS_PROXY:
|
||||
from_secret: HTTP_PROXY
|
||||
settings:
|
||||
mirror: https://docker.lab.vaala.tech
|
||||
debug: true
|
||||
platforms:
|
||||
- linux/amd64
|
||||
build_args:
|
||||
- ARCH=amd64
|
||||
repo: git.vaala.cloud/vaalacat/mcbot
|
||||
tags:
|
||||
- amd64
|
||||
registry:
|
||||
from_secret: docker_registry
|
||||
username:
|
||||
from_secret: docker_username
|
||||
password:
|
||||
from_secret: docker_password
|
||||
depends_on:
|
||||
- build - amd64
|
||||
when:
|
||||
event:
|
||||
- promote
|
||||
- rollback
|
||||
target:
|
||||
- production
|
||||
- name: publish - arm64
|
||||
image: git.vaala.cloud/vaalacat/drone-docker-buildx:24
|
||||
privileged: true
|
||||
environment:
|
||||
HTTP_PROXY:
|
||||
from_secret: HTTP_PROXY
|
||||
HTTPS_PROXY:
|
||||
from_secret: HTTP_PROXY
|
||||
settings:
|
||||
mirror: https://docker.lab.vaala.tech
|
||||
debug: true
|
||||
platforms:
|
||||
- linux/arm64
|
||||
build_args:
|
||||
- ARCH=arm64
|
||||
repo: git.vaala.cloud/vaalacat/mcbot
|
||||
tags:
|
||||
- arm64
|
||||
registry:
|
||||
from_secret: docker_registry
|
||||
username:
|
||||
from_secret: docker_username
|
||||
password:
|
||||
from_secret: docker_password
|
||||
depends_on:
|
||||
- build - arm64
|
||||
when:
|
||||
event:
|
||||
- promote
|
||||
- rollback
|
||||
target:
|
||||
- production
|
||||
volumes:
|
||||
- name: build
|
||||
temp: {}
|
||||
- name: gocache
|
||||
host:
|
||||
path: /tmp/drone/mcbot/gocache
|
5
.gitignore
vendored
5
.gitignore
vendored
@ -1,7 +1,2 @@
|
||||
.env
|
||||
run.sh
|
||||
mcbot
|
||||
*.sqlite
|
||||
|
||||
tmp
|
||||
tg-mc.db
|
19
Dockerfile
19
Dockerfile
@ -1,11 +1,12 @@
|
||||
FROM git.vaala.cloud/vaalacat/golang:1.20 AS builder
|
||||
|
||||
# WORKDIR $GOPATH/src/mcbot
|
||||
# COPY . .
|
||||
# RUN mkdir /app && \
|
||||
# CGO_ENABLED=0 GOPROXY=https://goproxy.cn,direct go build -o mcbot main.go && \
|
||||
# cp mcbot /app/
|
||||
|
||||
FROM git.vaala.cloud/vaalacat/alpine
|
||||
|
||||
ARG ARCH
|
||||
|
||||
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories && \
|
||||
apk update --no-cache && apk --no-cache add curl
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY mcbot-${ARCH} /app/mcbot
|
||||
# COPY --from=builder /app/mcbot /app/mcbot
|
||||
COPY mcbot /app/mcbot
|
||||
ENTRYPOINT [ "/app/mcbot" ]
|
18
conf/env.go
18
conf/env.go
@ -1,8 +1,6 @@
|
||||
package conf
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/ilyakaznacheev/cleanenv"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
@ -13,20 +11,7 @@ type botSettings struct {
|
||||
MCServer string `env:"MC_SERVER"`
|
||||
MCBotName string `env:"MC_BOT_NAME"`
|
||||
GroupID int64 `env:"GROUP_ID"`
|
||||
DBPath string `env:"DB_PATH" env-default:"tg-mc.db"`
|
||||
BotAPI string `env:"TG_BOT_API"`
|
||||
AdminID []int64 `env:"ADMIN_ID"`
|
||||
GatewaySettings GatewaySettings `env-prefix:"GATEWAY_"`
|
||||
EnableGateway bool `env:"ENABLE_GATEWAY" env-default:"true"`
|
||||
EnableBridge bool `env:"ENABLE_BRIDGE" env-default:"true"`
|
||||
EnableBot bool `env:"ENABLE_BOT" env-default:"true"`
|
||||
}
|
||||
|
||||
type GatewaySettings struct {
|
||||
ServerHost string `json:"server_host" env:"MC_SERVER_HOST" env-default:"127.0.0.1"`
|
||||
ServerPort int `json:"server_port" env:"MC_SERVER_PORT" env-default:"25566"`
|
||||
ProxyHost string `json:"proxy_host" env:"PROXY_HOST" env-default:"127.0.0.1"`
|
||||
ProxyPort int `json:"proxy_port" env:"PROXY_PORT" env-default:"25565"`
|
||||
DBPath string `env:"DB_PATH"`
|
||||
}
|
||||
|
||||
var (
|
||||
@ -36,7 +21,6 @@ var (
|
||||
func init() {
|
||||
godotenv.Load()
|
||||
cleanenv.ReadEnv(&botSettingsInstance)
|
||||
log.Printf("Bot settings: %+v", botSettingsInstance)
|
||||
}
|
||||
|
||||
func GetBotSettings() *botSettings {
|
||||
|
@ -1,39 +0,0 @@
|
||||
package defs
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type Command struct {
|
||||
Command string `json:"Command"`
|
||||
Argstr string `json:"Argstr"`
|
||||
}
|
||||
|
||||
func (c *Command) ToJSON() string {
|
||||
ans, _ := json.Marshal(c)
|
||||
return string(ans)
|
||||
}
|
||||
|
||||
const (
|
||||
CMD_UNKNOWN = "unknown"
|
||||
CMD_APPROVE = "approve"
|
||||
CMD_REJECT = "reject"
|
||||
)
|
||||
|
||||
func NewApproveCommand(mcName string) *Command {
|
||||
return &Command{
|
||||
Command: CMD_APPROVE,
|
||||
Argstr: mcName,
|
||||
}
|
||||
}
|
||||
|
||||
func NewRejectCommand(mcName string) *Command {
|
||||
return &Command{
|
||||
Command: CMD_REJECT,
|
||||
Argstr: mcName,
|
||||
}
|
||||
}
|
||||
|
||||
func NewCommandFromJSON(jsonStr string) (*Command, error) {
|
||||
var cmd Command
|
||||
err := json.Unmarshal([]byte(jsonStr), &cmd)
|
||||
return &cmd, err
|
||||
}
|
35
go.mod
35
go.mod
@ -3,36 +3,35 @@ module tg-mc
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/Tnze/go-mc v1.20.2-0.20231123224931-bc3d77d78437
|
||||
github.com/glebarez/sqlite v1.9.0
|
||||
github.com/go-co-op/gocron v1.32.1
|
||||
github.com/Tnze/go-mc v1.19.4-pre1
|
||||
github.com/glebarez/sqlite v1.8.0
|
||||
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
|
||||
github.com/ilyakaznacheev/cleanenv v1.5.0
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/samber/lo v1.38.1
|
||||
github.com/sirupsen/logrus v1.9.3
|
||||
gorm.io/gorm v1.25.4
|
||||
github.com/sirupsen/logrus v1.9.1
|
||||
gorm.io/gorm v1.25.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/glebarez/go-sqlite v1.21.2 // indirect
|
||||
github.com/google/uuid v1.3.1 // indirect
|
||||
github.com/glebarez/go-sqlite v1.21.1 // indirect
|
||||
github.com/go-co-op/gocron v1.28.3 // indirect
|
||||
github.com/google/uuid v1.3.0 // indirect
|
||||
github.com/iancoleman/strcase v0.2.0 // indirect
|
||||
github.com/ilyakaznacheev/cleanenv v1.4.2 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||
github.com/mattn/go-isatty v0.0.17 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/robfig/cron/v3 v3.0.1 // indirect
|
||||
go.uber.org/atomic v1.11.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 // indirect
|
||||
golang.org/x/sys v0.11.0 // indirect
|
||||
go.uber.org/atomic v1.9.0 // indirect
|
||||
golang.org/x/sys v0.4.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
modernc.org/libc v1.24.1 // indirect
|
||||
modernc.org/mathutil v1.6.0 // indirect
|
||||
modernc.org/memory v1.7.1 // indirect
|
||||
modernc.org/sqlite v1.25.0 // indirect
|
||||
modernc.org/libc v1.22.3 // indirect
|
||||
modernc.org/mathutil v1.5.0 // indirect
|
||||
modernc.org/memory v1.5.0 // indirect
|
||||
modernc.org/sqlite v1.21.1 // indirect
|
||||
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect
|
||||
)
|
||||
|
||||
// replace github.com/Tnze/go-mc => /home/coder/go-mc
|
||||
replace github.com/Tnze/go-mc => /Users/vaala/Workdir/Code/go-mc
|
||||
|
75
go.sum
75
go.sum
@ -1,93 +1,86 @@
|
||||
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
|
||||
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/Tnze/go-mc v1.20.2-0.20231123224931-bc3d77d78437 h1:cINogPegf6TCIEmAon1kyVuKJttjoM0H7kyIM5WXre4=
|
||||
github.com/Tnze/go-mc v1.20.2-0.20231123224931-bc3d77d78437/go.mod h1:geoRj2HsXSkB3FJBuhr7wCzXegRlzWsVXd7h7jiJ6aQ=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9gAXWo=
|
||||
github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k=
|
||||
github.com/glebarez/sqlite v1.9.0 h1:Aj6bPA12ZEx5GbSF6XADmCkYXlljPNUY+Zf1EQxynXs=
|
||||
github.com/glebarez/sqlite v1.9.0/go.mod h1:YBYCoyupOao60lzp1MVBLEjZfgkq0tdB1voAQ09K9zw=
|
||||
github.com/go-co-op/gocron v1.32.1 h1:h+StA6Qzlv+ImlCaLfA26rLN9eS/l4sO7oWmPUbRVIY=
|
||||
github.com/go-co-op/gocron v1.32.1/go.mod h1:UGz2oYvVS6PsqlwuOdo5L1Djsg/cQjxJ6T5ntkhp9Bg=
|
||||
github.com/glebarez/go-sqlite v1.21.1 h1:7MZyUPh2XTrHS7xNEHQbrhfMZuPSzhkm2A1qgg0y5NY=
|
||||
github.com/glebarez/go-sqlite v1.21.1/go.mod h1:ISs8MF6yk5cL4n/43rSOmVMGJJjHYr7L2MbZZ5Q4E2E=
|
||||
github.com/glebarez/sqlite v1.8.0 h1:02X12E2I/4C1n+v90yTqrjRa8yuo7c3KeHI3FRznCvc=
|
||||
github.com/glebarez/sqlite v1.8.0/go.mod h1:bpET16h1za2KOOMb8+jCp6UBP/iahDpfPQqSaYLTLx8=
|
||||
github.com/go-co-op/gocron v1.28.3 h1:swTsge6u/1Ei51b9VLMz/YTzEzWpbsk5SiR7m5fklTI=
|
||||
github.com/go-co-op/gocron v1.28.3/go.mod h1:39f6KNSGVOU1LO/ZOoZfcSxwlsJDQOKSu8erN0SH48Y=
|
||||
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
|
||||
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
|
||||
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
|
||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
|
||||
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/ilyakaznacheev/cleanenv v1.5.0 h1:0VNZXggJE2OYdXE87bfSSwGxeiGt9moSR2lOrsHHvr4=
|
||||
github.com/ilyakaznacheev/cleanenv v1.5.0/go.mod h1:a5aDzaJrLCQZsazHol1w8InnDcOX0OColm64SlIi6gk=
|
||||
github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0=
|
||||
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
|
||||
github.com/ilyakaznacheev/cleanenv v1.4.2 h1:nRqiriLMAC7tz7GzjzUTBHfzdzw6SQ7XvTagkFqe/zU=
|
||||
github.com/ilyakaznacheev/cleanenv v1.4.2/go.mod h1:i0owW+HDxeGKE0/JPREJOdSCPIyOnmh6C0xhWAkF/xA=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
|
||||
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
|
||||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
|
||||
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
||||
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
||||
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
|
||||
github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg=
|
||||
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
|
||||
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
|
||||
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
|
||||
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/sirupsen/logrus v1.9.1 h1:Ou41VVR3nMWWmTiEUnj0OlsgOSCUFgsPAOl6jRIcVtQ=
|
||||
github.com/sirupsen/logrus v1.9.1/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
|
||||
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
|
||||
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
|
||||
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
|
||||
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 h1:m64FZMko/V45gv0bNmrNYoDEq8U5YUhetc9cBWKS1TQ=
|
||||
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63/go.mod h1:0v4NqG35kSWCMzLaMeX+IQrlSnVE/bqGSyC2cz/9Le8=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
|
||||
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
|
||||
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gorm.io/gorm v1.25.4 h1:iyNd8fNAe8W9dvtlgeRI5zSVZPsq3OpcTu37cYcpCmw=
|
||||
gorm.io/gorm v1.25.4/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
|
||||
modernc.org/libc v1.24.1 h1:uvJSeCKL/AgzBo2yYIPPTy82v21KgGnizcGYfBHaNuM=
|
||||
modernc.org/libc v1.24.1/go.mod h1:FmfO1RLrU3MHJfyi9eYYmZBfi/R+tqZ6+hQ3yQQUkak=
|
||||
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
|
||||
modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo=
|
||||
modernc.org/memory v1.7.1 h1:9J+2/GKTlV503mk3yv8QJ6oEpRCUrRy0ad8TXEPoV8M=
|
||||
modernc.org/memory v1.7.1/go.mod h1:NO4NVCQy0N7ln+T9ngWqOQfi7ley4vpwvARR+Hjw95E=
|
||||
modernc.org/sqlite v1.25.0 h1:AFweiwPNd/b3BoKnBOfFm+Y260guGMF+0UFk0savqeA=
|
||||
modernc.org/sqlite v1.25.0/go.mod h1:FL3pVXie73rg3Rii6V/u5BoHlSoyeZeIgKZEgHARyCU=
|
||||
gorm.io/gorm v1.25.1 h1:nsSALe5Pr+cM3V1qwwQ7rOkw+6UeLrX5O4v3llhHa64=
|
||||
gorm.io/gorm v1.25.1/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
|
||||
modernc.org/libc v1.22.3 h1:D/g6O5ftAfavceqlLOFwaZuA5KYafKwmr30A6iSqoyY=
|
||||
modernc.org/libc v1.22.3/go.mod h1:MQrloYP209xa2zHome2a8HLiLm6k0UT8CoHpV74tOFw=
|
||||
modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ=
|
||||
modernc.org/mathutil v1.5.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
|
||||
modernc.org/memory v1.5.0 h1:N+/8c5rE6EqugZwHii4IFsaJ7MUhoWX07J5tC/iI5Ds=
|
||||
modernc.org/memory v1.5.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU=
|
||||
modernc.org/sqlite v1.21.1 h1:GyDFqNnESLOhwwDRaHGdp2jKLDzpyT/rNLglX3ZkMSU=
|
||||
modernc.org/sqlite v1.21.1/go.mod h1:XwQ0wZPIh1iKb5mkvCJ3szzbhk+tykC8ZWqTRTgYRwI=
|
||||
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 h1:slmdOY3vp8a7KQbHkL+FLbvbkgMqmXojpFUO/jENuqQ=
|
||||
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3/go.mod h1:oVgVk4OWVDi43qWBEyGhXgYxt7+ED4iYNpTngSLX2Iw=
|
||||
|
@ -9,9 +9,9 @@ import (
|
||||
|
||||
type User struct {
|
||||
gorm.Model
|
||||
TGID int64 `gorm:"column:tgid;NOT NULL;index"`
|
||||
MCName string `gorm:"column:mc_name;NOT NULL;unique"`
|
||||
Status int `gorm:"column:status;NOT NULL"` // 0: pending, 1: normal, 2: banned
|
||||
TGID int64 `gorm:"unique"`
|
||||
MCName string
|
||||
Status int // 0: pending, 1: normal, 2: banned
|
||||
}
|
||||
|
||||
func init() {
|
||||
@ -24,13 +24,6 @@ func (u *User) TableName() string {
|
||||
return "users"
|
||||
}
|
||||
|
||||
func GetUsersByTGID(tgID int64) (users []User, err error) {
|
||||
err = database.GetDB().Where(
|
||||
&User{TGID: tgID},
|
||||
).Find(&users).Error
|
||||
return
|
||||
}
|
||||
|
||||
func GetUserByTGID(tgID int64) (user User, err error) {
|
||||
err = database.GetDB().Where(
|
||||
&User{TGID: tgID},
|
||||
|
@ -1,86 +0,0 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/defs"
|
||||
"tg-mc/models"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
)
|
||||
|
||||
type Auth interface {
|
||||
IsAuthed(u models.User) bool
|
||||
RequestAuth(u models.User, req *LoginRequest)
|
||||
Reject(u models.User)
|
||||
SetAuth(u models.User)
|
||||
}
|
||||
|
||||
type Authcator struct {
|
||||
UserMap *sync.Map
|
||||
}
|
||||
|
||||
var authcator *Authcator
|
||||
|
||||
func GetAuthcator() Auth {
|
||||
if authcator == nil {
|
||||
authcator = &Authcator{
|
||||
UserMap: &sync.Map{},
|
||||
}
|
||||
}
|
||||
return authcator
|
||||
}
|
||||
|
||||
func (a *Authcator) getUserLoginReq(u models.User) *LoginRequest {
|
||||
chAny, ok := a.UserMap.Load(u.MCName)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
loginReq, ok := chAny.(*LoginRequest)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
return loginReq
|
||||
}
|
||||
|
||||
func (a *Authcator) IsAuthed(u models.User) bool {
|
||||
loginReq := a.getUserLoginReq(u)
|
||||
if loginReq == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return <-loginReq.Resolve
|
||||
}
|
||||
|
||||
func (a *Authcator) RequestAuth(u models.User, req *LoginRequest) {
|
||||
a.UserMap.Store(u.MCName, req)
|
||||
m := tgbotapi.NewMessage(u.TGID, fmt.Sprintf("MC用户:%v 尝试登录,请选择操作", u.MCName))
|
||||
|
||||
m.ReplyMarkup = tgbotapi.NewInlineKeyboardMarkup(
|
||||
tgbotapi.NewInlineKeyboardRow(
|
||||
tgbotapi.NewInlineKeyboardButtonData("批准", defs.NewApproveCommand(u.MCName).ToJSON()),
|
||||
tgbotapi.NewInlineKeyboardButtonData("拒绝", defs.NewRejectCommand(u.MCName).ToJSON())),
|
||||
)
|
||||
conf.Bot.Send(m)
|
||||
}
|
||||
|
||||
func (a *Authcator) Reject(u models.User) {
|
||||
loginReq := a.getUserLoginReq(u)
|
||||
if loginReq == nil {
|
||||
return
|
||||
}
|
||||
|
||||
loginReq.Resolve <- false
|
||||
}
|
||||
|
||||
func (a *Authcator) SetAuth(u models.User) {
|
||||
loginReq := a.getUserLoginReq(u)
|
||||
if loginReq == nil {
|
||||
return
|
||||
}
|
||||
|
||||
loginReq.Resolve <- true
|
||||
}
|
@ -1,214 +0,0 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/models"
|
||||
"time"
|
||||
|
||||
"github.com/Tnze/go-mc/net"
|
||||
pk "github.com/Tnze/go-mc/net/packet"
|
||||
"github.com/Tnze/go-mc/offline"
|
||||
"github.com/google/uuid"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type LoginRequest struct {
|
||||
ChatID int64
|
||||
Resolve chan bool
|
||||
}
|
||||
|
||||
func requestVerify(mcid string) (bool, error) {
|
||||
|
||||
user, err := models.GetUserByMCName(mcid)
|
||||
if err != nil {
|
||||
return false, errors.New("mcid not found in settings")
|
||||
}
|
||||
|
||||
// Send a message to the user to accept or reject using your bot logic
|
||||
logrus.Infof("Requesting verification for MCID: %s with ChatID: %d\n", mcid, user.TGID)
|
||||
|
||||
// The channel to await a response from the verification process
|
||||
loginRequest := &LoginRequest{
|
||||
ChatID: user.TGID,
|
||||
Resolve: make(chan bool),
|
||||
}
|
||||
|
||||
GetAuthcator().RequestAuth(user, loginRequest)
|
||||
|
||||
select {
|
||||
case result := <-loginRequest.Resolve:
|
||||
return result, nil
|
||||
case <-time.After(10 * time.Second): // Replace with your actual timeout handling logic
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
// This function should be called when a new client connection is accepted
|
||||
func HandleClientConnection(clientConn net.Conn) {
|
||||
settings := conf.GetBotSettings().GatewaySettings
|
||||
|
||||
targetConns, err := net.DialMC(settings.ServerHost + ":" + strconv.Itoa(settings.ServerPort))
|
||||
|
||||
targetConn := *targetConns
|
||||
|
||||
// targetConn, err := gonet.Dial("tcp", settings.ServerHost+":"+strconv.Itoa(settings.ServerPort))
|
||||
if err != nil {
|
||||
logrus.Errorf("Failed to connect to target: %s", err)
|
||||
return
|
||||
}
|
||||
protocol, intention, err := handshake(clientConn, targetConn)
|
||||
if err != nil {
|
||||
logrus.Errorf("Handshake error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
logrus.Infof("Protocol: %v, Intention: %v", protocol, intention)
|
||||
|
||||
switch intention {
|
||||
default: // unknown error
|
||||
logrus.Errorf("Unknown handshake intention: %v", intention)
|
||||
case 1: // for status
|
||||
handleProxyConnection(clientConn, targetConn)
|
||||
case 2: // for login
|
||||
handlePlaying(clientConn, targetConn)
|
||||
}
|
||||
}
|
||||
|
||||
type PlayerInfo struct {
|
||||
Name string
|
||||
UUID uuid.UUID
|
||||
OPLevel int
|
||||
}
|
||||
|
||||
func handlePlaying(conn, target net.Conn) {
|
||||
// login, get player info
|
||||
info, err := acceptLogin(conn, target)
|
||||
if err != nil {
|
||||
logrus.Errorf("user [%s] Login failed", info.Name)
|
||||
return
|
||||
}
|
||||
|
||||
logrus.Infof("Login successful: %s", info.Name)
|
||||
|
||||
// Write LoginSuccess packet
|
||||
|
||||
handleProxyConnection(conn, target)
|
||||
}
|
||||
|
||||
// acceptLogin check player's account
|
||||
func acceptLogin(conn, target net.Conn) (info PlayerInfo, err error) {
|
||||
// login start
|
||||
var p pk.Packet
|
||||
err = conn.ReadPacket(&p)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = p.Scan((*pk.String)(&info.Name)) // decode username as pk.String
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if ok, err := requestVerify(info.Name); !ok || err != nil {
|
||||
err = errors.New("verify failed")
|
||||
conn.Close()
|
||||
target.Close()
|
||||
return info, err
|
||||
}
|
||||
|
||||
if err := target.WritePacket(p); err != nil {
|
||||
return info, err
|
||||
}
|
||||
|
||||
// auth
|
||||
const OnlineMode = false
|
||||
if OnlineMode {
|
||||
info.UUID = offline.NameToUUID(info.Name)
|
||||
} else {
|
||||
// offline-mode UUID
|
||||
info.UUID = offline.NameToUUID(info.Name)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func handshake(conn, target net.Conn) (protocol, intention int32, err error) {
|
||||
var (
|
||||
p pk.Packet
|
||||
Protocol, Intention pk.VarInt
|
||||
ServerAddress pk.String // ignored
|
||||
ServerPort pk.UnsignedShort // ignored
|
||||
)
|
||||
// receive handshake packet
|
||||
if err = conn.ReadPacket(&p); err != nil {
|
||||
return
|
||||
}
|
||||
err = p.Scan(&Protocol, &ServerAddress, &ServerPort, &Intention)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = target.WritePacket(p)
|
||||
|
||||
return int32(Protocol), int32(Intention), err
|
||||
}
|
||||
|
||||
func handleProxyConnection(clientConn net.Conn, targetConn net.Conn) {
|
||||
defer clientConn.Close()
|
||||
|
||||
go func() {
|
||||
for {
|
||||
var p pk.Packet
|
||||
if err := clientConn.ReadPacket(&p); err != nil {
|
||||
logrus.Errorf("ReadPacket error: %v", err)
|
||||
break
|
||||
}
|
||||
if err := targetConn.WritePacket(p); err != nil {
|
||||
logrus.Errorf("WritePacket error: %v", err)
|
||||
break
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
func() {
|
||||
for {
|
||||
var p pk.Packet
|
||||
if err := targetConn.ReadPacket(&p); err != nil {
|
||||
logrus.Errorf("ReadPacket error: %v", err)
|
||||
break
|
||||
}
|
||||
if err := clientConn.WritePacket(p); err != nil {
|
||||
logrus.Errorf("WritePacket error: %v", err)
|
||||
break
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func startProxyServer(proxyHost string, proxyPort int) {
|
||||
|
||||
settings := conf.GetBotSettings().GatewaySettings
|
||||
|
||||
l, err := net.ListenMC(fmt.Sprintf(":%d", settings.ProxyPort))
|
||||
if err != nil {
|
||||
logrus.Fatalf("Error starting proxy server: %s", err)
|
||||
}
|
||||
defer l.Close()
|
||||
logrus.Infof("Proxy server started on %s:%d", proxyHost, proxyPort)
|
||||
|
||||
for {
|
||||
clientConn, err := l.Accept()
|
||||
if err != nil {
|
||||
logrus.Errorf("Error accepting connection: %s", err)
|
||||
continue
|
||||
}
|
||||
go HandleClientConnection(clientConn)
|
||||
}
|
||||
}
|
||||
|
||||
func StartGateway() {
|
||||
settings := conf.GetBotSettings().GatewaySettings
|
||||
startProxyServer(settings.ProxyHost, settings.ProxyPort)
|
||||
}
|
@ -1,28 +1,18 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"tg-mc/conf"
|
||||
"tg-mc/services/gateway"
|
||||
"tg-mc/services/mc"
|
||||
"tg-mc/services/tgbot"
|
||||
"tg-mc/services/utils"
|
||||
)
|
||||
|
||||
func Run() {
|
||||
settings := conf.GetBotSettings()
|
||||
|
||||
if settings.EnableGateway {
|
||||
go gateway.StartGateway()
|
||||
}
|
||||
|
||||
if settings.EnableBridge {
|
||||
go mc.StartBridgeClient()
|
||||
}
|
||||
|
||||
if settings.EnableBot {
|
||||
if settings.EnableBridge {
|
||||
tgbot.Run(mc.SendMsg, mc.SendCommand)
|
||||
} else {
|
||||
tgbot.Run(mc.SendMsg, func(s string) error { return nil })
|
||||
go func() {
|
||||
for {
|
||||
if err := mc.Run(); err != nil {
|
||||
utils.SendMsg("致命错误:" + err.Error())
|
||||
}
|
||||
}
|
||||
}()
|
||||
tgbot.Run(mc.SendMsg)
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/models"
|
||||
su "tg-mc/services/utils"
|
||||
"tg-mc/utils"
|
||||
"time"
|
||||
@ -12,6 +13,7 @@ import (
|
||||
"github.com/Tnze/go-mc/chat"
|
||||
"github.com/Tnze/go-mc/data/packetid"
|
||||
"github.com/Tnze/go-mc/net/packet"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func GetJoinedPlayer(m chat.Message) (userName string, err error) {
|
||||
@ -30,50 +32,33 @@ func GetLeftPlayer(m chat.Message) (userName string, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func HandleJoinGame(userName string, mention bool, expireMode bool) {
|
||||
// return
|
||||
func HandleJoinGame(userName string, mention bool) {
|
||||
|
||||
// u, err := models.GetUserByMCName(userName)
|
||||
// if err != nil {
|
||||
// logrus.Error("get user name error: ", err)
|
||||
// }
|
||||
u, err := models.GetUserByMCName(userName)
|
||||
if err != nil {
|
||||
logrus.Error("get user name error: ", err)
|
||||
}
|
||||
time.Sleep(3 * time.Second)
|
||||
|
||||
// switch u.Status {
|
||||
// case StatusNormal:
|
||||
// if !GetAuthcator().IsAuthed(u, expireMode) {
|
||||
// m := tgbotapi.NewMessage(u.TGID, fmt.Sprintf("MC用户:%v 尝试登录,请手动允许,每次授权持续30秒", userName))
|
||||
// m.ReplyMarkup = tgbotapi.NewInlineKeyboardMarkup(
|
||||
// tgbotapi.NewInlineKeyboardRow(
|
||||
// tgbotapi.NewInlineKeyboardButtonData("批准", defs.NewApproveCommand(u.MCName).ToJSON()),
|
||||
// tgbotapi.NewInlineKeyboardButtonData("拒绝", defs.NewRejectCommand(u.MCName).ToJSON())),
|
||||
// )
|
||||
// conf.Bot.Send(m)
|
||||
// KickPlayer(userName)
|
||||
// return
|
||||
// }
|
||||
switch u.Status {
|
||||
case StatusNormal:
|
||||
if mention {
|
||||
SendMsgToPlayer("欢迎回来!", userName)
|
||||
}
|
||||
// case StatusPending:
|
||||
// SendMsgToPlayer("你还没有绑定 Telegram 哦, 5秒后你将会被踢出。请在群组中发送 /bind <你的 MC 用户名> 进行绑定。", userName)
|
||||
// time.Sleep(5 * time.Second)
|
||||
// KickPlayer(userName)
|
||||
// m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("用户:%v 没有绑定,尝试登录已被T出", userName))
|
||||
// conf.Bot.Send(m)
|
||||
// case StatusBanned:
|
||||
// SendMsgToPlayer("你已被封禁,如有疑问请联系管理员。", userName)
|
||||
// m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("用户:%v 已被封禁,尝试登录已被T出", userName))
|
||||
// conf.Bot.Send(m)
|
||||
// default:
|
||||
// SendMsgToPlayer("未知错误,请联系管理员,你将被踢出", userName)
|
||||
// time.Sleep(3 * time.Second)
|
||||
// KickPlayer(userName)
|
||||
// m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("用户:%v 登录失败,错误未知,已被T出", userName))
|
||||
// conf.Bot.Send(m)
|
||||
// }
|
||||
case StatusPending:
|
||||
SendMsgToPlayer("你还没有绑定 Telegram 哦, 5秒后你将会被踢出。请在群组中发送 /bind <你的 MC 用户名> 进行绑定。", userName)
|
||||
time.Sleep(5 * time.Second)
|
||||
kickPlayer(userName)
|
||||
case StatusBanned:
|
||||
SendMsgToPlayer("你已被封禁,如有疑问请联系管理员。", userName)
|
||||
default:
|
||||
SendMsgToPlayer("未知错误,请联系管理员,你将被踢出", userName)
|
||||
time.Sleep(3 * time.Second)
|
||||
kickPlayer(userName)
|
||||
}
|
||||
}
|
||||
|
||||
func SendCommand(cmd string) error {
|
||||
func sendCommand(cmd string) error {
|
||||
var salt int64
|
||||
if err := binary.Read(rand.Reader, binary.BigEndian, &salt); err != nil {
|
||||
return err
|
||||
@ -91,8 +76,8 @@ func SendCommand(cmd string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func KickPlayer(userName string) error {
|
||||
err := SendCommand("kick " + userName)
|
||||
func kickPlayer(userName string) error {
|
||||
err := sendCommand("kick " + userName)
|
||||
return err
|
||||
}
|
||||
|
||||
@ -100,19 +85,7 @@ func CronKick() {
|
||||
utils.CronStart(func() {
|
||||
users := su.GetAlivePlayerList()
|
||||
for _, u := range users {
|
||||
HandleJoinGame(u, false, false)
|
||||
HandleJoinGame(u, false)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func isBotMsg(msg chat.Message) bool {
|
||||
return msg.Translate == "commands.message.display.outgoing"
|
||||
}
|
||||
|
||||
func HandleLeftGame(userName string) {
|
||||
// u, err := models.GetUserByMCName(userName)
|
||||
// if err != nil {
|
||||
// logrus.Error("get user name error: ", err)
|
||||
// }
|
||||
// GetAuthcator().Reject(u)
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
"log"
|
||||
"strings"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/services/utils"
|
||||
"time"
|
||||
|
||||
"github.com/Tnze/go-mc/bot"
|
||||
@ -18,15 +17,6 @@ import (
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func StartBridgeClient() {
|
||||
for {
|
||||
if err := Run(); err != nil {
|
||||
utils.SendMsgToGroup("致命错误:" + err.Error())
|
||||
}
|
||||
time.Sleep(time.Second * 5)
|
||||
}
|
||||
}
|
||||
|
||||
func Run() error {
|
||||
conf.Client = bot.NewClient()
|
||||
conf.Client.Auth.Name = conf.GetBotSettings().MCBotName
|
||||
@ -54,8 +44,7 @@ func Run() error {
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("joinserver error: %v", err)
|
||||
return err
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Println("Login success")
|
||||
@ -74,7 +63,7 @@ func SendMsg(msg string) {
|
||||
|
||||
func SendMsgToPlayer(msg string, playerName string) {
|
||||
go func() {
|
||||
err := SendCommand(fmt.Sprintf("tell %s %s", playerName, msg))
|
||||
err := sendCommand(fmt.Sprintf("tell %s %s", playerName, msg))
|
||||
if err != nil {
|
||||
logrus.Error("send msg to player error: ", err)
|
||||
}
|
||||
@ -91,14 +80,7 @@ func onSystemMsg(msg chat.Message, overlay bool) error {
|
||||
logrus.Error("user join error ", err)
|
||||
break
|
||||
}
|
||||
go HandleJoinGame(userName, true, true)
|
||||
case EventPlayerLeft:
|
||||
userName, err := GetLeftPlayer(msg)
|
||||
if err != nil {
|
||||
logrus.Error("user left error ", err)
|
||||
break
|
||||
}
|
||||
go HandleLeftGame(userName)
|
||||
go HandleJoinGame(userName, true)
|
||||
default:
|
||||
break
|
||||
}
|
||||
@ -110,12 +92,9 @@ func onSystemMsg(msg chat.Message, overlay bool) error {
|
||||
|
||||
func onPlayerMsg(msg chat.Message, validated bool) error {
|
||||
go func() {
|
||||
if msg.Translate == "commands.message.display.outgoing" {
|
||||
return
|
||||
}
|
||||
log.Printf("Player: %s", msg)
|
||||
s := strings.Split(msg.String(), " ")
|
||||
if len(s) > 1 && !isBotMsg(msg) {
|
||||
if len(s) > 1 {
|
||||
if s[0] != fmt.Sprintf("<%v>", conf.GetBotSettings().MCBotName) {
|
||||
m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("%v", msg))
|
||||
_, err := conf.Bot.Send(m)
|
||||
|
@ -1,45 +0,0 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/defs"
|
||||
"tg-mc/models"
|
||||
"tg-mc/services/gateway"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func ApproveHandler(update tgbotapi.Update, cmd defs.Command) {
|
||||
u, err := models.GetUserByMCName(cmd.Argstr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
gateway.GetAuthcator().SetAuth(u)
|
||||
|
||||
callback := tgbotapi.NewCallback(update.CallbackQuery.ID, "已授权")
|
||||
if _, err := conf.Bot.Request(callback); err != nil {
|
||||
logrus.Panic(err)
|
||||
}
|
||||
conf.Bot.Send(tgbotapi.NewDeleteMessage(update.CallbackQuery.Message.Chat.ID,
|
||||
update.CallbackQuery.Message.MessageID))
|
||||
conf.Bot.Send(tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID,
|
||||
fmt.Sprintf("已授权☑️: %s 登录MC", u.MCName)))
|
||||
}
|
||||
|
||||
// func ApproveHandler(update tgbotapi.Update, cmd defs.Command) {
|
||||
// u, err := models.GetUserByTGID(update.CallbackQuery.From.ID)
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
// mc.GetAuthcator().Auth(u)
|
||||
// callback := tgbotapi.NewCallback(update.CallbackQuery.ID, "已授权")
|
||||
// if _, err := conf.Bot.Request(callback); err != nil {
|
||||
// logrus.Panic(err)
|
||||
// }
|
||||
// conf.Bot.Send(tgbotapi.NewDeleteMessage(update.CallbackQuery.Message.Chat.ID,
|
||||
// update.CallbackQuery.Message.MessageID))
|
||||
// conf.Bot.Send(tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID,
|
||||
// fmt.Sprintf("已授权☑️: %s 登录MC", u.MCName)))
|
||||
// }
|
@ -1,29 +0,0 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/models"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func BindHandler(m *tgbotapi.Message, i interface{}) {
|
||||
logrus.Infof("id is %d", m.Chat.ID)
|
||||
err := models.CreateUser(&models.User{
|
||||
TGID: m.From.ID,
|
||||
MCName: m.CommandArguments(),
|
||||
Status: 1,
|
||||
})
|
||||
if err != nil {
|
||||
m := tgbotapi.NewMessage(m.Chat.ID, "绑定失败, err: "+err.Error())
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
|
||||
msg := tgbotapi.NewMessage(m.Chat.ID,
|
||||
fmt.Sprintf("绑定成功,你的MCID是%v", m.CommandArguments()))
|
||||
conf.Bot.Send(msg)
|
||||
return
|
||||
}
|
@ -1,86 +1,108 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/defs"
|
||||
"tg-mc/models"
|
||||
"tg-mc/services/utils"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var funcHandlers = map[string]func(*tgbotapi.Message, interface{}){
|
||||
"talk": TalkHandler,
|
||||
"list": ListHandler,
|
||||
"bind": BindHandler,
|
||||
"unbind": UnbindHandler,
|
||||
"get": GetHandler,
|
||||
"set": SetHandler,
|
||||
"kick": KickHandler,
|
||||
"ban": BanHandler,
|
||||
}
|
||||
|
||||
var callBackHandlers = map[string]func(tgbotapi.Update, defs.Command){
|
||||
defs.CMD_APPROVE: ApproveHandler,
|
||||
defs.CMD_REJECT: RejectHandler,
|
||||
}
|
||||
|
||||
func init() {
|
||||
func Run(sendFunc func(string)) {
|
||||
var err error
|
||||
|
||||
api := conf.GetBotSettings().BotAPI
|
||||
if len(api) == 0 {
|
||||
api = tgbotapi.APIEndpoint
|
||||
}
|
||||
|
||||
HttpProxy := conf.GetBotSettings().HTTPProxy
|
||||
proxyUrl, err := url.Parse(HttpProxy)
|
||||
if err != nil {
|
||||
log.Panic(err, "HTTP_PROXY environment variable is not set correctly")
|
||||
}
|
||||
|
||||
if len(HttpProxy) != 0 {
|
||||
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}}
|
||||
conf.Bot, err = tgbotapi.NewBotAPIWithClient(
|
||||
conf.GetBotSettings().BotToken,
|
||||
api,
|
||||
tgbotapi.APIEndpoint,
|
||||
client)
|
||||
} else {
|
||||
conf.Bot, err = tgbotapi.NewBotAPIWithAPIEndpoint(conf.GetBotSettings().BotToken, api)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
conf.Bot.Debug = false
|
||||
log.Printf("Authorized on account %s", conf.Bot.Self.UserName)
|
||||
}
|
||||
|
||||
func Run(sendFunc func(string), cmdFunc func(string) error) {
|
||||
conf.Bot.Debug = false
|
||||
|
||||
log.Printf("Authorized on account %s", conf.Bot.Self.UserName)
|
||||
|
||||
u := tgbotapi.NewUpdate(0)
|
||||
u.Timeout = 60
|
||||
updates := conf.Bot.GetUpdatesChan(u)
|
||||
|
||||
for update := range updates {
|
||||
if update.CallbackQuery != nil {
|
||||
go func(update tgbotapi.Update) {
|
||||
logrus.Infof("[%s] %s", update.CallbackQuery.From.UserName, update.CallbackQuery.Data)
|
||||
cmd, err := defs.NewCommandFromJSON(update.CallbackQuery.Data)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
return
|
||||
}
|
||||
if handler, ok := callBackHandlers[cmd.Command]; ok {
|
||||
handler(update, *cmd)
|
||||
}
|
||||
}(update)
|
||||
} else if update.Message != nil {
|
||||
if update.Message != nil {
|
||||
go func(m *tgbotapi.Message) {
|
||||
logrus.Infof("[%s] %s", m.From.UserName, m.Text)
|
||||
if handler, ok := funcHandlers[m.Command()]; ok {
|
||||
handler(m, sendFunc)
|
||||
if m.Command() == "talk" {
|
||||
logrus.Infof("id is %d", m.Chat.ID)
|
||||
m := fmt.Sprintf("%v: %v", m.From.UserName, m.CommandArguments())
|
||||
sendFunc(m)
|
||||
logrus.WithError(err).Error("send message error")
|
||||
return
|
||||
}
|
||||
if m.Command() == "list" {
|
||||
logrus.Infof("id is %d", m.Chat.ID)
|
||||
m := tgbotapi.NewMessage(m.Chat.ID, utils.GetAlivePlayer())
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
if m.Command() == "bind" {
|
||||
logrus.Infof("id is %d", m.Chat.ID)
|
||||
err := models.CreateUser(&models.User{
|
||||
TGID: m.From.ID,
|
||||
MCName: m.CommandArguments(),
|
||||
Status: 1,
|
||||
})
|
||||
if err != nil {
|
||||
m := tgbotapi.NewMessage(m.Chat.ID, "绑定失败, err: "+err.Error())
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
m := tgbotapi.NewMessage(m.Chat.ID,
|
||||
fmt.Sprintf("绑定成功,你的MCID是%v", m.CommandArguments()))
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
if m.Command() == "unbind" {
|
||||
logrus.Infof("id is %d", m.Chat.ID)
|
||||
u, err := models.GetUserByTGID(m.From.ID)
|
||||
if err != nil {
|
||||
m := tgbotapi.NewMessage(m.Chat.ID, "你还没有绑定")
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
err = u.Delete(m.From.ID)
|
||||
if err != nil {
|
||||
m := tgbotapi.NewMessage(m.Chat.ID, "解绑失败")
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
m := tgbotapi.NewMessage(m.Chat.ID, "解绑成功")
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
if m.Command() == "get" {
|
||||
logrus.Infof("id is %d", m.Chat.ID)
|
||||
u, err := models.GetUserByTGID(m.From.ID)
|
||||
if err != nil {
|
||||
m := tgbotapi.NewMessage(m.Chat.ID, "你还没有绑定")
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
m := tgbotapi.NewMessage(m.Chat.ID, fmt.Sprintf("你的MCID是%v", u.MCName))
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
}(update.Message)
|
||||
}
|
||||
|
@ -1,74 +0,0 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/models"
|
||||
"tg-mc/services/utils"
|
||||
commonUtils "tg-mc/utils"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/samber/lo"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func GetHandler(msg *tgbotapi.Message, i interface{}) {
|
||||
if !utils.IsAdmin(msg) &&
|
||||
len(msg.CommandArguments()) != 0 {
|
||||
tm := tgbotapi.NewMessage(msg.Chat.ID, "您不是管理员,没有该权限")
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
} else if utils.IsAdmin(msg) &&
|
||||
len(msg.CommandArguments()) != 0 {
|
||||
a := commonUtils.GetArgs(msg.CommandArguments())
|
||||
if len(a) != 2 {
|
||||
tm := tgbotapi.NewMessage(msg.Chat.ID, "参数错误,样例:\n```\n/get <tgid|username> <value>\n```")
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
}
|
||||
if a[0] == "tgid" {
|
||||
tgid, err := strconv.ParseInt(a[1], 10, 64)
|
||||
if err != nil {
|
||||
conf.Bot.Send(tgbotapi.NewMessage(msg.Chat.ID, "ID错误,应该为int64"))
|
||||
return
|
||||
}
|
||||
u, err := models.GetUsersByTGID(tgid)
|
||||
if err != nil {
|
||||
tm := tgbotapi.NewMessage(msg.Chat.ID, fmt.Sprintf("查询出错,err:\n```\n%+v\n```", err))
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
}
|
||||
tm := tgbotapi.NewMessage(msg.Chat.ID, fmt.Sprintf("用户信息:\n```\n%+v\n```", u))
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
}
|
||||
if a[0] == "username" {
|
||||
u, err := models.GetUserByMCName(a[1])
|
||||
if err != nil {
|
||||
tm := tgbotapi.NewMessage(msg.Chat.ID, fmt.Sprintf("查询出错,err:\n```\n%+v\n```", err))
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
}
|
||||
tm := tgbotapi.NewMessage(msg.Chat.ID, fmt.Sprintf("用户信息:\n```\n%+v\n```", u))
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
}
|
||||
return
|
||||
}
|
||||
logrus.Infof("id is %d", msg.Chat.ID)
|
||||
u, err := models.GetUsersByTGID(msg.From.ID)
|
||||
if err != nil {
|
||||
m := tgbotapi.NewMessage(msg.Chat.ID, "你还没有绑定")
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
ans := strings.Join(lo.Map(u, func(u models.User, _ int) string { return u.MCName }), "\n")
|
||||
m := tgbotapi.NewMessage(msg.Chat.ID, fmt.Sprintf("你的MCID绑定有\n%v", ans))
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/models"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
)
|
||||
|
||||
func KickHandler(m *tgbotapi.Message, i interface{}) {
|
||||
f, ok := i.(func(string) error)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
u, err := models.GetUserByTGID(m.From.ID)
|
||||
if err != nil {
|
||||
conf.Bot.Send(tgbotapi.NewMessage(m.Chat.ID, "您还没有绑定账号,请先绑定"))
|
||||
return
|
||||
}
|
||||
|
||||
err = f(fmt.Sprintf("kick %s", u.MCName))
|
||||
if err != nil {
|
||||
conf.Bot.Send(tgbotapi.NewMessage(m.Chat.ID, err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
conf.Bot.Send(tgbotapi.NewMessage(m.Chat.ID, fmt.Sprintf("已踢出用户 %s", u.MCName)))
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"tg-mc/conf"
|
||||
"tg-mc/services/utils"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func ListHandler(m *tgbotapi.Message, i interface{}) {
|
||||
logrus.Infof("id is %d", m.Chat.ID)
|
||||
msg := tgbotapi.NewMessage(m.Chat.ID, utils.GetAlivePlayer())
|
||||
conf.Bot.Send(msg)
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/defs"
|
||||
"tg-mc/models"
|
||||
"tg-mc/services/gateway"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func RejectHandler(update tgbotapi.Update, cmd defs.Command) {
|
||||
u, err := models.GetUserByMCName(cmd.Argstr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
gateway.GetAuthcator().Reject(u)
|
||||
|
||||
callback := tgbotapi.NewCallback(update.CallbackQuery.ID, "已拒绝")
|
||||
if _, err := conf.Bot.Request(callback); err != nil {
|
||||
logrus.Panic(err)
|
||||
}
|
||||
conf.Bot.Send(tgbotapi.NewDeleteMessage(update.CallbackQuery.Message.Chat.ID,
|
||||
update.CallbackQuery.Message.MessageID))
|
||||
conf.Bot.Send(tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID,
|
||||
fmt.Sprintf("已拒绝❌: %s 登录MC", u.MCName)))
|
||||
}
|
@ -1,71 +0,0 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/models"
|
||||
"tg-mc/services/utils"
|
||||
commonUtils "tg-mc/utils"
|
||||
"time"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func SetHandler(m *tgbotapi.Message, i interface{}) {
|
||||
var a []string
|
||||
if t, ok := i.([]string); ok {
|
||||
a = t
|
||||
} else {
|
||||
a = commonUtils.GetArgs(m.CommandArguments())
|
||||
}
|
||||
|
||||
if !utils.IsAdmin(m) {
|
||||
tm := tgbotapi.NewMessage(m.Chat.ID, "您不是管理员,没有该权限")
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
}
|
||||
if len(a) != 3 {
|
||||
tm := tgbotapi.NewMessage(m.Chat.ID, "参数错误,样例:\n```\n/set username tgid status\n```")
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
}
|
||||
tgid, err := strconv.ParseInt(a[1], 10, 64)
|
||||
if err != nil {
|
||||
conf.Bot.Send(tgbotapi.NewMessage(m.Chat.ID, "ID错误,应该为int64"))
|
||||
return
|
||||
}
|
||||
status, err := strconv.ParseInt(a[2], 10, 64)
|
||||
if err != nil || !lo.Contains([]int64{0, 1, 2}, status) {
|
||||
conf.Bot.Send(tgbotapi.NewMessage(m.Chat.ID, "Status错误,应该为0(Pending),1(Normal),2(Banned)"))
|
||||
return
|
||||
}
|
||||
if err := models.CreateUser(&models.User{
|
||||
TGID: tgid,
|
||||
MCName: a[0],
|
||||
Status: int(status),
|
||||
}); err != nil {
|
||||
tm := tgbotapi.NewMessage(m.Chat.ID, fmt.Sprintf("创建用户错误,err:\n```\n%+v\n```", err))
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
}
|
||||
conf.Bot.Send(tgbotapi.NewMessage(m.Chat.ID, "设置用户成功"))
|
||||
}
|
||||
|
||||
func BanHandler(m *tgbotapi.Message, i interface{}) {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
randomNumber := rand.Intn(11)
|
||||
for {
|
||||
_, err := models.GetUserByTGID(int64(randomNumber))
|
||||
if err != nil {
|
||||
break
|
||||
} else {
|
||||
randomNumber = rand.Intn(11)
|
||||
}
|
||||
}
|
||||
SetHandler(m, []string{m.CommandArguments(), fmt.Sprintf("-%d", randomNumber), "2"})
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func TalkHandler(m *tgbotapi.Message, i interface{}) {
|
||||
sendFunc := i.(func(string))
|
||||
logrus.Infof("id is %d", m.Chat.ID)
|
||||
msg := fmt.Sprintf("%v: %v", m.From.UserName, m.CommandArguments())
|
||||
sendFunc(msg)
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"tg-mc/conf"
|
||||
"tg-mc/models"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/samber/lo"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func UnbindHandler(msg *tgbotapi.Message, i interface{}) {
|
||||
logrus.Infof("id is %d", msg.Chat.ID)
|
||||
if len(msg.CommandArguments()) == 0 {
|
||||
m := tgbotapi.NewMessage(msg.Chat.ID, "请输入正确的参数")
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
|
||||
us, err := models.GetUsersByTGID(msg.From.ID)
|
||||
if err != nil {
|
||||
m := tgbotapi.NewMessage(msg.Chat.ID, "你还没有绑定")
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
|
||||
lo.Map(us, func(u models.User, _ int) bool {
|
||||
if u.MCName == msg.CommandArguments() {
|
||||
u.Status = 0
|
||||
err = u.Delete(msg.From.ID)
|
||||
if err != nil {
|
||||
m := tgbotapi.NewMessage(msg.Chat.ID, "解绑失败")
|
||||
conf.Bot.Send(m)
|
||||
return false
|
||||
}
|
||||
m := tgbotapi.NewMessage(msg.Chat.ID, "解绑成功")
|
||||
conf.Bot.Send(m)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})
|
||||
}
|
@ -4,22 +4,10 @@ import (
|
||||
"tg-mc/conf"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func SendMsgToGroup(msg string) error {
|
||||
func SendMsg(msg string) error {
|
||||
msgT := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, msg)
|
||||
_, err := conf.Bot.Send(msgT)
|
||||
return err
|
||||
}
|
||||
|
||||
func SendMsg(chatID int64, msg string) error {
|
||||
msgT := tgbotapi.NewMessage(chatID, msg)
|
||||
_, err := conf.Bot.Send(msgT)
|
||||
return err
|
||||
}
|
||||
|
||||
func IsAdmin(m *tgbotapi.Message) bool {
|
||||
return lo.Contains(conf.GetBotSettings().AdminID, m.From.ID) ||
|
||||
lo.Contains(conf.GetBotSettings().AdminID, m.Chat.ID)
|
||||
}
|
||||
|
@ -1,7 +0,0 @@
|
||||
package utils
|
||||
|
||||
import "strings"
|
||||
|
||||
func GetArgs(i string) []string {
|
||||
return strings.Split(i, " ")
|
||||
}
|
@ -11,7 +11,6 @@ var (
|
||||
)
|
||||
|
||||
func CronStart(f func()) {
|
||||
return
|
||||
s.Every("1m").Do(f)
|
||||
s.StartAsync()
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package database
|
||||
|
||||
import (
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@ -14,10 +15,10 @@ func GetDB() *gorm.DB {
|
||||
return GetSqlite()
|
||||
}
|
||||
|
||||
// func CloseDB(db *gorm.DB) {
|
||||
// tdb, err := db.DB()
|
||||
// if err != nil {
|
||||
// logrus.WithError(err).Errorf("Close DB error")
|
||||
// }
|
||||
// tdb.Close()
|
||||
// }
|
||||
func CloseDB(db *gorm.DB) {
|
||||
tdb, err := db.DB()
|
||||
if err != nil {
|
||||
logrus.WithError(err).Errorf("Close DB error")
|
||||
}
|
||||
tdb.Close()
|
||||
}
|
||||
|
@ -9,25 +9,23 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var (
|
||||
db *gorm.DB
|
||||
)
|
||||
|
||||
func initSqlite() {
|
||||
var err error
|
||||
godotenv.Load()
|
||||
|
||||
dbPath := conf.GetBotSettings().DBPath
|
||||
db, err = gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
|
||||
db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
|
||||
if err != nil {
|
||||
logrus.Panic(err, "Initializing DB Error")
|
||||
}
|
||||
logrus.Info("Initialized DB at ", dbPath)
|
||||
CloseDB(db)
|
||||
}
|
||||
|
||||
func GetSqlite() *gorm.DB {
|
||||
if db == nil {
|
||||
initSqlite()
|
||||
dbPath := conf.GetBotSettings().DBPath
|
||||
db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user