diff --git a/commands/nicks.go b/commands/nicks.go index 29cfe01..d079533 100644 --- a/commands/nicks.go +++ b/commands/nicks.go @@ -4,12 +4,19 @@ import ( "errors" "fmt" "log" + "strings" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" "github.com/pandacrew-net/diosteama/database" "github.com/pandacrew-net/diosteama/format" ) +var SacredNames = map[string]string{ + "El Fary": "Solo existe un auténtico Fary y no eres tu.", + "una taza": "Yo soy la tetera.", + "Steve Ballmer": "DEVELOPERS DEVELOPERS DEVELOPERS!!!", +} + func soy(update tgbotapi.Update, bot *tgbotapi.BotAPI, argv []string) { var reply string if len(argv) != 1 { @@ -33,29 +40,67 @@ func soy(update tgbotapi.Update, bot *tgbotapi.BotAPI, argv []string) { bot.Send(msg) } +func quienesNick(nick string) (string, error) { + var reply = "" + _, username, err := database.TGUserFromNick(nick) + if err == nil { + if username == "" { + // Exists, but doesn't have a username + reply = fmt.Sprintf("El panda anteriormente conocido como %s ahora es
anonymous", nick) + } else { + reply = fmt.Sprintf("@%s es el panda anteriormente conocido como %s", username, nick) + } + } + + return reply, err +} + +func quienesUsername(username string) (string, error) { + var reply = "" + if username[0] == '@' { + username = username[1:] + } + + nick, err := database.NickFromTGUserName(username) + if err == nil { + reply = fmt.Sprintf("@%s es el panda anteriormente conocido como %s", username, nick) + } + + return reply, err +} + +func quienesTGUser(user *tgbotapi.User) (string, error) { + var reply = "" + nick, err := database.NickFromTGUser(user) + if err == nil { + reply = fmt.Sprintf("@%s es el panda anteriormente conocido como %s", user.UserName, nick) + } + return reply, err +} + func quienes(update tgbotapi.Update, bot *tgbotapi.BotAPI, argv []string) { var reply string + var err error + var exists bool - log.Printf("%v\n", argv) - - if len(argv) != 1 { + if update.Message.ReplyToMessage != nil { + reply, _ = quienesTGUser(update.Message.ReplyToMessage.From) + } else if len(argv) < 1 { reply = fmt.Sprintf("¿Por quien preguntas?") } else { - term := argv[0] - username, err := database.TGUserFromNick(term) - if err == nil { - reply = fmt.Sprintf("@%s es el panda anteriormente conocido como %s", username, term) - } else { - nick, err := database.NickFromTGUserName(term) - if err == nil { - reply = fmt.Sprintf("@%s es el panda anteriormente conocido como %s", term, nick) - } else { - log.Printf("Algo no fue bien: %s", err) - reply = fmt.Sprintf("No sé de quien me hablas.") + term := strings.Join(argv, " ") + if reply, exists = SacredNames[term]; !exists { + reply, err = quienesNick(term) + if err != nil { + reply, err = quienesUsername(term) } } } + if reply == "" { + reply = fmt.Sprintf("No sé de quien me hablas.") + } + msg := tgbotapi.NewMessage(update.Message.Chat.ID, reply) msg.ParseMode = "html" bot.Send(msg) diff --git a/database/nicks.go b/database/nicks.go index cf736cb..92c9e3f 100644 --- a/database/nicks.go +++ b/database/nicks.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "log" + "strings" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" pgx "github.com/jackc/pgx/v4" @@ -66,27 +67,28 @@ func NickFromTGUserName(username string) (string, error) { } // TGUserFromNick return the Telegram user ID from a given nick -func TGUserFromNick(nick string) (string, error) { +func TGUserFromNick(nick string) (int, string, error) { var TGUser string + var TGUserID int - query := fmt.Sprintf(`SELECT tg_username FROM %s WHERE nick = $1;`, UsersTable) - err := pool.QueryRow(context.Background(), query, nick).Scan(&TGUser) + query := fmt.Sprintf(`SELECT tg_id, tg_username FROM %s WHERE lower(nick) = $1;`, UsersTable) + err := pool.QueryRow(context.Background(), query, strings.ToLower(nick)).Scan(&TGUserID, &TGUser) if err != nil { - return "", fmt.Errorf("Error finding user for %s: %w", nick, err) + return -1, "", fmt.Errorf("Error finding user for %s: %w", nick, err) } if nick == "" { - return "", fmt.Errorf("%s: %w", nick, ErrPandaNotFound) + return -1, "", fmt.Errorf("%s: %w", nick, ErrPandaNotFound) } - return TGUser, nil + return TGUserID, TGUser, nil } // UserNickIsAssociated Checks if the user or the nick has a previous association func UserNickIsAssociated(user *tgbotapi.User, nick string) error { var count int - query := fmt.Sprintf(`SELECT count(*) from %s WHERE nick = $1 or tg_id = $2`, UsersTable) - err := pool.QueryRow(context.Background(), query, nick, user.ID).Scan(&count) + query := fmt.Sprintf(`SELECT count(*) from %s WHERE lower(nick) = $1 or tg_id = $2`, UsersTable) + err := pool.QueryRow(context.Background(), query, strings.ToLower(nick), user.ID).Scan(&count) if err != nil { return fmt.Errorf("Something happened on DB: %s", err) @@ -123,8 +125,8 @@ func SetNick(user *tgbotapi.User, nick string) error { func deleteNick(nick string) error { // Im scared some ppl sending an * as username, that I will check before var count int - query := fmt.Sprintf(`SELECT count(*) FROM %s WHERE nick = $1`, UsersTable) - err := pool.QueryRow(context.Background(), query, nick).Scan(&count) + query := fmt.Sprintf(`SELECT count(*) FROM %s WHERE lower(nick) = $1`, UsersTable) + err := pool.QueryRow(context.Background(), query, strings.ToLower(nick)).Scan(&count) if count > 1 { return fmt.Errorf("You son of a bitch. Don't even try to hack me") } else if count == 0 {