🔀 Merge branch 'develop'
This commit is contained in:
commit
41fcfd5f51
44 changed files with 563 additions and 561 deletions
47
.gitlab-ci.yml
Normal file
47
.gitlab-ci.yml
Normal file
|
@ -0,0 +1,47 @@
|
|||
image: golang:alpine
|
||||
|
||||
cache:
|
||||
paths:
|
||||
- /go/src/github.com
|
||||
- /go/src/gitlab.com
|
||||
- /go/src/golang.org
|
||||
- /go/src/google.golang.org
|
||||
- /go/src/gopkg.in
|
||||
|
||||
stages:
|
||||
- test
|
||||
- build
|
||||
|
||||
before_script:
|
||||
- apk add --no-cache git build-base bash
|
||||
- mkdir -p /go/src/gitlab.com/$CI_PROJECT_NAMESPACE /go/src/_/builds
|
||||
- cp -r $CI_PROJECT_DIR /go/src/gitlab.com/$CI_PROJECT_PATH
|
||||
- ln -s /go/src/gitlab.com/$CI_PROJECT_NAMESPACE /go/src/_/builds/$CI_PROJECT_NAMESPACE
|
||||
- make dep
|
||||
|
||||
unit_tests:
|
||||
stage: test
|
||||
script:
|
||||
- make test
|
||||
|
||||
.race_detector:
|
||||
stage: test
|
||||
script:
|
||||
- make race
|
||||
|
||||
code_coverage:
|
||||
stage: test
|
||||
script:
|
||||
- make coverage
|
||||
|
||||
lint_code:
|
||||
stage: test
|
||||
script:
|
||||
- go get github.com/go-critic/go-critic/cmd/gocritic
|
||||
- go install github.com/go-critic/go-critic/cmd/gocritic
|
||||
- make lint
|
||||
|
||||
build:
|
||||
stage: build
|
||||
script:
|
||||
- make
|
|
@ -1,8 +0,0 @@
|
|||
language: go
|
||||
|
||||
go:
|
||||
- tip
|
||||
|
||||
install:
|
||||
- go get -tags=debug
|
||||
- go get -u github.com/nicksnyder/go-i18n/goi18n
|
53
Makefile
53
Makefile
|
@ -1,31 +1,32 @@
|
|||
# Build package by default with all general tools and things
|
||||
all:
|
||||
make localization
|
||||
make build
|
||||
PROJECT_NAMESPACE := $(CI_PROJECT_NAMESPACE)
|
||||
PROJECT_NAME := $(CI_PROJECT_NAME)
|
||||
PROJECT_PATH := $(PROJECT_NAMESPACE)/$(PROJECT_NAME)
|
||||
PACKAGE_NAME := "gitlab.com/$(PROJECT_PATH)"
|
||||
PACKAGE_PATH := $(GOPATH)/src/$(PACKAGE_NAME)
|
||||
PACKAGE_LIST := $(shell go list $(PACKAGE_NAME)/... | grep -v /vendor/)
|
||||
GO_FILES := $(shell find . -name '*.go' | grep -v /vendor/ | grep -v _test.go)
|
||||
|
||||
# Build minimal package only
|
||||
build:
|
||||
go build
|
||||
.PHONY: all lint test rase coverage dep build clean
|
||||
|
||||
# Build debug version with more logs
|
||||
debug:
|
||||
go build -tags=debug
|
||||
all: build
|
||||
|
||||
# Format the source code
|
||||
fmt:
|
||||
go fmt
|
||||
lint: ## Lint the files
|
||||
@gocritic check-project $(PACKAGE_PATH)
|
||||
|
||||
# Build localization files with separated untranslated strings
|
||||
translation:
|
||||
goi18n merge -format yaml \
|
||||
-sourceLanguage en \
|
||||
-outdir ./i18n/ \
|
||||
./i18n/src/*/*
|
||||
race: dep ## Run data race detector
|
||||
@go test -race -short ${PACKAGE_LIST}
|
||||
|
||||
# Build localization files and merge untranslated strings
|
||||
localization:
|
||||
make translation
|
||||
goi18n -format yaml \
|
||||
-sourceLanguage en \
|
||||
-outdir ./i18n/ \
|
||||
./i18n/*.all.yaml ./i18n/*.untranslated.yaml
|
||||
coverage: ## Generate global code coverage report
|
||||
@go test -cover -v -coverpkg=$(PACKAGE_NAME) ${PACKAGE_LIST}
|
||||
|
||||
dep: ## Get the dependencies
|
||||
@go get -v -d -t ${PACKAGE_LIST}
|
||||
|
||||
build: dep ## Build the binary file
|
||||
@go build -i -v $(PACKAGE_NAME)
|
||||
|
||||
clean: ## Remove previous build
|
||||
@rm -f $(PROJECT_NAME)
|
||||
|
||||
help: ## Display this help screen
|
||||
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
|
|
@ -1,20 +0,0 @@
|
|||
<!--
|
||||
Welcome to my repo! 👋
|
||||
|
||||
Need help or you have a question? Please come chat in Discord: https://discord.gg/KYQB9FR
|
||||
Found a bug? Want a new feature? Please fill out the sections below. Thanks! 👍
|
||||
-->
|
||||
### Summary
|
||||
<!-- A summary of the issue or, maybe, your ideas for the feature. -->
|
||||
|
||||
### How-To
|
||||
<!--
|
||||
1. This is the first step
|
||||
2. This is the second step, etc.
|
||||
|
||||
Any other info e.g. Why do you consider this to be a bug? What did you expect to happen instead?
|
||||
|
||||
If this is a feature, then describe how it works, what it can potentially be useful, and so on.
|
||||
|
||||
P.S.: Bugs are always in priority over the new functionality, unless, of course, it was offered by a sponsor on my Patreon: https://patreon.com/toby3d
|
||||
-->
|
|
@ -1,16 +0,0 @@
|
|||
<!--
|
||||
Thanks for your help to my repo! 👋
|
||||
|
||||
Need help or you have a question? Please come chat in Discord: https://discord.gg/KYQB9FR
|
||||
Fixed a bug? Added new feature? Make a new translation? Please fill out the section below. Thanks! 👍
|
||||
-->
|
||||
### Summary
|
||||
<!--
|
||||
Describe the essence of your commits.
|
||||
|
||||
- Is it a fix, feature or a translation?
|
||||
- What issue is closed by your commits?
|
||||
- You are totally sure what are you removed all warnings by [gometalinter](https://github.com/alecthomas/gometalinter)?
|
||||
- Your code is compiled without errors?
|
||||
- Your code works without obvious errors?
|
||||
-->
|
28
init/init.go
28
init/init.go
|
@ -1,28 +0,0 @@
|
|||
package init
|
||||
|
||||
import (
|
||||
log "github.com/kirillDanshin/dlog"
|
||||
"github.com/toby3d/MyPackBot/internal/bot"
|
||||
"github.com/toby3d/MyPackBot/internal/config"
|
||||
"github.com/toby3d/MyPackBot/internal/db"
|
||||
"github.com/toby3d/MyPackBot/internal/errors"
|
||||
"github.com/toby3d/MyPackBot/internal/i18n"
|
||||
)
|
||||
|
||||
// init prepare configuration and other things for successful start
|
||||
func init() {
|
||||
log.Ln("Initializing...")
|
||||
|
||||
// Preload localization strings
|
||||
err := i18n.Open("i18n/")
|
||||
errors.Check(err)
|
||||
|
||||
// Preload configuration file
|
||||
config.Open("configs/config.yaml")
|
||||
|
||||
// Open database or create new one
|
||||
db.Open("stickers.db")
|
||||
|
||||
// Create bot with credentials from config
|
||||
bot.New()
|
||||
}
|
|
@ -2,16 +2,16 @@ package actions
|
|||
|
||||
import (
|
||||
log "github.com/kirillDanshin/dlog"
|
||||
"github.com/toby3d/MyPackBot/internal/db"
|
||||
"github.com/toby3d/MyPackBot/internal/errors"
|
||||
"github.com/toby3d/MyPackBot/internal/models"
|
||||
tg "github.com/toby3d/telegram"
|
||||
"gitlab.com/toby3d/mypackbot/internal/db"
|
||||
"gitlab.com/toby3d/mypackbot/internal/errors"
|
||||
"gitlab.com/toby3d/mypackbot/internal/models"
|
||||
tg "gitlab.com/toby3d/telegram"
|
||||
)
|
||||
|
||||
// Action function check Message update on commands, sended stickers or other
|
||||
// user stuff if user state is not 'none'
|
||||
func Action(msg *tg.Message) {
|
||||
state, err := db.UserState(msg.From.ID)
|
||||
state, err := db.DB.GetUserState(msg.From)
|
||||
errors.Check(err)
|
||||
|
||||
log.Ln("state:", state)
|
||||
|
@ -27,7 +27,7 @@ func Action(msg *tg.Message) {
|
|||
case models.StateReset:
|
||||
Reset(msg)
|
||||
default:
|
||||
err = db.ChangeUserState(msg.From.ID, models.StateNone)
|
||||
err = db.DB.ChangeUserState(msg.From, models.StateNone)
|
||||
errors.Check(err)
|
||||
|
||||
Error(msg)
|
||||
|
|
|
@ -2,13 +2,13 @@ package actions
|
|||
|
||||
import (
|
||||
log "github.com/kirillDanshin/dlog"
|
||||
"github.com/toby3d/MyPackBot/internal/bot"
|
||||
"github.com/toby3d/MyPackBot/internal/db"
|
||||
"github.com/toby3d/MyPackBot/internal/errors"
|
||||
"github.com/toby3d/MyPackBot/internal/helpers"
|
||||
"github.com/toby3d/MyPackBot/internal/i18n"
|
||||
"github.com/toby3d/MyPackBot/internal/models"
|
||||
tg "github.com/toby3d/telegram"
|
||||
"gitlab.com/toby3d/mypackbot/internal/bot"
|
||||
"gitlab.com/toby3d/mypackbot/internal/db"
|
||||
"gitlab.com/toby3d/mypackbot/internal/errors"
|
||||
"gitlab.com/toby3d/mypackbot/internal/i18n"
|
||||
"gitlab.com/toby3d/mypackbot/internal/models"
|
||||
"gitlab.com/toby3d/mypackbot/internal/utils"
|
||||
tg "gitlab.com/toby3d/telegram"
|
||||
)
|
||||
|
||||
// Add action add sticker or set to user's pack
|
||||
|
@ -17,34 +17,32 @@ func Add(msg *tg.Message, pack bool) {
|
|||
return
|
||||
}
|
||||
|
||||
T, err := i18n.SwitchTo(msg.From.LanguageCode)
|
||||
t, err := i18n.SwitchTo(msg.From.LanguageCode)
|
||||
errors.Check(err)
|
||||
|
||||
_, err = bot.Bot.SendChatAction(msg.Chat.ID, tg.ActionTyping)
|
||||
errors.Check(err)
|
||||
|
||||
reply := tg.NewMessage(msg.Chat.ID, T("success_add_sticker"))
|
||||
reply.ParseMode = tg.ModeMarkdown
|
||||
reply := tg.NewMessage(msg.Chat.ID, t("success_add_sticker"))
|
||||
reply.ParseMode = tg.StyleMarkdown
|
||||
|
||||
if !pack {
|
||||
var exist bool
|
||||
sticker := msg.Sticker
|
||||
exist, err = db.AddSticker(
|
||||
msg.From.ID, sticker.SetName, sticker.FileID, sticker.Emoji,
|
||||
)
|
||||
exist, err = db.DB.AddSticker(msg.From, sticker)
|
||||
errors.Check(err)
|
||||
|
||||
if exist {
|
||||
reply.Text = T("error_already_add_sticker")
|
||||
reply.Text = t("error_already_add_sticker")
|
||||
}
|
||||
|
||||
reply.ReplyMarkup = helpers.CancelButton(T)
|
||||
reply.ReplyMarkup = utils.CancelButton(t)
|
||||
_, err = bot.Bot.SendMessage(reply)
|
||||
errors.Check(err)
|
||||
return
|
||||
}
|
||||
|
||||
reply.Text = T("error_empty_add_pack", map[string]interface{}{
|
||||
reply.Text = t("error_empty_add_pack", map[string]interface{}{
|
||||
"AddStickerCommand": models.CommandAddSticker,
|
||||
})
|
||||
|
||||
|
@ -54,16 +52,14 @@ func Add(msg *tg.Message, pack bool) {
|
|||
errors.Check(err)
|
||||
|
||||
log.Ln("SetTitle:", set.Title)
|
||||
reply.Text = T("success_add_pack", map[string]interface{}{
|
||||
reply.Text = t("success_add_pack", map[string]interface{}{
|
||||
"SetTitle": set.Title,
|
||||
})
|
||||
|
||||
allExists := true
|
||||
for _, sticker := range set.Stickers {
|
||||
for i := range set.Stickers {
|
||||
var exist bool
|
||||
exist, err = db.AddSticker(
|
||||
msg.From.ID, sticker.SetName, sticker.FileID, sticker.Emoji,
|
||||
)
|
||||
exist, err = db.DB.AddSticker(msg.From, &set.Stickers[i])
|
||||
errors.Check(err)
|
||||
|
||||
if !exist {
|
||||
|
@ -73,13 +69,13 @@ func Add(msg *tg.Message, pack bool) {
|
|||
|
||||
log.Ln("All exists?", allExists)
|
||||
if allExists {
|
||||
reply.Text = T("error_already_add_pack", map[string]interface{}{
|
||||
reply.Text = t("error_already_add_pack", map[string]interface{}{
|
||||
"SetTitle": set.Title,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
reply.ReplyMarkup = helpers.CancelButton(T)
|
||||
reply.ReplyMarkup = utils.CancelButton(t)
|
||||
_, err = bot.Bot.SendMessage(reply)
|
||||
errors.Check(err)
|
||||
}
|
||||
|
|
|
@ -2,12 +2,12 @@ package actions
|
|||
|
||||
import (
|
||||
log "github.com/kirillDanshin/dlog"
|
||||
"github.com/toby3d/MyPackBot/internal/bot"
|
||||
"github.com/toby3d/MyPackBot/internal/db"
|
||||
"github.com/toby3d/MyPackBot/internal/errors"
|
||||
"github.com/toby3d/MyPackBot/internal/helpers"
|
||||
"github.com/toby3d/MyPackBot/internal/i18n"
|
||||
tg "github.com/toby3d/telegram"
|
||||
"gitlab.com/toby3d/mypackbot/internal/bot"
|
||||
"gitlab.com/toby3d/mypackbot/internal/db"
|
||||
"gitlab.com/toby3d/mypackbot/internal/errors"
|
||||
"gitlab.com/toby3d/mypackbot/internal/i18n"
|
||||
"gitlab.com/toby3d/mypackbot/internal/utils"
|
||||
tg "gitlab.com/toby3d/telegram"
|
||||
)
|
||||
|
||||
// Delete action remove sticker or set from user's pack
|
||||
|
@ -16,15 +16,15 @@ func Delete(msg *tg.Message, pack bool) {
|
|||
return
|
||||
}
|
||||
|
||||
T, err := i18n.SwitchTo(msg.From.LanguageCode)
|
||||
t, err := i18n.SwitchTo(msg.From.LanguageCode)
|
||||
errors.Check(err)
|
||||
|
||||
_, err = bot.Bot.SendChatAction(msg.Chat.ID, tg.ActionTyping)
|
||||
errors.Check(err)
|
||||
|
||||
reply := tg.NewMessage(msg.Chat.ID, T("success_del_sticker"))
|
||||
reply.ParseMode = tg.ModeMarkdown
|
||||
reply.ReplyMarkup = helpers.CancelButton(T)
|
||||
reply := tg.NewMessage(msg.Chat.ID, t("success_del_sticker"))
|
||||
reply.ParseMode = tg.StyleMarkdown
|
||||
reply.ReplyMarkup = utils.CancelButton(t)
|
||||
|
||||
var notExist bool
|
||||
if pack {
|
||||
|
@ -33,22 +33,18 @@ func Delete(msg *tg.Message, pack bool) {
|
|||
errors.Check(err)
|
||||
|
||||
log.Ln("SetName:", set.Title)
|
||||
reply.Text = T("success_del_pack", map[string]interface{}{
|
||||
reply.Text = t("success_del_pack", map[string]interface{}{
|
||||
"SetTitle": set.Title,
|
||||
})
|
||||
|
||||
notExist, err = db.DeletePack(msg.From.ID, msg.Sticker.SetName)
|
||||
notExist, err = db.DB.DeletePack(msg.From, msg.Sticker)
|
||||
if notExist {
|
||||
reply.Text = T("error_already_del_pack")
|
||||
reply.Text = t("error_already_del_pack")
|
||||
}
|
||||
} else {
|
||||
notExist, err = db.DeleteSticker(
|
||||
msg.From.ID,
|
||||
msg.Sticker.SetName,
|
||||
msg.Sticker.FileID,
|
||||
)
|
||||
notExist, err = db.DB.DeleteSticker(msg.From, msg.Sticker)
|
||||
if notExist {
|
||||
reply.Text = T("error_already_del_sticker")
|
||||
reply.Text = t("error_already_del_sticker")
|
||||
}
|
||||
}
|
||||
errors.Check(err)
|
||||
|
|
|
@ -1,32 +1,32 @@
|
|||
package actions
|
||||
|
||||
import (
|
||||
"github.com/toby3d/MyPackBot/internal/bot"
|
||||
"github.com/toby3d/MyPackBot/internal/errors"
|
||||
"github.com/toby3d/MyPackBot/internal/helpers"
|
||||
"github.com/toby3d/MyPackBot/internal/i18n"
|
||||
"github.com/toby3d/MyPackBot/internal/models"
|
||||
tg "github.com/toby3d/telegram"
|
||||
"gitlab.com/toby3d/mypackbot/internal/bot"
|
||||
"gitlab.com/toby3d/mypackbot/internal/errors"
|
||||
"gitlab.com/toby3d/mypackbot/internal/i18n"
|
||||
"gitlab.com/toby3d/mypackbot/internal/models"
|
||||
"gitlab.com/toby3d/mypackbot/internal/utils"
|
||||
tg "gitlab.com/toby3d/telegram"
|
||||
)
|
||||
|
||||
// Error action send error reply about invalid user request
|
||||
func Error(msg *tg.Message) {
|
||||
T, err := i18n.SwitchTo(msg.From.LanguageCode)
|
||||
t, err := i18n.SwitchTo(msg.From.LanguageCode)
|
||||
errors.Check(err)
|
||||
|
||||
_, err = bot.Bot.SendChatAction(msg.Chat.ID, tg.ActionTyping)
|
||||
errors.Check(err)
|
||||
|
||||
reply := tg.NewMessage(
|
||||
msg.Chat.ID, T("error_unknown", map[string]interface{}{
|
||||
msg.Chat.ID, t("error_unknown", map[string]interface{}{
|
||||
"AddStickerCommand": models.CommandAddSticker,
|
||||
"AddPackCommand": models.CommandAddPack,
|
||||
"DeleteStickerCommand": models.CommandDeleteSticker,
|
||||
"DeletePackCommand": models.CommandDeletePack,
|
||||
}),
|
||||
)
|
||||
reply.ParseMode = tg.ModeMarkdown
|
||||
reply.ReplyMarkup = helpers.MenuKeyboard(T)
|
||||
reply.ParseMode = tg.StyleMarkdown
|
||||
reply.ReplyMarkup = utils.MenuKeyboard(t)
|
||||
|
||||
_, err = bot.Bot.SendMessage(reply)
|
||||
errors.Check(err)
|
||||
|
|
|
@ -3,42 +3,42 @@ package actions
|
|||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/toby3d/MyPackBot/internal/bot"
|
||||
"github.com/toby3d/MyPackBot/internal/db"
|
||||
"github.com/toby3d/MyPackBot/internal/errors"
|
||||
"github.com/toby3d/MyPackBot/internal/helpers"
|
||||
"github.com/toby3d/MyPackBot/internal/i18n"
|
||||
"github.com/toby3d/MyPackBot/internal/models"
|
||||
tg "github.com/toby3d/telegram"
|
||||
"gitlab.com/toby3d/mypackbot/internal/bot"
|
||||
"gitlab.com/toby3d/mypackbot/internal/db"
|
||||
"gitlab.com/toby3d/mypackbot/internal/errors"
|
||||
"gitlab.com/toby3d/mypackbot/internal/i18n"
|
||||
"gitlab.com/toby3d/mypackbot/internal/models"
|
||||
"gitlab.com/toby3d/mypackbot/internal/utils"
|
||||
tg "gitlab.com/toby3d/telegram"
|
||||
)
|
||||
|
||||
// Reset action checks key phrase and reset user's pack
|
||||
func Reset(msg *tg.Message) {
|
||||
T, err := i18n.SwitchTo(msg.From.LanguageCode)
|
||||
t, err := i18n.SwitchTo(msg.From.LanguageCode)
|
||||
errors.Check(err)
|
||||
|
||||
err = db.ChangeUserState(msg.From.ID, models.StateNone)
|
||||
err = db.DB.ChangeUserState(msg.From, models.StateNone)
|
||||
errors.Check(err)
|
||||
|
||||
_, err = bot.Bot.SendChatAction(msg.Chat.ID, tg.ActionTyping)
|
||||
errors.Check(err)
|
||||
|
||||
if !strings.EqualFold(msg.Text, T("key_phrase")) {
|
||||
reply := tg.NewMessage(msg.Chat.ID, T("error_reset_phrase"))
|
||||
reply.ParseMode = tg.ModeMarkdown
|
||||
reply.ReplyMarkup = helpers.MenuKeyboard(T)
|
||||
if !strings.EqualFold(msg.Text, t("key_phrase")) {
|
||||
reply := tg.NewMessage(msg.Chat.ID, t("error_reset_phrase"))
|
||||
reply.ParseMode = tg.StyleMarkdown
|
||||
reply.ReplyMarkup = utils.MenuKeyboard(t)
|
||||
|
||||
_, err = bot.Bot.SendMessage(reply)
|
||||
errors.Check(err)
|
||||
return
|
||||
}
|
||||
|
||||
err = db.ResetUser(msg.From.ID)
|
||||
err = db.DB.ResetUser(msg.From)
|
||||
errors.Check(err)
|
||||
|
||||
reply := tg.NewMessage(msg.Chat.ID, T("success_reset"))
|
||||
reply.ParseMode = tg.ModeMarkdown
|
||||
reply.ReplyMarkup = helpers.MenuKeyboard(T)
|
||||
reply := tg.NewMessage(msg.Chat.ID, t("success_reset"))
|
||||
reply.ParseMode = tg.StyleMarkdown
|
||||
reply.ReplyMarkup = utils.MenuKeyboard(t)
|
||||
_, err = bot.Bot.SendMessage(reply)
|
||||
errors.Check(err)
|
||||
}
|
||||
|
|
|
@ -1,19 +1,11 @@
|
|||
package bot
|
||||
|
||||
import (
|
||||
"github.com/toby3d/MyPackBot/internal/config"
|
||||
"github.com/toby3d/MyPackBot/internal/errors"
|
||||
tg "github.com/toby3d/telegram"
|
||||
)
|
||||
import tg "gitlab.com/toby3d/telegram"
|
||||
|
||||
// Bot is a main object of Telegram bot
|
||||
var Bot *tg.Bot
|
||||
|
||||
// New just create new bot by configuration credentials
|
||||
func New() {
|
||||
accessToken, err := config.Config.String("telegram.token")
|
||||
errors.Check(err)
|
||||
|
||||
Bot, err = tg.NewBot(accessToken)
|
||||
errors.Check(err)
|
||||
func New(accessToken string) (bot *tg.Bot, err error) {
|
||||
return tg.New(accessToken)
|
||||
}
|
||||
|
|
|
@ -2,34 +2,34 @@ package commands
|
|||
|
||||
import (
|
||||
log "github.com/kirillDanshin/dlog"
|
||||
"github.com/toby3d/MyPackBot/internal/bot"
|
||||
"github.com/toby3d/MyPackBot/internal/db"
|
||||
"github.com/toby3d/MyPackBot/internal/errors"
|
||||
"github.com/toby3d/MyPackBot/internal/helpers"
|
||||
"github.com/toby3d/MyPackBot/internal/i18n"
|
||||
"github.com/toby3d/MyPackBot/internal/models"
|
||||
tg "github.com/toby3d/telegram"
|
||||
"gitlab.com/toby3d/mypackbot/internal/bot"
|
||||
"gitlab.com/toby3d/mypackbot/internal/db"
|
||||
"gitlab.com/toby3d/mypackbot/internal/errors"
|
||||
"gitlab.com/toby3d/mypackbot/internal/i18n"
|
||||
"gitlab.com/toby3d/mypackbot/internal/models"
|
||||
"gitlab.com/toby3d/mypackbot/internal/utils"
|
||||
tg "gitlab.com/toby3d/telegram"
|
||||
)
|
||||
|
||||
// Add command prepare user for adding some stickers or sets to his pack
|
||||
func Add(msg *tg.Message, pack bool) {
|
||||
T, err := i18n.SwitchTo(msg.From.LanguageCode)
|
||||
t, err := i18n.SwitchTo(msg.From.LanguageCode)
|
||||
errors.Check(err)
|
||||
|
||||
_, err = bot.Bot.SendChatAction(msg.Chat.ID, tg.ActionTyping)
|
||||
errors.Check(err)
|
||||
|
||||
reply := tg.NewMessage(msg.Chat.ID, T("reply_add_sticker"))
|
||||
reply.ParseMode = tg.ModeMarkdown
|
||||
reply.ReplyMarkup = helpers.CancelButton(T)
|
||||
reply := tg.NewMessage(msg.Chat.ID, t("reply_add_sticker"))
|
||||
reply.ParseMode = tg.StyleMarkdown
|
||||
reply.ReplyMarkup = utils.CancelButton(t)
|
||||
|
||||
err = db.ChangeUserState(msg.From.ID, models.StateAddSticker)
|
||||
err = db.DB.ChangeUserState(msg.From, models.StateAddSticker)
|
||||
errors.Check(err)
|
||||
|
||||
if pack {
|
||||
reply.Text = T("reply_add_pack")
|
||||
reply.Text = t("reply_add_pack")
|
||||
|
||||
err = db.ChangeUserState(msg.From.ID, models.StateAddPack)
|
||||
err = db.DB.ChangeUserState(msg.From, models.StateAddPack)
|
||||
errors.Check(err)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,21 +1,21 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"github.com/toby3d/MyPackBot/internal/bot"
|
||||
"github.com/toby3d/MyPackBot/internal/db"
|
||||
"github.com/toby3d/MyPackBot/internal/errors"
|
||||
"github.com/toby3d/MyPackBot/internal/helpers"
|
||||
"github.com/toby3d/MyPackBot/internal/i18n"
|
||||
"github.com/toby3d/MyPackBot/internal/models"
|
||||
tg "github.com/toby3d/telegram"
|
||||
"gitlab.com/toby3d/mypackbot/internal/bot"
|
||||
"gitlab.com/toby3d/mypackbot/internal/db"
|
||||
"gitlab.com/toby3d/mypackbot/internal/errors"
|
||||
"gitlab.com/toby3d/mypackbot/internal/i18n"
|
||||
"gitlab.com/toby3d/mypackbot/internal/models"
|
||||
"gitlab.com/toby3d/mypackbot/internal/utils"
|
||||
tg "gitlab.com/toby3d/telegram"
|
||||
)
|
||||
|
||||
// Cancel just cancel current user operation
|
||||
func Cancel(msg *tg.Message) {
|
||||
T, err := i18n.SwitchTo(msg.From.LanguageCode)
|
||||
t, err := i18n.SwitchTo(msg.From.LanguageCode)
|
||||
errors.Check(err)
|
||||
|
||||
state, err := db.UserState(msg.From.ID)
|
||||
state, err := db.DB.GetUserState(msg.From)
|
||||
errors.Check(err)
|
||||
|
||||
_, err = bot.Bot.SendChatAction(msg.Chat.ID, tg.ActionTyping)
|
||||
|
@ -24,24 +24,24 @@ func Cancel(msg *tg.Message) {
|
|||
var text string
|
||||
switch state {
|
||||
case models.StateAddSticker:
|
||||
text = T("cancel_add_sticker")
|
||||
text = t("cancel_add_sticker")
|
||||
case models.StateAddPack:
|
||||
text = T("cancel_add_pack")
|
||||
text = t("cancel_add_pack")
|
||||
case models.StateDeleteSticker:
|
||||
text = T("cancel_del_sticker")
|
||||
text = t("cancel_del_sticker")
|
||||
case models.StateDeletePack:
|
||||
text = T("cancel_del_pack")
|
||||
text = t("cancel_del_pack")
|
||||
case models.StateReset:
|
||||
text = T("cancel_reset")
|
||||
text = t("cancel_reset")
|
||||
default:
|
||||
text = T("cancel_error")
|
||||
text = t("cancel_error")
|
||||
}
|
||||
|
||||
err = db.ChangeUserState(msg.From.ID, models.StateNone)
|
||||
err = db.DB.ChangeUserState(msg.From, models.StateNone)
|
||||
errors.Check(err)
|
||||
|
||||
reply := tg.NewMessage(msg.Chat.ID, text)
|
||||
reply.ReplyMarkup = helpers.MenuKeyboard(T)
|
||||
reply.ReplyMarkup = utils.MenuKeyboard(t)
|
||||
|
||||
_, err = bot.Bot.SendMessage(reply)
|
||||
errors.Check(err)
|
||||
|
|
|
@ -2,29 +2,29 @@ package commands
|
|||
|
||||
import (
|
||||
log "github.com/kirillDanshin/dlog"
|
||||
"github.com/toby3d/MyPackBot/internal/models"
|
||||
tg "github.com/toby3d/telegram"
|
||||
"gitlab.com/toby3d/mypackbot/internal/models"
|
||||
tg "gitlab.com/toby3d/telegram"
|
||||
)
|
||||
|
||||
// Command check's got user command
|
||||
func Command(msg *tg.Message) {
|
||||
log.Ln("command:", msg.Command())
|
||||
switch {
|
||||
case msg.IsCommand(models.CommandStart):
|
||||
case msg.IsCommandEqual(tg.CommandStart):
|
||||
Start(msg)
|
||||
case msg.IsCommand(models.CommandHelp):
|
||||
case msg.IsCommandEqual(tg.CommandHelp):
|
||||
Help(msg)
|
||||
case msg.IsCommand(models.CommandAddSticker):
|
||||
case msg.IsCommandEqual(models.CommandAddSticker):
|
||||
Add(msg, false)
|
||||
case msg.IsCommand(models.CommandAddPack):
|
||||
case msg.IsCommandEqual(models.CommandAddPack):
|
||||
Add(msg, true)
|
||||
case msg.IsCommand(models.CommandDeleteSticker):
|
||||
case msg.IsCommandEqual(models.CommandDeleteSticker):
|
||||
Delete(msg, false)
|
||||
case msg.IsCommand(models.CommandDeletePack):
|
||||
case msg.IsCommandEqual(models.CommandDeletePack):
|
||||
Delete(msg, true)
|
||||
case msg.IsCommand(models.CommandReset):
|
||||
case msg.IsCommandEqual(models.CommandReset):
|
||||
Reset(msg)
|
||||
case msg.IsCommand(models.CommandCancel):
|
||||
case msg.IsCommandEqual(models.CommandCancel):
|
||||
Cancel(msg)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,49 +1,49 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"github.com/toby3d/MyPackBot/internal/bot"
|
||||
"github.com/toby3d/MyPackBot/internal/db"
|
||||
"github.com/toby3d/MyPackBot/internal/errors"
|
||||
"github.com/toby3d/MyPackBot/internal/helpers"
|
||||
"github.com/toby3d/MyPackBot/internal/i18n"
|
||||
"github.com/toby3d/MyPackBot/internal/models"
|
||||
tg "github.com/toby3d/telegram"
|
||||
"gitlab.com/toby3d/mypackbot/internal/bot"
|
||||
"gitlab.com/toby3d/mypackbot/internal/db"
|
||||
"gitlab.com/toby3d/mypackbot/internal/errors"
|
||||
"gitlab.com/toby3d/mypackbot/internal/i18n"
|
||||
"gitlab.com/toby3d/mypackbot/internal/models"
|
||||
"gitlab.com/toby3d/mypackbot/internal/utils"
|
||||
tg "gitlab.com/toby3d/telegram"
|
||||
)
|
||||
|
||||
// Delete prepare user to remove some stickers or sets from his pack
|
||||
func Delete(msg *tg.Message, pack bool) {
|
||||
T, err := i18n.SwitchTo(msg.From.LanguageCode)
|
||||
t, err := i18n.SwitchTo(msg.From.LanguageCode)
|
||||
errors.Check(err)
|
||||
|
||||
_, total, err := db.UserStickers(msg.From.ID, 0, "")
|
||||
stickers, err := db.DB.GetUserStickers(msg.From, &tg.InlineQuery{})
|
||||
errors.Check(err)
|
||||
|
||||
_, err = bot.Bot.SendChatAction(msg.Chat.ID, tg.ActionTyping)
|
||||
errors.Check(err)
|
||||
|
||||
if total <= 0 {
|
||||
err = db.ChangeUserState(msg.From.ID, models.StateNone)
|
||||
if len(stickers) <= 0 {
|
||||
err = db.DB.ChangeUserState(msg.From, models.StateNone)
|
||||
errors.Check(err)
|
||||
|
||||
reply := tg.NewMessage(msg.Chat.ID, T("error_empty_del"))
|
||||
reply.ReplyMarkup = helpers.MenuKeyboard(T)
|
||||
reply := tg.NewMessage(msg.Chat.ID, t("error_empty_del"))
|
||||
reply.ReplyMarkup = utils.MenuKeyboard(t)
|
||||
_, err = bot.Bot.SendMessage(reply)
|
||||
errors.Check(err)
|
||||
return
|
||||
}
|
||||
|
||||
reply := tg.NewMessage(msg.Chat.ID, T("reply_del_sticker"))
|
||||
reply.ParseMode = tg.ModeMarkdown
|
||||
reply.ReplyMarkup = helpers.CancelButton(T)
|
||||
reply := tg.NewMessage(msg.Chat.ID, t("reply_del_sticker"))
|
||||
reply.ParseMode = tg.StyleMarkdown
|
||||
reply.ReplyMarkup = utils.CancelButton(t)
|
||||
|
||||
err = db.ChangeUserState(msg.From.ID, models.StateDeleteSticker)
|
||||
err = db.DB.ChangeUserState(msg.From, models.StateDeleteSticker)
|
||||
errors.Check(err)
|
||||
|
||||
if pack {
|
||||
err = db.ChangeUserState(msg.From.ID, models.StateDeletePack)
|
||||
err = db.DB.ChangeUserState(msg.From, models.StateDeletePack)
|
||||
errors.Check(err)
|
||||
|
||||
reply.Text = T("reply_del_pack")
|
||||
reply.Text = t("reply_del_pack")
|
||||
}
|
||||
|
||||
_, err = bot.Bot.SendMessage(reply)
|
||||
|
@ -52,8 +52,8 @@ func Delete(msg *tg.Message, pack bool) {
|
|||
_, err = bot.Bot.SendChatAction(msg.Chat.ID, tg.ActionTyping)
|
||||
errors.Check(err)
|
||||
|
||||
reply = tg.NewMessage(msg.Chat.ID, T("reply_switch_button"))
|
||||
reply.ReplyMarkup = helpers.SwitchButton(T)
|
||||
reply = tg.NewMessage(msg.Chat.ID, t("reply_switch_button"))
|
||||
reply.ReplyMarkup = utils.SwitchButton(t)
|
||||
_, err = bot.Bot.SendMessage(reply)
|
||||
errors.Check(err)
|
||||
}
|
||||
|
|
|
@ -1,39 +1,39 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"github.com/toby3d/MyPackBot/internal/bot"
|
||||
"github.com/toby3d/MyPackBot/internal/db"
|
||||
"github.com/toby3d/MyPackBot/internal/errors"
|
||||
"github.com/toby3d/MyPackBot/internal/helpers"
|
||||
"github.com/toby3d/MyPackBot/internal/i18n"
|
||||
"github.com/toby3d/MyPackBot/internal/models"
|
||||
tg "github.com/toby3d/telegram"
|
||||
"gitlab.com/toby3d/mypackbot/internal/bot"
|
||||
"gitlab.com/toby3d/mypackbot/internal/db"
|
||||
"gitlab.com/toby3d/mypackbot/internal/errors"
|
||||
"gitlab.com/toby3d/mypackbot/internal/i18n"
|
||||
"gitlab.com/toby3d/mypackbot/internal/models"
|
||||
"gitlab.com/toby3d/mypackbot/internal/utils"
|
||||
tg "gitlab.com/toby3d/telegram"
|
||||
)
|
||||
|
||||
// Help just send instructions about bot usage
|
||||
func Help(msg *tg.Message) {
|
||||
T, err := i18n.SwitchTo(msg.From.LanguageCode)
|
||||
t, err := i18n.SwitchTo(msg.From.LanguageCode)
|
||||
errors.Check(err)
|
||||
|
||||
err = db.ChangeUserState(msg.From.ID, models.StateNone)
|
||||
err = db.DB.ChangeUserState(msg.From, models.StateNone)
|
||||
errors.Check(err)
|
||||
|
||||
_, err = bot.Bot.SendChatAction(msg.Chat.ID, tg.ActionTyping)
|
||||
errors.Check(err)
|
||||
|
||||
reply := tg.NewMessage(
|
||||
msg.Chat.ID, T("reply_help", map[string]interface{}{
|
||||
msg.Chat.ID, t("reply_help", map[string]interface{}{
|
||||
"AddStickerCommand": models.CommandAddSticker,
|
||||
"AddPackCommand": models.CommandAddPack,
|
||||
"DeleteStickerCommand": models.CommandDeleteSticker,
|
||||
"DeletePackCommand": models.CommandDeletePack,
|
||||
"ResetCommand": models.CommandReset,
|
||||
"CancelCommand": models.CommandCancel,
|
||||
"Username": bot.Bot.Self.Username,
|
||||
"Username": bot.Bot.Username,
|
||||
}),
|
||||
)
|
||||
reply.ParseMode = tg.ModeMarkdown
|
||||
reply.ReplyMarkup = helpers.MenuKeyboard(T)
|
||||
reply.ParseMode = tg.StyleMarkdown
|
||||
reply.ReplyMarkup = utils.MenuKeyboard(t)
|
||||
|
||||
_, err = bot.Bot.SendMessage(reply)
|
||||
errors.Check(err)
|
||||
|
|
|
@ -1,47 +1,47 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"github.com/toby3d/MyPackBot/internal/bot"
|
||||
"github.com/toby3d/MyPackBot/internal/db"
|
||||
"github.com/toby3d/MyPackBot/internal/errors"
|
||||
"github.com/toby3d/MyPackBot/internal/helpers"
|
||||
"github.com/toby3d/MyPackBot/internal/i18n"
|
||||
"github.com/toby3d/MyPackBot/internal/models"
|
||||
tg "github.com/toby3d/telegram"
|
||||
"gitlab.com/toby3d/mypackbot/internal/bot"
|
||||
"gitlab.com/toby3d/mypackbot/internal/db"
|
||||
"gitlab.com/toby3d/mypackbot/internal/errors"
|
||||
"gitlab.com/toby3d/mypackbot/internal/i18n"
|
||||
"gitlab.com/toby3d/mypackbot/internal/models"
|
||||
"gitlab.com/toby3d/mypackbot/internal/utils"
|
||||
tg "gitlab.com/toby3d/telegram"
|
||||
)
|
||||
|
||||
// Reset prepare user to reset his pack
|
||||
func Reset(msg *tg.Message) {
|
||||
T, err := i18n.SwitchTo(msg.From.LanguageCode)
|
||||
t, err := i18n.SwitchTo(msg.From.LanguageCode)
|
||||
errors.Check(err)
|
||||
|
||||
_, total, err := db.UserStickers(msg.From.ID, 0, "")
|
||||
stickers, err := db.DB.GetUserStickers(msg.From, &tg.InlineQuery{})
|
||||
errors.Check(err)
|
||||
|
||||
_, err = bot.Bot.SendChatAction(msg.Chat.ID, tg.ActionTyping)
|
||||
errors.Check(err)
|
||||
|
||||
if total <= 0 {
|
||||
err = db.ChangeUserState(msg.From.ID, models.StateNone)
|
||||
if len(stickers) <= 0 {
|
||||
err = db.DB.ChangeUserState(msg.From, models.StateNone)
|
||||
errors.Check(err)
|
||||
|
||||
reply := tg.NewMessage(msg.Chat.ID, T("error_already_reset"))
|
||||
reply.ParseMode = tg.ModeMarkdown
|
||||
reply.ReplyMarkup = helpers.MenuKeyboard(T)
|
||||
reply := tg.NewMessage(msg.Chat.ID, t("error_already_reset"))
|
||||
reply.ParseMode = tg.StyleMarkdown
|
||||
reply.ReplyMarkup = utils.MenuKeyboard(t)
|
||||
_, err = bot.Bot.SendMessage(reply)
|
||||
errors.Check(err)
|
||||
return
|
||||
}
|
||||
|
||||
err = db.ChangeUserState(msg.From.ID, models.StateReset)
|
||||
err = db.DB.ChangeUserState(msg.From, models.StateReset)
|
||||
errors.Check(err)
|
||||
|
||||
reply := tg.NewMessage(msg.Chat.ID, T("reply_reset", map[string]interface{}{
|
||||
"KeyPhrase": T("key_phrase"),
|
||||
reply := tg.NewMessage(msg.Chat.ID, t("reply_reset", map[string]interface{}{
|
||||
"KeyPhrase": t("key_phrase"),
|
||||
"CancelCommand": models.CommandCancel,
|
||||
}))
|
||||
reply.ParseMode = tg.ModeMarkdown
|
||||
reply.ReplyMarkup = helpers.CancelButton(T)
|
||||
reply.ParseMode = tg.StyleMarkdown
|
||||
reply.ReplyMarkup = utils.CancelButton(t)
|
||||
_, err = bot.Bot.SendMessage(reply)
|
||||
errors.Check(err)
|
||||
}
|
||||
|
|
|
@ -4,18 +4,18 @@ import (
|
|||
"strings"
|
||||
|
||||
log "github.com/kirillDanshin/dlog"
|
||||
"github.com/toby3d/MyPackBot/internal/bot"
|
||||
"github.com/toby3d/MyPackBot/internal/db"
|
||||
"github.com/toby3d/MyPackBot/internal/errors"
|
||||
"github.com/toby3d/MyPackBot/internal/helpers"
|
||||
"github.com/toby3d/MyPackBot/internal/i18n"
|
||||
"github.com/toby3d/MyPackBot/internal/models"
|
||||
tg "github.com/toby3d/telegram"
|
||||
"gitlab.com/toby3d/mypackbot/internal/bot"
|
||||
"gitlab.com/toby3d/mypackbot/internal/db"
|
||||
"gitlab.com/toby3d/mypackbot/internal/errors"
|
||||
"gitlab.com/toby3d/mypackbot/internal/i18n"
|
||||
"gitlab.com/toby3d/mypackbot/internal/models"
|
||||
"gitlab.com/toby3d/mypackbot/internal/utils"
|
||||
tg "gitlab.com/toby3d/telegram"
|
||||
)
|
||||
|
||||
// Start just send introduction about bot to user
|
||||
func Start(msg *tg.Message) {
|
||||
err := db.ChangeUserState(msg.From.ID, models.StateNone)
|
||||
err := db.DB.ChangeUserState(msg.From, models.StateNone)
|
||||
errors.Check(err)
|
||||
|
||||
_, err = bot.Bot.SendChatAction(msg.Chat.ID, tg.ActionTyping)
|
||||
|
@ -23,24 +23,24 @@ func Start(msg *tg.Message) {
|
|||
|
||||
if msg.HasCommandArgument() {
|
||||
log.Ln("Received a", msg.Command(), "command with", msg.CommandArgument(), "argument")
|
||||
if strings.EqualFold(msg.CommandArgument(), models.CommandHelp) {
|
||||
if strings.EqualFold(msg.CommandArgument(), tg.CommandHelp) {
|
||||
Help(msg)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
T, err := i18n.SwitchTo(msg.From.LanguageCode)
|
||||
t, err := i18n.SwitchTo(msg.From.LanguageCode)
|
||||
errors.Check(err)
|
||||
|
||||
reply := tg.NewMessage(
|
||||
msg.Chat.ID,
|
||||
T("reply_start", map[string]interface{}{
|
||||
"Username": bot.Bot.Self.Username,
|
||||
"ID": bot.Bot.Self.ID,
|
||||
t("reply_start", map[string]interface{}{
|
||||
"Username": bot.Bot.Username,
|
||||
"ID": bot.Bot.ID,
|
||||
}),
|
||||
)
|
||||
reply.ParseMode = tg.ModeMarkdown
|
||||
reply.ReplyMarkup = helpers.MenuKeyboard(T)
|
||||
reply.ParseMode = tg.StyleMarkdown
|
||||
reply.ReplyMarkup = utils.MenuKeyboard(t)
|
||||
|
||||
_, err = bot.Bot.SendMessage(reply)
|
||||
errors.Check(err)
|
||||
|
|
|
@ -1,23 +1,17 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"github.com/olebedev/config"
|
||||
"github.com/toby3d/MyPackBot/internal/errors"
|
||||
)
|
||||
import "github.com/spf13/viper"
|
||||
|
||||
var (
|
||||
// Config is a main object of preloaded configuration YAML
|
||||
Config *config.Config
|
||||
|
||||
// ChannelID is a announcements channel ID
|
||||
ChannelID int64
|
||||
)
|
||||
var Config *viper.Viper
|
||||
|
||||
// Open just open configuration file for parsing some data in other functions
|
||||
func Open(path string) {
|
||||
var err error
|
||||
Config, err = config.ParseYamlFile(path)
|
||||
errors.Check(err)
|
||||
func Open(path string) (*viper.Viper, error) {
|
||||
cfg := viper.New()
|
||||
|
||||
ChannelID = int64(Config.UInt("telegram.channel"))
|
||||
cfg.AddConfigPath(path)
|
||||
cfg.SetConfigName("config")
|
||||
cfg.SetConfigType("yaml")
|
||||
|
||||
err := cfg.ReadInConfig()
|
||||
return cfg, err
|
||||
}
|
||||
|
|
|
@ -5,23 +5,24 @@ import (
|
|||
|
||||
log "github.com/kirillDanshin/dlog"
|
||||
"github.com/tidwall/buntdb"
|
||||
"github.com/toby3d/MyPackBot/internal/models"
|
||||
"gitlab.com/toby3d/mypackbot/internal/models"
|
||||
tg "gitlab.com/toby3d/telegram"
|
||||
)
|
||||
|
||||
// AddSticker add sticker FileID, Emoji and SetName meta for UserID
|
||||
func AddSticker(userID int, setName, fileID, emoji string) (bool, error) {
|
||||
log.Ln("Trying to add", fileID, "sticker from", userID, "user")
|
||||
if setName == "" {
|
||||
setName = models.SetUploaded
|
||||
func (db *DataBase) AddSticker(user *tg.User, sticker *tg.Sticker) (bool, error) {
|
||||
log.Ln("Trying to add", sticker.FileID, "sticker from", user.ID, "user")
|
||||
if sticker.SetName == "" {
|
||||
sticker.SetName = models.SetUploaded
|
||||
}
|
||||
|
||||
var exists bool
|
||||
err := DB.Update(func(tx *buntdb.Tx) error {
|
||||
err := db.Update(func(tx *buntdb.Tx) error {
|
||||
var err error
|
||||
_, exists, err = tx.Set(
|
||||
fmt.Sprint("user:", userID, ":set:", setName, ":sticker:", fileID), // key
|
||||
emoji, // value
|
||||
nil, // options
|
||||
fmt.Sprint("user:", user.ID, ":set:", sticker.SetName, ":sticker:", sticker.FileID), // key
|
||||
sticker.Emoji, // value
|
||||
nil, // options
|
||||
)
|
||||
if err == buntdb.ErrIndexExists {
|
||||
exists = true
|
||||
|
|
|
@ -5,13 +5,14 @@ import (
|
|||
|
||||
log "github.com/kirillDanshin/dlog"
|
||||
"github.com/tidwall/buntdb"
|
||||
tg "gitlab.com/toby3d/telegram"
|
||||
)
|
||||
|
||||
// ChangeUserState change current user state on input state.
|
||||
func ChangeUserState(userID int, state string) error {
|
||||
log.Ln("Trying to change", userID, "state to", state)
|
||||
return DB.Update(func(tx *buntdb.Tx) error {
|
||||
_, _, err := tx.Set(fmt.Sprint("user:", userID, ":state"), state, nil)
|
||||
func (db *DataBase) ChangeUserState(user *tg.User, state string) error {
|
||||
log.Ln("Trying to change", user.ID, "state to", state)
|
||||
return db.Update(func(tx *buntdb.Tx) error {
|
||||
_, _, err := tx.Set(fmt.Sprint("user:", user.ID, ":state"), state, nil)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
|
|
@ -6,43 +6,43 @@ import (
|
|||
|
||||
log "github.com/kirillDanshin/dlog"
|
||||
"github.com/tidwall/buntdb"
|
||||
"github.com/toby3d/MyPackBot/internal/models"
|
||||
"gitlab.com/toby3d/mypackbot/internal/models"
|
||||
tg "gitlab.com/toby3d/telegram"
|
||||
)
|
||||
|
||||
// DeletePack remove all keys for UserID which contains input SetName
|
||||
func DeletePack(userID int, setName string) (bool, error) {
|
||||
log.Ln("Trying to remove all", setName, "sticker from", userID, "user")
|
||||
if setName == "" {
|
||||
setName = models.SetUploaded
|
||||
func (db *DataBase) DeletePack(user *tg.User, sticker *tg.Sticker) (bool, error) {
|
||||
log.Ln("Trying to remove all", sticker.SetName, "sticker from", user.ID, "user")
|
||||
if sticker.SetName == "" {
|
||||
sticker.SetName = models.SetUploaded
|
||||
}
|
||||
|
||||
var fileIDs []string
|
||||
err := DB.View(func(tx *buntdb.Tx) error {
|
||||
var ids []string
|
||||
err := db.View(func(tx *buntdb.Tx) error {
|
||||
return tx.AscendKeys(
|
||||
fmt.Sprint("user:", userID, ":set:", setName, ":*"),
|
||||
fmt.Sprint("user:", user.ID, ":set:", sticker.SetName, ":*"),
|
||||
func(key, val string) bool {
|
||||
keys := strings.Split(key, ":")
|
||||
fileIDs = append(fileIDs, keys[5])
|
||||
ids = append(ids, keys[5])
|
||||
return true
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
if len(fileIDs) == 0 {
|
||||
if len(ids) == 0 {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
for _, fileID := range fileIDs {
|
||||
for _, id := range ids {
|
||||
var notExist bool
|
||||
notExist, err = DeleteSticker(userID, setName, fileID)
|
||||
notExist, err = db.DeleteSticker(user, &tg.Sticker{FileID: id})
|
||||
if err != nil {
|
||||
return notExist, err
|
||||
}
|
||||
}
|
||||
|
||||
switch err {
|
||||
case buntdb.ErrNotFound:
|
||||
log.Ln(userID, "not found")
|
||||
if err == buntdb.ErrNotFound {
|
||||
log.Ln(user.ID, "not found")
|
||||
return true, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -5,25 +5,25 @@ import (
|
|||
|
||||
log "github.com/kirillDanshin/dlog"
|
||||
"github.com/tidwall/buntdb"
|
||||
"github.com/toby3d/MyPackBot/internal/models"
|
||||
"gitlab.com/toby3d/mypackbot/internal/models"
|
||||
tg "gitlab.com/toby3d/telegram"
|
||||
)
|
||||
|
||||
// DeleteSticker just remove specified sticker key from database.
|
||||
func DeleteSticker(userID int, setName, fileID string) (bool, error) {
|
||||
log.Ln("Trying to remove", fileID, "sticker from", userID, "user")
|
||||
if setName == "" {
|
||||
setName = models.SetUploaded
|
||||
func (db *DataBase) DeleteSticker(user *tg.User, sticker *tg.Sticker) (bool, error) {
|
||||
log.Ln("Trying to remove", sticker.FileID, "sticker from", user.ID, "user")
|
||||
if sticker.SetName == "" {
|
||||
sticker.SetName = models.SetUploaded
|
||||
}
|
||||
|
||||
err := DB.Update(func(tx *buntdb.Tx) error {
|
||||
err := db.Update(func(tx *buntdb.Tx) error {
|
||||
_, err := tx.Delete(
|
||||
fmt.Sprint("user:", userID, ":set:", setName, ":sticker:", fileID),
|
||||
fmt.Sprint("user:", user.ID, ":set:", sticker.SetName, ":sticker:", sticker.FileID),
|
||||
)
|
||||
return err
|
||||
})
|
||||
|
||||
if err == buntdb.ErrNotFound {
|
||||
log.Ln(userID, "not found, create new one")
|
||||
log.Ln(user.ID, "not found, create new one")
|
||||
return true, nil
|
||||
}
|
||||
|
||||
|
|
29
internal/db/get_user_state.go
Normal file
29
internal/db/get_user_state.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
log "github.com/kirillDanshin/dlog"
|
||||
"github.com/tidwall/buntdb"
|
||||
"gitlab.com/toby3d/mypackbot/internal/models"
|
||||
tg "gitlab.com/toby3d/telegram"
|
||||
)
|
||||
|
||||
// UserState return current state for UserID
|
||||
func (db *DataBase) GetUserState(user *tg.User) (string, error) {
|
||||
log.Ln("Trying to get", user.ID, "state")
|
||||
var state string
|
||||
err := db.View(func(tx *buntdb.Tx) error {
|
||||
var err error
|
||||
state, err = tx.Get(fmt.Sprint("user:", user.ID, ":state"))
|
||||
return err
|
||||
})
|
||||
if err == buntdb.ErrNotFound {
|
||||
log.Ln(user.ID, "not found, create new one")
|
||||
if err = db.ChangeUserState(user, models.StateNone); err != nil {
|
||||
return state, err
|
||||
}
|
||||
}
|
||||
|
||||
return state, err
|
||||
}
|
55
internal/db/get_user_stickers.go
Normal file
55
internal/db/get_user_stickers.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
log "github.com/kirillDanshin/dlog"
|
||||
"github.com/tidwall/buntdb"
|
||||
tg "gitlab.com/toby3d/telegram"
|
||||
)
|
||||
|
||||
// GetUserStickers return array of saved stickers for input UserID and his total count
|
||||
func (db *DataBase) GetUserStickers(user *tg.User, query *tg.InlineQuery) ([]string, error) {
|
||||
log.Ln("Trying to get", user.ID, "stickers")
|
||||
var i int
|
||||
var stickers []string
|
||||
offset, _ := strconv.Atoi(query.Offset)
|
||||
offset *= 50
|
||||
|
||||
err := db.View(func(tx *buntdb.Tx) error {
|
||||
return tx.AscendKeys(
|
||||
fmt.Sprint("user:", user.ID, ":set:*"), // index
|
||||
func(key, val string) bool { // iterator
|
||||
subKeys := strings.Split(key, ":")
|
||||
if subKeys[1] != strconv.Itoa(user.ID) {
|
||||
return true
|
||||
}
|
||||
|
||||
if len(stickers) == 50 {
|
||||
return false
|
||||
}
|
||||
|
||||
i++
|
||||
if i < offset {
|
||||
return true
|
||||
}
|
||||
|
||||
if query.Query != "" && !strings.Contains(query.Query, val) {
|
||||
return true
|
||||
}
|
||||
|
||||
stickers = append(stickers, subKeys[5])
|
||||
return true
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
if err == buntdb.ErrNotFound {
|
||||
log.Ln("Not found stickers")
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return stickers, err
|
||||
}
|
|
@ -8,10 +8,10 @@ import (
|
|||
"github.com/tidwall/buntdb"
|
||||
)
|
||||
|
||||
// Users return array of all available UserID in database
|
||||
func Users() ([]int, error) {
|
||||
// GetUsers return array of all available UserID in database
|
||||
func (db *DataBase) GetUsers() ([]int, error) {
|
||||
var users []int
|
||||
err := DB.View(func(tx *buntdb.Tx) error {
|
||||
err := db.View(func(tx *buntdb.Tx) error {
|
||||
return tx.AscendKeys(
|
||||
"user:*:state",
|
||||
func(key, val string) bool {
|
|
@ -3,18 +3,16 @@ package db
|
|||
import (
|
||||
log "github.com/kirillDanshin/dlog"
|
||||
"github.com/tidwall/buntdb"
|
||||
"github.com/toby3d/MyPackBot/internal/errors"
|
||||
)
|
||||
|
||||
type DataBase struct{ *buntdb.DB }
|
||||
|
||||
// DB is a main object of current database connection
|
||||
var DB *buntdb.DB
|
||||
var DB *DataBase
|
||||
|
||||
// Open just open connection to database for work
|
||||
func Open(path string) {
|
||||
func Open(path string) (*DataBase, error) {
|
||||
log.Ln("Open database file...")
|
||||
go func() {
|
||||
var err error
|
||||
DB, err = buntdb.Open(path)
|
||||
errors.Check(err)
|
||||
}()
|
||||
db, err := buntdb.Open(path)
|
||||
return &DataBase{db}, err
|
||||
}
|
||||
|
|
|
@ -7,18 +7,19 @@ import (
|
|||
|
||||
log "github.com/kirillDanshin/dlog"
|
||||
"github.com/tidwall/buntdb"
|
||||
tg "gitlab.com/toby3d/telegram"
|
||||
)
|
||||
|
||||
// ResetUser just drop out all stickers keys for input UserID
|
||||
func ResetUser(userID int) error {
|
||||
log.Ln("Trying reset all stickers of", userID, "user")
|
||||
return DB.Update(func(tx *buntdb.Tx) error {
|
||||
func (db *DataBase) ResetUser(user *tg.User) error {
|
||||
log.Ln("Trying reset all stickers of", user.ID, "user")
|
||||
return db.Update(func(tx *buntdb.Tx) error {
|
||||
var keys []string
|
||||
if err := tx.AscendKeys(
|
||||
fmt.Sprint("user:", userID, ":set:*"), // index
|
||||
fmt.Sprint("user:", user.ID, ":set:*"), // index
|
||||
func(key, val string) bool { // iterator
|
||||
subKeys := strings.Split(key, ":")
|
||||
if subKeys[1] == strconv.Itoa(userID) {
|
||||
if subKeys[1] == strconv.Itoa(user.ID) {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
return true
|
||||
|
|
|
@ -5,23 +5,23 @@ import (
|
|||
|
||||
log "github.com/kirillDanshin/dlog"
|
||||
"github.com/tidwall/buntdb"
|
||||
"github.com/toby3d/MyPackBot/internal/models"
|
||||
"gitlab.com/toby3d/mypackbot/internal/models"
|
||||
tg "gitlab.com/toby3d/telegram"
|
||||
)
|
||||
|
||||
// UserState return current state for UserID
|
||||
func UserState(userID int) (string, error) {
|
||||
log.Ln("Trying to get", userID, "state")
|
||||
func (db *DataBase) UserState(usr *tg.User) (string, error) {
|
||||
log.Ln("Trying to get", usr.ID, "state")
|
||||
var state string
|
||||
err := DB.View(func(tx *buntdb.Tx) error {
|
||||
var err error
|
||||
state, err = tx.Get(fmt.Sprint("user:", userID, ":state"))
|
||||
state, err = tx.Get(fmt.Sprint("user:", usr.ID, ":state"))
|
||||
return err
|
||||
})
|
||||
|
||||
switch err {
|
||||
case buntdb.ErrNotFound:
|
||||
log.Ln(userID, "not found, create new one")
|
||||
if err = ChangeUserState(userID, models.StateNone); err != nil {
|
||||
if err == buntdb.ErrNotFound {
|
||||
log.Ln(usr.ID, "not found, create new one")
|
||||