Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 59 additions & 14 deletions commands/nicks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 <pre>anonymous</pre>", 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)
Expand Down
22 changes: 12 additions & 10 deletions database/nicks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down