go-dette/main.go

50 lines
1.5 KiB
Go

package main
import (
"context"
"log"
"os"
"git.nefrace.ru/nefrace/nechotron"
"git.nefrace.ru/nefrace/tongo"
"github.com/NicoNex/echotron/v3"
"github.com/joho/godotenv"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var BuildTime string
var db *tongo.Database
func main() {
log.Println("Starting Go-Dette")
godotenv.Load()
var err error
db, err = tongo.NewConnection(os.Getenv("MONGO_URI"), "godette")
if err != nil {
log.Fatalf("Can't connect to database: %v", err)
}
createIndexes(db)
token := os.Getenv("TELEGRAM_TOKEN")
neco := nechotron.NewTron(token, &MainState)
neco.
Use(UserDBUpdater(db)).
Use(ErrorLogger).
Use(ExecTimeLogger)
api := echotron.NewAPI(token)
defaultCommands := []*nechotron.Command{commandHelp, commandMe}
adminCommands := []*nechotron.Command{commandSay, commandWarn}
allCommands := append(defaultCommands, adminCommands...)
nechotron.SetMyCommands(api, "", echotron.BotCommandScope{Type: echotron.Any}, defaultCommands...)
nechotron.SetMyCommands(api, "", echotron.BotCommandScope{Type: echotron.BCSTAllChatAdministrators}, allCommands...)
log.Fatal(neco.DispatchPoll())
}
func createIndexes(db *tongo.Database) error {
userStore := tongo.NewStore[User](db)
_, err := userStore.Coll.Indexes().CreateOne(context.Background(), userIndex, options.CreateIndexes())
karmaShotStore := tongo.NewStore[KarmaShot](db)
_, err = karmaShotStore.Coll.Indexes().CreateMany(context.Background(), []mongo.IndexModel{karmaFromIndex, karmaToIndex}, options.CreateIndexes())
return err
}