mirror of
https://github.com/cjdenio/jia.git
synced 2024-11-22 07:33:39 +00:00
Add basic validation
This commit is contained in:
parent
dbb0a90706
commit
c103e9e4fc
1 changed files with 56 additions and 0 deletions
|
@ -1,6 +1,11 @@
|
|||
package jia
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
||||
"github.com/slack-go/slack"
|
||||
"github.com/slack-go/slack/slackevents"
|
||||
)
|
||||
|
@ -13,5 +18,56 @@ func HandleInnerEvent(slackClient *slack.Client, innerEvent *slackevents.EventsA
|
|||
}
|
||||
}
|
||||
|
||||
// TODO: Persist to Redis
|
||||
var (
|
||||
lastValidNumber = 0
|
||||
lastSender = ""
|
||||
)
|
||||
|
||||
func onMessage(slackClient *slack.Client, event *slackevents.MessageEvent) {
|
||||
// Ignore messages that aren't in the target channel, or are non-user messages
|
||||
if event.Channel != jiaConfig.ChannelID || event.User == "USLACKBOT" || event.User == "" {
|
||||
return
|
||||
}
|
||||
|
||||
// Attempt to extract a positive number from the beginning of a string
|
||||
countPattern := regexp.MustCompile(`^\d+`)
|
||||
matchedNumber, err := strconv.Atoi(countPattern.FindString(event.Text))
|
||||
log.Println(matchedNumber)
|
||||
|
||||
// Ignore messages that don't have numbers.
|
||||
if err != nil {
|
||||
log.Println("Failed to retrieve number, skipping…")
|
||||
return
|
||||
}
|
||||
|
||||
// Reject if sender also sent the previous number.
|
||||
if event.User == lastSender {
|
||||
slackClient.AddReaction("bangbang", slack.ItemRef{
|
||||
Channel: event.Channel,
|
||||
Timestamp: event.TimeStamp,
|
||||
})
|
||||
slackClient.PostEphemeral(event.Channel, event.User, slack.MsgOptionText(
|
||||
"You counted consecutively! That’s not allowed.", false))
|
||||
return
|
||||
}
|
||||
|
||||
// Ignore numbers that aren't in order.
|
||||
if matchedNumber != lastValidNumber+1 {
|
||||
slackClient.AddReaction("bangbang", slack.ItemRef{
|
||||
Channel: event.Channel,
|
||||
Timestamp: event.TimeStamp,
|
||||
})
|
||||
slackClient.PostEphemeral(event.Channel, event.User, slack.MsgOptionText(
|
||||
fmt.Sprintf("You counted incorrectly! The next valid number is supposed to be *%d*.", lastValidNumber+1), false))
|
||||
return
|
||||
}
|
||||
|
||||
// Finally!
|
||||
lastValidNumber = matchedNumber
|
||||
lastSender = event.User
|
||||
slackClient.AddReaction("+1", slack.ItemRef{
|
||||
Channel: event.Channel,
|
||||
Timestamp: event.TimeStamp,
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue