Skip to content

Commit

Permalink
feat: added finals as a new game or voting
Browse files Browse the repository at this point in the history
  • Loading branch information
d.pikaliuk committed Feb 23, 2024
1 parent e9f3f7c commit 8f2df66
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cmd/app/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func Run(ctx context.Context, args run.Args) error {

botApi, err := tgbotapi.NewBotAPI(cfg.TelegramBot.Token)
if err != nil {
return fmt.Errorf("new tg bot api err: %w", err)
return fmt.Errorf("new tg bot api token: [%s] err: %w", cfg.TelegramBot.Token, err)
}

u := tgbotapi.NewUpdate(0)
Expand Down
5 changes: 3 additions & 2 deletions internal/bot/listener/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import (
)

const (
CSGOContext = "csgo"
PUBGContext = "pubg"
CSGOContext = "csgo"
PUBGContext = "pubg"
FinalsContext = "finals"

PollContext = "poll"
UsersContext = "users"
Expand Down
11 changes: 1 addition & 10 deletions internal/bot/listener/cmd-help-handler.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package listener

import (
"github.com/haski007/insta-bot/pkg/emoji"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

Expand All @@ -15,13 +13,6 @@ func (rcv *InstaBotService) cmdStartHandler(update tgbotapi.Update) {
}

func (rcv *InstaBotService) sendStartInfo(chatID int64) error {
message := `Hello, I am a bot that can help you make your chat meme life a lot easier and funnier.
For the best user experience give me please administrator rights ` + emoji.FaceWinking + "\n" +
"you can use such commands for creating csgo polls:\n" +
"/reg\\_csgo\\_players {username\\_1} {username\\_2} ... - to register users that will be tagged before poll to play CS GO\n" +
"/purge\\_csgo\\_players - to delete all csgo players at this chat\n" +
"/lets\\_play - to offer to play CS GO (creates poll to vote)\n" +
"/sum {count} - to summarize last {count > 0} messages\n" +
"/purge\\_history - to delete all messages from this chat history\n"
message := `Нащо ви мене знову розбудили ?! 😡`
return rcv.SendMessage(chatID, message)
}
78 changes: 78 additions & 0 deletions internal/bot/listener/cmd-lets-play-finals.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package listener

import (
"fmt"
"strings"
"time"

"github.com/sirupsen/logrus"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

func (rcv *InstaBotService) cmdLetsPlayFinalsHandler(update tgbotapi.Update) {
chatID := update.Message.Chat.ID

args := strings.Fields(update.Message.CommandArguments())

var timeToPlay string
if len(args) != 0 {
_, err := time.Parse("15:04", args[0])
if err == nil {
timeToPlay = args[0]
}
}

// ---> Check if chat is registered not to spam in usual chats
chatRegistered, err := rcv.storage.ChatExists(FinalsContext, chatID)
if err != nil {
rcv.SendError(chatID, ErrInternalServerError)
rcv.log.WithError(err).Error("[cmdLetsPlayFinalsHandler] get chat exists")
return
}
if !chatRegistered {
rcv.SendError(chatID, ErrNoFinalsPlayers)
return
}

members, err := rcv.storage.GetChatMembers(FinalsContext, chatID)
if err != nil {
rcv.SendError(chatID, ErrInternalServerError)
rcv.log.WithError(err).Error("[cmdLetsPlayFinalsHandler] get chat members")
return
}

if len(members) == 0 {
rcv.SendError(chatID, ErrNoFinalsPlayers)
return
}

var message string
for _, m := range members {
message += fmt.Sprintf("@%s ", m)
}

var voteCaption = "Галасаваніє!"

if timeToPlay != "" {
voteCaption = fmt.Sprintf("%s The Finals в %s?", voteCaption, timeToPlay)
message += fmt.Sprintf("\nХто буде в The Finals в %s? Галасуєм!", timeToPlay)
} else {
message += "\nХто буде в The Finals? Галасуєм!"
}
message = strings.ReplaceAll(message, "_", "\\_")

if err := rcv.SendMessageWithoutMarkdown(chatID, message); err != nil {
logrus.WithError(err).Println("[cmdLetsPlayFinalsHandler] send message to chat")
}

options := []string{
"(+) Звичайно, я справжній козак!",
"(-) Нііі, ні я не ту кохав, не ті слова..., в общем лох я!",
}

_, err = rcv.CreatePoll(chatID, voteCaption, false, options...)
if err != nil {
logrus.WithError(err).Println("[cmdLetsPlayFinalsHandler] create poll to chat")
}
}
23 changes: 23 additions & 0 deletions internal/bot/listener/cmd-purge-finals.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package listener

import (
"github.com/haski007/insta-bot/pkg/emoji"
"github.com/sirupsen/logrus"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

func (rcv *InstaBotService) cmdPurgeFinalsPlayersHandler(update tgbotapi.Update) {
chatID := update.Message.Chat.ID

if err := rcv.storage.DeleteChat(FinalsContext, chatID); err != nil {
rcv.SendError(chatID, ErrInternalServerError)
rcv.log.WithError(err).Error("[cmdPurgeFinalsPlayersHandler] delete finals chat with members")
return
}

message := "Successfully deleted stopped to listen The Finals triggers "
if err := rcv.SendMessage(chatID, message+emoji.Basket); err != nil {
logrus.WithError(err).Println("[cmdPurgeFinalsPlayersHandler] send message to chat")
}
}
32 changes: 32 additions & 0 deletions internal/bot/listener/cmd-reg-finals-players.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package listener

import (
"fmt"
"strings"

"github.com/haski007/insta-bot/pkg/emoji"
"github.com/sirupsen/logrus"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

func (rcv *InstaBotService) cmdRegFinalsPlayersHandler(update tgbotapi.Update) {
chatID := update.Message.Chat.ID

members := strings.Fields(update.Message.CommandArguments())
if len(members) == 0 {
rcv.SendError(chatID, ErrNoArguments)
return
}

if err := rcv.storage.AddChatWithMembers(FinalsContext, chatID, members); err != nil {
rcv.SendError(chatID, ErrInternalServerError)
rcv.log.WithError(err).Error("[cmdRegFinalsPlayersHandler] add chat with members")
return
}

message := fmt.Sprintf("Added listener of CSGO triggers for *%d* users ", len(members))
if err := rcv.SendMessage(chatID, message+emoji.Check); err != nil {
logrus.WithError(err).Println("[cmdRegFinalsPlayersHandler] send message to chat")
}
}
2 changes: 2 additions & 0 deletions internal/bot/listener/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ const (

ErrNoPUBGPlayers = "There are no PUBG players registered in this chat!" +
"\nTo register enter /reg_pubg_players {username_1} {username_2} ..."
ErrNoFinalsPlayers = "There are no The Finals players registered in this chat!" +
"\nTo register enter /reg_finals_players {username_1} {username_2} ..."
)
3 changes: 2 additions & 1 deletion internal/bot/listener/msg-media-trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"strings"
"time"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/haski007/insta-bot/internal/bot/model"
"github.com/haski007/insta-bot/pkg/file"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)

const (
Expand Down
10 changes: 9 additions & 1 deletion internal/bot/listener/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,22 @@ func (rcv *InstaBotService) StartPool() error {
case command == "lets_play":
go rcv.cmdLetsPlayHandler(update)

// CSGO addon ^)
// PUBG addon ^)
case command == "reg_pubg_players":
go rcv.cmdRegPUBGPlayersHandler(update)
case command == "purge_pubg_players":
go rcv.cmdPurgePUBGPlayersHandler(update)
case command == "lets_play_pubg":
go rcv.cmdLetsPlayPUBGHandler(update)

// Finals addon ^)
case command == "reg_finals_players":
go rcv.cmdRegFinalsPlayersHandler(update)
case command == "purge_finals_players":
go rcv.cmdPurgeFinalsPlayersHandler(update)
case command == "lets_play_finals":
go rcv.cmdLetsPlayFinalsHandler(update)

// Users
case command == "set_email":
go rcv.cmdSetEmailHandler(update)
Expand Down

0 comments on commit 8f2df66

Please sign in to comment.