~exprez135/go-hypixel

6d442e856ee2aed1b7f5c522b7c4b53f6396b016 — Nate Ijams 3 years ago 99b73ee v0.0.15
Change online list functions and add bed defence command.
2 files changed, 114 insertions(+), 69 deletions(-)

M engine/discord.go
M engine/tracking.go
M engine/discord.go => engine/discord.go +43 -37
@@ 7,6 7,7 @@ import (
	"strings"
	"syscall"
	"time"
	"math/rand"

	"git.sr.ht/~exprez135/go-hypixel/api"



@@ 68,11 69,11 @@ func StartDiscord() {
	router.On("trackguild", func(ctx *exrouter.Context) {
		trackGuild(ctx)
	}).Desc("adds a guild to the tracking list. You will get notifications when members get on or off Hypixel")
/*
	/*
		router.On("trackfriends", func(ctx *exrouter.Context) {
			trackFriends(ctx)
		}).Desc("adds a player's friends to the tracking list. You will get notifications when their friends get on or off Hypixel")
*/
	*/
	router.On("removeplayer", func(ctx *exrouter.Context) {
		removePlayer(ctx)
	}).Desc("adds a player to the tracking list. You will get notifications when they get on or off Hypixel")


@@ 93,6 94,10 @@ func StartDiscord() {
		listOnline(ctx)
	}).Desc("lists all the tracked players, guilds, and friends who are online.")

	router.On("bed", func(ctx *exrouter.Context) {
		bedDefence(ctx)
	}).Desc("chooses an online player to make the bed defence.")

	router.On("info", func(ctx *exrouter.Context) {
		ctx.Reply("**Info**\n---\nWritten by exprez135. Licensed under the AGPLv3. See https://git.sr.ht/~exprez135/go-hypixel.")
	}).Desc("responds with info about the program")


@@ 398,42 403,10 @@ func listOnline(ctx *exrouter.Context) {
		ctx.Reply("Unable to get tracked list. Report this error!")
	}

	var message = "**Guild Members Online:**\nNote: at the moment, we only support listing the status of each member in a tracked guilt.\n"

	var listOfPlayers []string
	var message = "**Guild Members Online:**\n"

	// Iterate over each guild in discord
	// Get all UUIDs from the guilds in list

	for _, g := range guilds {
		// Get list of UUIDs in the guild

		guildList, err := GetGuildList(g)

		if err != nil {
			log.Println("Error in GetGuildList within listOnline:", err)
			ctx.Reply("Error code 1000. Report this.")
			return
		}

		// Append guildList to listOfPlayers
		listOfPlayers = append(listOfPlayers, guildList...)
	}

	// RemoveDuplicates(listOfPlayers)

	newListOfPlayers := RemoveDuplicates(listOfPlayers)

	// Reference tracked bucket, if online: add to onlineList

	var onlineList []string

	for _, uuid := range newListOfPlayers {
		if GetCachedPlayerStatus(uuid) == true {
			onlineList = append(onlineList, uuid)
		}

	}
	// Fetch list of online UUIDs in the monitored guilds
	onlineList := ListOnlinePlayers(guilds)

	// Get actual usernames for each UUID in onlineList



@@ 457,6 430,39 @@ func listOnline(ctx *exrouter.Context) {
	ctx.Reply(message)
}

func bedDefence(ctx *exrouter.Context) {
	log.Println("STARTING bedDefence() run...")
	// players, friends, guilds, err
	_, _, guilds, err := GetTrackedList("discord", ctx.Msg.GuildID)

	if err != nil {
		log.Println("discord.listTracked:", err)
		ctx.Reply("Unable to get tracked list. Report this error!")
	}

	// Fetch list of online UUIDs in the monitored guilds
	onlineList := ListOnlinePlayers(guilds)

	// Choose a random UUID
	randomIndex := rand.Intn(len(onlineList))

	var chosenUUID = onlineList[randomIndex]

	// Get actual username for the chosen one
	chosenOne, err := api.UUIDToUsername(chosenUUID)

	if err != nil {
		log.Println("Error code 1002")
		chosenOne = chosenUUID
	}

	var message = "Who will defend the bed?\nThe Chosen One is ... "

	message += chosenOne

	ctx.Reply(message)
}

func setChannel(ctx *exrouter.Context) {
	err := SetOrganizationChannel(platform, ctx.Msg.GuildID, ctx.Msg.ChannelID)
	if err != nil {

M engine/tracking.go => engine/tracking.go +71 -32
@@ 36,17 36,17 @@ func UpdateTrackedPlayerList() error {

			// take players and append to trackedPlayers
			trackedPlayers = append(trackedPlayers, obj.Players...)
/*
			// for each friends GetList then append
			for _, f := range obj.Friends {
				fList, err := GetFriendsList(f)
				if err != nil {
					log.Println("UpdateTrackedPlayerList ~ range org.Friends ~ GetFriendsList():", err)
					continue
			/*
				// for each friends GetList then append
				for _, f := range obj.Friends {
					fList, err := GetFriendsList(f)
					if err != nil {
						log.Println("UpdateTrackedPlayerList ~ range org.Friends ~ GetFriendsList():", err)
						continue
					}
					trackedPlayers = append(trackedPlayers, fList...)
				}
				trackedPlayers = append(trackedPlayers, fList...)
			}
*/
			*/
			// for each guild GetList then append
			for _, g := range obj.Guilds {
				gList, err := GetGuildList(g)


@@ 177,32 177,32 @@ func SendStatusNotifications(uuid string, newStatus bool) error {
					return nil
				}
			}
/*
			log.Println("Starting timer to time the GetFriendsList part of notifications")
			start := time.Now()
			log.Println("NOTIFICATIONS ISSUE MIGHT HAPPEN BELOW.")
			// playerFriendList
			log.Println("tracking.SendStatusNotifications: about to run GetFriendsList(uuid)")
			playerFriendList, err := GetFriendsList(uuid)
			if err != nil {
				log.Println("Could not get friends list:", err)
			}
			/*
				log.Println("Starting timer to time the GetFriendsList part of notifications")
				start := time.Now()
				log.Println("NOTIFICATIONS ISSUE MIGHT HAPPEN BELOW.")
				// playerFriendList
				log.Println("tracking.SendStatusNotifications: about to run GetFriendsList(uuid)")
				playerFriendList, err := GetFriendsList(uuid)
				if err != nil {
					log.Println("Could not get friends list:", err)
				}

			log.Println("tracking.SendStatusNotifications: about to check if player is in a friend list")
			// Check if one of the player's friends is on the org's player list
			for _, f := range obj.Friends {
				for _, p := range playerFriendList {
					if f == p {
						// Notify
						go Notify(obj.Platform, obj.DefaultChannel, uuid, newStatus)
						log.Println("Send NOTIFICATION :)")
						return nil
				log.Println("tracking.SendStatusNotifications: about to check if player is in a friend list")
				// Check if one of the player's friends is on the org's player list
				for _, f := range obj.Friends {
					for _, p := range playerFriendList {
						if f == p {
							// Notify
							go Notify(obj.Platform, obj.DefaultChannel, uuid, newStatus)
							log.Println("Send NOTIFICATION :)")
							return nil
						}
					}
				}
			}

			log.Println("TIME SINCE start timer:", time.Since(start))
*/
				log.Println("TIME SINCE start timer:", time.Since(start))
			*/
			return nil

		})


@@ 229,7 229,46 @@ func GetCachedPlayerStatus(uuid string) bool {
	})

	return result
}

// Returns a string array of the UUIDs of each online player.
// Currently limited to guild members only!
func ListOnlinePlayers(guilds []string) []string {
	var listOfPlayers []string

	// Iterate over each guild in discord
	// Get all UUIDs from the guilds in list

	for _, g := range guilds {
		// Get list of UUIDs in the guild

		guildList, err := GetGuildList(g)

		if err != nil {
			log.Println("Error in GetGuildList within listOnline:", err)
			break
		}

		// Append guildList to listOfPlayers
		listOfPlayers = append(listOfPlayers, guildList...)
	}

	// RemoveDuplicates(listOfPlayers)

	newListOfPlayers := RemoveDuplicates(listOfPlayers)

	// Reference tracked bucket, if online: add to onlineList

	var onlineList []string

	for _, uuid := range newListOfPlayers {
		if GetCachedPlayerStatus(uuid) == true {
			onlineList = append(onlineList, uuid)
		}

	}

	return onlineList
}

// RemoveDuplicates takes a slice of strings and removes any duplicate values. Returns a slice of strings.