From d38b982733c79e55f4c02635bb41760defa411e9 Mon Sep 17 00:00:00 2001 From: Ash Keel Date: Mon, 6 Feb 2023 10:36:00 +0100 Subject: [PATCH] fix: clarify no contribution to goals --- loyalty/manager.go | 8 ++++---- loyalty/twitch-bot.go | 20 +++++++++++++------- twitch/client.go | 11 +++++------ 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/loyalty/manager.go b/loyalty/manager.go index f1e2aa3..d7e74f0 100644 --- a/loyalty/manager.go +++ b/loyalty/manager.go @@ -313,7 +313,7 @@ func (m *Manager) ContributeGoal(goal Goal, user string, points int64) error { return ErrGoalNotFound } -func (m *Manager) PerformContribution(goal Goal, user string, points int64) error { +func (m *Manager) PerformContribution(goal Goal, user string, points int64) (int64, error) { // Get user balance balance := m.GetPoints(user) @@ -324,7 +324,7 @@ func (m *Manager) PerformContribution(goal Goal, user string, points int64) erro // Check if goal was reached already if goal.Contributed >= goal.TotalGoal { - return ErrGoalAlreadyReached + return 0, ErrGoalAlreadyReached } // If remaining points are lower than what user is contributing, only take what's needed @@ -335,11 +335,11 @@ func (m *Manager) PerformContribution(goal Goal, user string, points int64) erro // Remove points from user if err := m.TakePoints(map[string]int64{user: points}); err != nil { - return err + return 0, err } // Add points to goal - return m.ContributeGoal(goal, user, points) + return points, m.ContributeGoal(goal, user, points) } func (m *Manager) GetReward(id string) Reward { diff --git a/loyalty/twitch-bot.go b/loyalty/twitch-bot.go index 0d8a2b1..fac01fe 100644 --- a/loyalty/twitch-bot.go +++ b/loyalty/twitch-bot.go @@ -280,7 +280,7 @@ func (m *Manager) cmdContributeGoal(bot *twitch.Bot, message irc.PrivateMessage) // Do we not have any goal we can contribute to? Hooray I guess? if goalIndex < 0 { if hasGoals { - bot.Client.Say(message.Channel, fmt.Sprintf("%s: All active community goals have been reached already! ShowOfHands", message.User.DisplayName)) + bot.Client.Say(message.Channel, fmt.Sprintf("%s: All active community goals have been reached already! NewRecord", message.User.DisplayName)) } else { bot.Client.Say(message.Channel, fmt.Sprintf("%s: There are no active community goals right now :(!", message.User.DisplayName)) } @@ -290,13 +290,13 @@ func (m *Manager) cmdContributeGoal(bot *twitch.Bot, message irc.PrivateMessage) // Parse parameters if provided parts := strings.Fields(message.Message) if len(parts) > 1 { - newpoints, err := strconv.ParseInt(parts[1], 10, 64) + newPoints, err := strconv.ParseInt(parts[1], 10, 64) if err == nil { - if newpoints <= 0 { + if newPoints <= 0 { bot.Client.Say(message.Channel, fmt.Sprintf("Nice try %s SoBayed", message.User.DisplayName)) return } - points = newpoints + points = newPoints } if len(parts) > 2 { found := false @@ -330,18 +330,24 @@ func (m *Manager) cmdContributeGoal(bot *twitch.Bot, message irc.PrivateMessage) } // Add points to goal - if err := m.PerformContribution(selectedGoal, message.User.Name, points); err != nil { + points, err := m.PerformContribution(selectedGoal, message.User.Name, points) + if err != nil { m.logger.Error("error while contributing to goal", zap.Error(err)) return } + if points == 0 { + bot.Client.Say(message.Channel, fmt.Sprintf("%s: Sorry but you're broke", message.User.DisplayName)) + return + } + selectedGoal = m.Goals.Get()[goalIndex] config := m.Config.Get() newRemaining := selectedGoal.TotalGoal - selectedGoal.Contributed - bot.Client.Say(message.Channel, fmt.Sprintf("ShowOfHands %s contributed %d %s to \"%s\"!! Only %d %s left!", message.User.DisplayName, points, config.Currency, selectedGoal.Name, newRemaining, config.Currency)) + bot.Client.Say(message.Channel, fmt.Sprintf("NewRecord %s contributed %d %s to \"%s\"!! Only %d %s left!", message.User.DisplayName, points, config.Currency, selectedGoal.Name, newRemaining, config.Currency)) // Check if goal was reached! // TODO Replace this with sub from loyalty system or something? if newRemaining <= 0 { - bot.Client.Say(message.Channel, fmt.Sprintf("ShowOfHands The community goal \"%s\" was reached! ShowOfHands", selectedGoal.Name)) + bot.Client.Say(message.Channel, fmt.Sprintf("FallWinning The community goal \"%s\" was reached! FallWinning", selectedGoal.Name)) } } diff --git a/twitch/client.go b/twitch/client.go index 4cf9b2b..768fe47 100644 --- a/twitch/client.go +++ b/twitch/client.go @@ -212,13 +212,12 @@ func newClient(config Config, db *database.LocalDBClient, server *http.Server, l users, err := userClient.GetUsers(&helix.UsersParams{}) if err != nil { client.logger.Error("failed looking up user", zap.Error(err)) + } else if len(users.Data.Users) < 1 { + client.logger.Error("no users found, please authenticate in Twitch configuration -> Events") + } else { + client.User = users.Data.Users[0] + go client.connectWebsocket(userClient) } - if len(users.Data.Users) < 1 { - client.logger.Error("no users found") - } - client.User = users.Data.Users[0] - - go client.connectWebsocket(userClient) } else { client.logger.Warn("twitch user not identified, this will break most features") }