mirror of
https://github.com/purofle/remake_bot.git
synced 2025-06-17 14:53:29 +08:00
feat: use uber/fx
This commit is contained in:
parent
f6717ad1b4
commit
5b37ae6794
8
.idea/.gitignore
generated
vendored
8
.idea/.gitignore
generated
vendored
@ -1,8 +0,0 @@
|
||||
# 默认忽略的文件
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# 基于编辑器的 HTTP 客户端请求
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
44
bot/tele.go
Normal file
44
bot/tele.go
Normal file
@ -0,0 +1,44 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/purofle/remake_bot/command"
|
||||
"go.uber.org/fx"
|
||||
"go.uber.org/zap"
|
||||
tele "gopkg.in/telebot.v3"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewRemakeBot(lc fx.Lifecycle, logger *zap.Logger) *tele.Bot {
|
||||
|
||||
pref := tele.Settings{
|
||||
Token: os.Getenv("TOKEN"),
|
||||
Poller: &tele.LongPoller{Timeout: 10 * time.Second},
|
||||
}
|
||||
|
||||
b, err := tele.NewBot(pref)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return nil
|
||||
}
|
||||
|
||||
lc.Append(fx.Hook{OnStart: func(ctx context.Context) error {
|
||||
|
||||
command.InitHandler()
|
||||
|
||||
b.Handle(tele.OnQuery, command.InlineQuery)
|
||||
b.Handle("/remake", command.CommandRemake)
|
||||
b.Handle("/remake_data", command.CommandRemakeData)
|
||||
b.Handle("/eat", command.CommandEat)
|
||||
b.Handle(tele.OnText, command.CommandOnText)
|
||||
go b.Start()
|
||||
|
||||
logger.Info("Remake Bot is now running...")
|
||||
|
||||
return nil
|
||||
}})
|
||||
|
||||
return b
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package main
|
||||
package command
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"github.com/purofle/remake_bot/quotely"
|
||||
tele "gopkg.in/telebot.v3"
|
||||
"log"
|
||||
"math/rand"
|
||||
crand "math/rand"
|
||||
"os"
|
||||
@ -20,13 +21,39 @@ type Country struct {
|
||||
Population int64 `json:"population"`
|
||||
}
|
||||
|
||||
type RemakeData struct {
|
||||
count int64
|
||||
country string
|
||||
gender string
|
||||
}
|
||||
|
||||
var (
|
||||
countryList []Country
|
||||
userList []string
|
||||
totalPopulation int64
|
||||
mutex sync.Mutex
|
||||
remakeCount map[int64]*RemakeData
|
||||
)
|
||||
|
||||
var database *sql.DB
|
||||
|
||||
func InitHandler() {
|
||||
|
||||
connStr := "postgresql://postgres:114514@localhost:5432/postgres?sslmode=disable"
|
||||
db, err := sql.Open("postgres", connStr)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
database = db
|
||||
|
||||
err = initList()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
remakeCount = make(map[int64]*RemakeData)
|
||||
}
|
||||
|
||||
func initList() error {
|
||||
rawJson, err := os.ReadFile("countries.json")
|
||||
if err != nil {
|
5
go.mod
5
go.mod
@ -9,4 +9,9 @@ require gopkg.in/telebot.v3 v3.3.8
|
||||
require (
|
||||
github.com/lib/pq v1.10.9 // indirect
|
||||
github.com/stretchr/testify v1.10.0 // indirect
|
||||
go.uber.org/dig v1.18.0 // indirect
|
||||
go.uber.org/fx v1.23.0 // indirect
|
||||
go.uber.org/multierr v1.10.0 // indirect
|
||||
go.uber.org/zap v1.26.0 // indirect
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
|
||||
)
|
||||
|
9
go.sum
9
go.sum
@ -377,8 +377,16 @@ go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=
|
||||
go.opencensus.io v0.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E=
|
||||
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
|
||||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/dig v1.18.0 h1:imUL1UiY0Mg4bqbFfsRQO5G4CGRBec/ZujWTvSVp3pw=
|
||||
go.uber.org/dig v1.18.0/go.mod h1:Us0rSJiThwCv2GteUN0Q7OKvU7n5J4dxZ9JKUXozFdE=
|
||||
go.uber.org/fx v1.23.0 h1:lIr/gYWQGfTwGcSXWXu4vP5Ws6iqnNEIY+F/aFzCKTg=
|
||||
go.uber.org/fx v1.23.0/go.mod h1:o/D9n+2mLP6v1EG+qsdT1O8wKopYAsqZasju97SDFCU=
|
||||
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
|
||||
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
|
||||
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||
go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo=
|
||||
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
|
||||
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
|
||||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
@ -580,6 +588,7 @@ golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBc
|
||||
golang.org/x/sys v0.0.0-20220328115105-d36c6a25d886/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220502124256-b6088ccd6cba/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
|
16
logger.go
Normal file
16
logger.go
Normal file
@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"go.uber.org/fx"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func NewLogger(lc fx.Lifecycle) *zap.Logger {
|
||||
logger, err := zap.NewDevelopment()
|
||||
defer logger.Sync()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return logger
|
||||
}
|
60
main.go
60
main.go
@ -1,55 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
tele "gopkg.in/telebot.v3"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/purofle/remake_bot/bot"
|
||||
"go.uber.org/fx"
|
||||
"go.uber.org/fx/fxevent"
|
||||
"go.uber.org/zap"
|
||||
"gopkg.in/telebot.v3"
|
||||
)
|
||||
|
||||
type RemakeData struct {
|
||||
count int64
|
||||
country string
|
||||
gender string
|
||||
}
|
||||
|
||||
var remakeCount map[int64]*RemakeData
|
||||
var database *sql.DB
|
||||
|
||||
func main() {
|
||||
pref := tele.Settings{
|
||||
Token: os.Getenv("TOKEN"),
|
||||
Poller: &tele.LongPoller{Timeout: 10 * time.Second},
|
||||
}
|
||||
|
||||
connStr := "postgresql://postgres:114514@localhost:5432/postgres?sslmode=disable"
|
||||
db, err := sql.Open("postgres", connStr)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
database = db
|
||||
|
||||
err = initList()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
remakeCount = make(map[int64]*RemakeData)
|
||||
|
||||
b, err := tele.NewBot(pref)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
b.Handle(tele.OnQuery, InlineQuery)
|
||||
b.Handle("/remake", CommandRemake)
|
||||
b.Handle("/remake_data", CommandRemakeData)
|
||||
b.Handle("/eat", CommandEat)
|
||||
b.Handle(tele.OnText, CommandOnText)
|
||||
b.Start()
|
||||
fx.New(
|
||||
fx.Provide(NewLogger),
|
||||
fx.WithLogger(func(log *zap.Logger) fxevent.Logger {
|
||||
return &fxevent.ZapLogger{Logger: log}
|
||||
}),
|
||||
fx.Provide(bot.NewRemakeBot),
|
||||
fx.Invoke(func(bot *telebot.Bot) {}),
|
||||
).Run()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user