go-discord-bot / bot / handlers.go
handlers.go
Raw
package bot

import (
	"errors"
	"fmt"

	"github.com/mr1hm/go-discord-bot/internal/tft"
)

type BotHandlerError struct {
	Err error
}

func (e *BotHandlerError) Error() string {
	return fmt.Sprintf("[ BOT ] Error: %v", e.Err)
}

func Fun(username string) string {
	msg := ""
	switch username {
	case "defter":
		msg = "The best of the best. The creator and its destroyer."
	case "lavem":
		msg = "Commonly referred to as the ultimate predator. Stealth is its strength and the creature's ability to use it to its advantage is unmatched by any other. "
	case "nukleas":
		msg = "Referred to as the Persian Powerhouse by many legends, this creature is known to adapt very well in all habitats and dominate all living species swiftly. Do not let this creature's height fool you."
	case "Ugoff":
		msg = "Often thought of as an ancestor to the panda. Serves no real purpose to the world and is of the lowest kind of its own ancestry. Does not contribute anything :)"
	case "sammypoon":
		msg = "A curious creature with a distaste for fish. While studies are scarce due to its innate ability to hide and play dead, studies have shown this creature can be found in tropical areas.\nProminent features include: Abnormally large head."
	default:
		msg = "That username doesn't exist."
		break
	}
	return msg
}

// ListUsers returns a string formatted list of available users
func ListUsers() (string, error) {
	instruction := "Please find a username and reply with `get {username} matches`..."
	example := "\nFor example: `get defter`"

	users := "\n`[ "
	len_data := len(tft.UserData.Data)
	for _, AMD := range tft.UserData.Data {
		if len_data > 1 {
			users = users + AMD.Account.GameName + ", "
		} else {
			users = users + AMD.Account.GameName
		}

		len_data--
	}

	users = users + " ]`"
	return instruction + users + example, nil
}

// GetPlayerAMDByGameName returns the PUUID when a matching `GameName` is found. Else, it returns an error
func GetPlayerAMDByGameName(gameName string) (tft.AccountMatchData, error) {
	for _, amd := range tft.UserData.Data {
		if amd.Account.GameName == gameName {
			return amd, nil
		}
	}

	return tft.AccountMatchData{}, &BotHandlerError{
		Err: errors.New(fmt.Sprintf("Couldn't find player game name: %v", gameName)),
	}
}

// GetUserTFTData finds the appropriate `matchinfo` by PUUID
func GetUserTFTData(TFT_gamename, secondaryCmd string, matchID ...string) ([]string, string, error) {
	// `id` can be the player `GameName` or `MatchID`
	var user_matches_data []string
	var user_match_info string

	playerAMD, err := GetPlayerAMDByGameName(TFT_gamename)
	if err != nil {
		fmt.Println(err.Error())
	}

	switch secondaryCmd {
	case "matches":
		user_matches_data = playerAMD.Matches
		break
	case "matchinfo":
		if len(matchID) == 0 {
			return []string{}, "", &BotHandlerError{
				Err: errors.New(fmt.Sprintf("Please supply a MatchID from the matches list. You can type in `get {username} matches` to find the last 5 match IDs from the games you played")),
			}
		}

		var game_time_played, augments, gold_left, players_eliminated, placement, total_damage_to_players, units, traits string
		player_md := playerAMD.MatchDetails[matchID[0]]

		game_time_played = "__Match Time Played__" + "```" + fmt.Sprintf("%v", player_md.GameTimePlayed.String()) + "```"
		user_match_info += game_time_played

		augments = "__Augments__"
		for _, a := range player_md.Data.Augments {
			augments += "```" + a + "```"
		}
		user_match_info += augments

		gold_left = "__GoldLeft__" + "```" + fmt.Sprintf("%v", player_md.Data.GoldLeft) + "```"
		user_match_info += gold_left

		players_eliminated = "__Players Eliminated__" + "```" + fmt.Sprintf("%v", player_md.Data.PlayersEliminated) + "```"
		user_match_info += players_eliminated

		placement = "__Placement__" + "```" + fmt.Sprintf("%v", player_md.Data.Placement) + "```"
		user_match_info += placement

		total_damage_to_players = "__Total Damage To Players__" + "```" + fmt.Sprintf("%v", player_md.Data.TotalDmgToPlayers) + "```"
		user_match_info += total_damage_to_players

		len_traits := len(player_md.Data.Traits)
		if len_traits > 0 {
			traits = "__Traits__"
			for i, trait := range player_md.Data.Traits {
				if i == 0 && (i == len_traits-1) {
					traits += "```[ " + trait.Name + " " + fmt.Sprintf("%v", trait.NumUnits) + "/" + fmt.Sprintf("%v", trait.TierTotal) + " ]```"
				} else {
					if i == 0 {
						traits += "```[ " + trait.Name + " " + fmt.Sprintf("%v", trait.NumUnits) + "/" + fmt.Sprintf("%v", trait.TierTotal)
					} else {
						if i == (len_traits - 1) {
							traits += trait.Name + " " + fmt.Sprintf("%v", trait.NumUnits) + "/" + fmt.Sprintf("%v", trait.TierTotal) + " ]```"
						} else {
							if i == (len_traits - 2) {
								traits += ", " + trait.Name + " " + fmt.Sprintf("%v", trait.NumUnits) + "/" + fmt.Sprintf("%v", trait.TierTotal) + ", "
							} else {
								traits += ", " + trait.Name + " " + fmt.Sprintf("%v", trait.NumUnits) + "/" + fmt.Sprintf("%v", trait.TierTotal)
							}
						}
					}
				}
			}
			user_match_info += traits
		}

		units = "__Units__"
		for _, unit := range player_md.Data.Units {
			len_items := len(unit.ItemNames)
			items := ""
			if len_items > 0 {
				units += "```" + unit.CharacterID + ":"
				for i, item := range unit.ItemNames {
					// If there's only 1 item
					if i == 0 && i == (len_items-1) {
						items += "[ " + item + " ]```"
						break
					} else {
						// If there's more than 1 item
						if i == 0 {
							items += "[ " + item
						} else {
							if i == (len_items - 1) {
								items += ", " + item + " ]```"
							} else {
								items += ", " + item
							}
						}
					}
				}
				units += items
			} else {
				units += "```" + unit.CharacterID + "```"
			}
		}
		user_match_info += units
		return []string{}, user_match_info, nil
	default:
		fmt.Printf("Unknown command given while trying to retrieve match data: %v\n", secondaryCmd)
		break
	}

	if secondaryCmd == "matches" {
		return user_matches_data, "", nil
	} else if secondaryCmd == "matchinfo" {
		return []string{}, user_match_info, nil
	}

	return []string{}, "", nil
}