-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cbd3eab
commit 97c1187
Showing
9 changed files
with
116 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,44 @@ | ||
package flags | ||
|
||
import ( | ||
"fmt" | ||
"context" | ||
"errors" | ||
"github.com/gotd/td/telegram/message" | ||
"github.com/gotd/td/tg" | ||
"github.com/koenigskraut/piktagbot/database" | ||
"github.com/koenigskraut/piktagbot/util" | ||
"strings" | ||
) | ||
|
||
func Add(m *tg.Message, u *database.User) string { | ||
func Add(ctx context.Context, m *tg.Message, u *database.User, answer *message.RequestBuilder) error { | ||
if strings.HasPrefix(m.Message, "/cancel") { | ||
database.DB.Model(&u).Select("Flag", "FlagData").Updates(database.User{Flag: "", FlagData: ""}) | ||
return "Действие отменено" | ||
} else { | ||
if sticker, ok := util.StickerFromMedia(m.Media); ok { | ||
// if there is a sticker, check if there is such a tag attached to it, | ||
// if not — add one | ||
sTag := database.StickerTag{ | ||
User: u.UserID, | ||
StickerID: sticker.ID, | ||
Tag: u.FlagData, | ||
} | ||
answer, _ := sTag.CheckAndAdd() | ||
if !strings.HasPrefix(answer, "Что") { | ||
database.DB.Model(u).Select("Flag", "FlagData").Updates(database.User{Flag: "", FlagData: ""}) | ||
} | ||
return answer | ||
} else { | ||
return fmt.Sprintf("В сообщении нет стикера! Отправьте мне стикер для "+ | ||
"тега \"%s\" или отмените действие командой /cancel", | ||
u.FlagData) | ||
u.Flag, u.FlagData = NoFlag, "" | ||
if err := u.Save(); err != nil { | ||
return err | ||
} | ||
_, err := answer.Text(ctx, "Действие отменено") | ||
return err | ||
} | ||
sticker, ok := util.StickerFromMedia(m.Media) | ||
if !ok { | ||
_, err := answer.Text(ctx, "В сообщении нет стикера! Отправьте мне стикер для удаления тегов или "+ | ||
"отмените действие командой /cancel") | ||
return err | ||
} | ||
// if there is a sticker, check if there is such a tag attached to it, | ||
// if not — add one | ||
sTag := database.StickerTag{ | ||
User: u.UserID, | ||
StickerID: sticker.ID, | ||
Tag: u.FlagData, | ||
} | ||
text, errDB := sTag.CheckAndAdd() | ||
if errDB == nil { | ||
u.Flag, u.FlagData = NoFlag, "" | ||
if err := u.Save(); err != nil { | ||
return err | ||
} | ||
} | ||
_, errMsg := answer.Text(ctx, text) | ||
return errors.Join(errDB, errMsg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package flags | ||
|
||
const ( | ||
NoFlag = iota | ||
RemoveTag | ||
AddTag | ||
CheckTag | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,45 @@ | ||
package flags | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/gotd/td/telegram/message" | ||
"github.com/gotd/td/telegram/message/html" | ||
"github.com/gotd/td/tg" | ||
"github.com/koenigskraut/piktagbot/database" | ||
"github.com/koenigskraut/piktagbot/util" | ||
"log" | ||
"strings" | ||
) | ||
|
||
func Check(m *tg.Message, u *database.User) string { | ||
func Check(ctx context.Context, m *tg.Message, u *database.User, answer *message.RequestBuilder) error { | ||
if strings.HasPrefix(m.Message, "/cancel") { | ||
database.DB.Model(&u).Select("Flag", "FlagData").Updates(database.User{Flag: "", FlagData: ""}) | ||
return "Действие отменено" | ||
} else { | ||
if sticker, ok := util.StickerFromMedia(m.Media); ok { | ||
tags, err := (&database.StickerTag{User: u.UserID, StickerID: sticker.ID}).GetAllForUser() | ||
if err != nil { | ||
log.Println(err) | ||
return "Что-то пошло не так, попробуйте ещё раз!" | ||
} | ||
if len(tags) == 0 { | ||
return "У этого стикера нет ни одного тега!" | ||
} | ||
return util.CheckStickerResponse(tags) | ||
} else { | ||
return "В сообщении нет стикера! Отправьте мне стикер для " + | ||
"просмотра тегов или отмените действие командой /cancel" | ||
u.Flag, u.FlagData = NoFlag, "" | ||
if err := u.Save(); err != nil { | ||
return err | ||
} | ||
_, err := answer.Text(ctx, "Действие отменено") | ||
return err | ||
} | ||
sticker, ok := util.StickerFromMedia(m.Media) | ||
if !ok { | ||
_, err := answer.Text(ctx, "В сообщении нет стикера! Отправьте мне стикер для "+ | ||
"просмотра тегов или отмените действие командой /cancel") | ||
return err | ||
} | ||
tags, err := (&database.StickerTag{User: u.UserID, StickerID: sticker.ID}).GetAllForUser() | ||
if err != nil { | ||
_, errMsg := answer.Text(ctx, "Что-то пошло не так, попробуйте ещё раз!") | ||
return errors.Join(err, errMsg) | ||
} | ||
if len(tags) == 0 { | ||
_, err := answer.Text(ctx, "У этого стикера нет ни одного тега!") | ||
return err | ||
} | ||
u.Flag, u.FlagData = NoFlag, "" | ||
if err := u.Save(); err != nil { | ||
_, errMsg := answer.Text(ctx, "Что-то пошло не так, попробуйте ещё раз!") | ||
return errors.Join(err, errMsg) | ||
} | ||
_, err = answer.StyledText(ctx, html.String(nil, util.CheckStickerResponse(tags))) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,45 @@ | ||
package flags | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/gotd/td/telegram/message" | ||
"github.com/gotd/td/tg" | ||
"github.com/koenigskraut/piktagbot/callback" | ||
"github.com/koenigskraut/piktagbot/database" | ||
"github.com/koenigskraut/piktagbot/util" | ||
"log" | ||
"strings" | ||
) | ||
|
||
func Remove(m *tg.Message, u *database.User) (string, tg.ReplyMarkupClass) { | ||
func Remove(ctx context.Context, m *tg.Message, u *database.User, answer *message.RequestBuilder) error { | ||
if strings.HasPrefix(m.Message, "/cancel") { | ||
database.DB.Model(&u).Select("Flag", "FlagData").Updates(database.User{Flag: "", FlagData: ""}) | ||
return "Действие отменено", nil | ||
} else { | ||
if sticker, ok := util.StickerFromMedia(m.Media); ok { | ||
// if there is a sticker, let's build a keyboard for it with | ||
// tags to delete | ||
markup, err := callback.BuildMarkup(sticker.ID, u.UserID, 0) | ||
if err != nil { | ||
if errors.Is(err, callback.MarkupError) { | ||
return "У этого стикера нет ни одного тега!", nil | ||
} | ||
log.Println(err) | ||
return "Неизвестная ошибка", nil | ||
} | ||
database.DB.Model(u).Select("Flag", "FlagData").Updates(database.User{Flag: "", FlagData: ""}) | ||
return "Выберите тег для удаления:", markup | ||
} else { | ||
return "В сообщении нет стикера! Отправьте мне стикер для " + | ||
"удаления тегов или отмените действие командой /cancel", nil | ||
u.Flag, u.FlagData = NoFlag, "" | ||
if err := u.Save(); err != nil { | ||
return err | ||
} | ||
_, err := answer.Text(ctx, "Действие отменено") | ||
return err | ||
} | ||
sticker, ok := util.StickerFromMedia(m.Media) | ||
if !ok { | ||
_, err := answer.Text(ctx, "В сообщении нет стикера! Отправьте мне стикер для удаления тегов или "+ | ||
"отмените действие командой /cancel") | ||
return err | ||
} | ||
// if there is a sticker, let's build a keyboard for it with tags to delete | ||
markup, errMarkup := callback.BuildMarkup(sticker.ID, u.UserID, 0) | ||
if errMarkup != nil { | ||
msg := "Неизвестная ошибка" | ||
if errors.Is(errMarkup, callback.MarkupError) { | ||
msg = "У этого стикера нет ни одного тега!" | ||
} | ||
_, errMsg := answer.Text(ctx, msg) | ||
return errors.Join(errMarkup, errMsg) | ||
} | ||
u.Flag, u.FlagData = NoFlag, "" | ||
if err := u.Save(); err != nil { | ||
return err | ||
} | ||
_, err := answer.Markup(markup).Text(ctx, "Выберите тег для удаления:") | ||
return err | ||
} |