~exprez135/go-hypixel

f5d033baa2cfc67085e56ff48f66f922accf8c1e — Nate Ijams 3 years ago cceb43c v0.0.11
Add !online command. Listing online guild members.
2 files changed, 90 insertions(+), 4 deletions(-)

M engine/discord.go
M engine/tracking.go
M engine/discord.go => engine/discord.go +71 -1
@@ 89,6 89,10 @@ func StartDiscord() {
		listTracked(ctx)
	}).Desc("lists all the players, guilds, and friend lists you are tracking.")

	router.On("online", func(ctx *exrouter.Context) {
		listOnline(ctx)
	}).Desc("lists all the tracked players, guilds, and friends who are online.")

	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")


@@ 119,7 123,6 @@ func StartDiscord() {
	// Register guildCreate as a callback for the guildCreate events.
	Dsession.AddHandler(GuildCreate)


	// Open the Discord session
	err = Dsession.Open()
	if err != nil {


@@ 383,6 386,73 @@ func listTracked(ctx *exrouter.Context) {
	ctx.Reply(message)
}

func listOnline(ctx *exrouter.Context) {
	players, friends, 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!")
	}

	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

	// 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)
		}

	}

	// Get actual usernames for each UUID in onlineList

	var userOnlineList []string

	for _, uuid := range onlineList {

		username, err := api.UUIDToUsername(uuid)
		if err != nil {
			log.Println("Error code 1001, report this!:", err)
			// will cause us to send notification with uuid instead of username
			username = uuid
		}
		userOnlineList = append(userOnlineList, username)
	}
	// Print list of usernames

	userOnlineListString := strings.Join(userOnlineList, ", ")
	message += userOnlineListString

	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 +19 -3
@@ 63,7 63,7 @@ func UpdateTrackedPlayerList() error {
	})

	// De-duplicate trackedPlayers
	trackedPlayersNarrowed := removeDuplicates(trackedPlayers)
	trackedPlayersNarrowed := RemoveDuplicates(trackedPlayers)
	log.Println("~ ~ trackedPlayersNarrowed is:", trackedPlayersNarrowed)

	// Iterate through trackedPlayers, get API status, add "uuid:status" to BoltDB


@@ 213,8 213,24 @@ func SendStatusNotifications(uuid string, newStatus bool) error {

}

// removeDuplicates takes a slice of strings and removes any duplicate values. Returns a slice of strings.
func removeDuplicates(elements []string) []string {
// TODO add error checking
func GetCachedPlayerStatus(uuid string) bool {
	var result bool

	Iron.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte("tracked"))
		v := b.Get([]byte(uuid))

		result = strconv.FormatBool(v)
		return nil
	})

	return result

}

// RemoveDuplicates takes a slice of strings and removes any duplicate values. Returns a slice of strings.
func RemoveDuplicates(elements []string) []string {
	// Use map to record duplicates as we find them.
	encountered := map[string]bool{}
	result := []string{}