package handler import ( tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" "fmt" "gogs.veloe.link/toomanysugar/GoEthemineTelegramBot/internal/database" log "gogs.veloe.link/toomanysugar/GoEthemineTelegramBot/internal/logger" ) // whata var wrongFormatErr error = fmt.Errorf("Wrong format!") // returns wallet in format 0x%.40s or error if format is wrong func parseWallet(walletStr string) (wallet string, err error) { if len(walletStr) == 40 { return "0x" + walletStr, nil } if len(walletStr) == 42 && walletStr[0] == '0' && walletStr[1] == 'x' { return walletStr, nil } return "", wrongFormatErr } func (ah ActionHandler) SetWalletHandler(chatId int64, params []string) []tgbotapi.MessageConfig { if len(params) != 1 { return []tgbotapi.MessageConfig{tgbotapi.NewMessage(chatId, "usage: /setwallet ")} } walletStr := params[0] // Check if user registered var user database.User ah.dbconn.GormDb. Model(&database.User{}). Where(&database.User{ChatId: chatId}).FirstOrInit(&user) if user == (database.User{}) { return []tgbotapi.MessageConfig{ tgbotapi.NewMessage(chatId, "You are not registered! Type /start first!")} } wallet, err := parseWallet(walletStr) if err != nil { if err == wrongFormatErr { return []tgbotapi.MessageConfig{ tgbotapi.NewMessage(chatId, "wallet must be in format:\n" + "[0x]****************************************!")} } } user.Wallet = wallet err = ah.dbconn.GormDb.Save(&user).Error if err != nil { log.LogError.Printf("failed querying user: %s", err.Error()) return []tgbotapi.MessageConfig{ tgbotapi.NewMessage(chatId, "An exception occurred while executing this command.")} } log.LogInfo.Printf("Updated wallet of user %d to %s\n", chatId, wallet) return []tgbotapi.MessageConfig{ tgbotapi.NewMessage(chatId, fmt.Sprintf("%s set!", wallet))} }