commit 7707b4ff8fe36ad65edd458c9d26efcff548c279
parent 8796e4160dc82139cae346511ea0ae4b495e6b45
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Sat, 31 Dec 2022 17:53:14 -0800
rand command
Diffstat:
2 files changed, 40 insertions(+), 0 deletions(-)
diff --git a/pkg/web/handlers/api/v1/handlers.go b/pkg/web/handlers/api/v1/handlers.go
@@ -53,6 +53,7 @@ var forceCaptchaRgx = regexp.MustCompile(`^/(?:captcha) ` + optAtGUser)
var unkickRgx = regexp.MustCompile(`^/(?:unkick|uk) ` + optAtGUser)
var hellbanRgx = regexp.MustCompile(`^/(?:hellban|hb) ` + optAtGUser)
var unhellbanRgx = regexp.MustCompile(`^/(?:unhellban|uhb) ` + optAtGUser)
+var randRgx = regexp.MustCompile(`^/rand (-?\d+) (-?\d+)$`)
var tokenRgx = regexp.MustCompile(`^/token (\d{1,2})$`)
var snippetRgx = regexp.MustCompile(`!\w{1,20}`)
var tagRgx = regexp.MustCompile(`@(` + userOr0 + `)`)
diff --git a/pkg/web/handlers/api/v1/slashInterceptor.go b/pkg/web/handlers/api/v1/slashInterceptor.go
@@ -75,6 +75,7 @@ func handleUserCmd(c *Command) (handled bool) {
handleSha256Cmd(c) ||
handleSha512Cmd(c) ||
handleDiceCmd(c) ||
+ handleRandCmd(c) ||
handleChoiceCmd(c) ||
handleSuccessCmd(c) ||
handleErrorCmd(c)
@@ -398,6 +399,44 @@ func handleDiceCmd(c *Command) (handled bool) {
return
}
+func handleRandCmd(c *Command) (handled bool) {
+ if strings.HasPrefix(c.message, "/rand") {
+ min := 1
+ max := 6
+ var dice int
+ if m := randRgx.FindStringSubmatch(c.message); len(m) == 3 {
+ var err error
+ min, err = strconv.Atoi(m[1])
+ if err != nil {
+ c.err = err
+ return true
+ }
+ max, err = strconv.Atoi(m[2])
+ if err != nil {
+ c.err = err
+ return true
+ }
+ if max <= min {
+ c.err = errors.New("max must be greater than min")
+ return true
+ }
+ } else if c.message != "/rand" {
+ c.err = errors.New("invalid /rand command")
+ return true
+ }
+ dice = utils.RandInt(min, max)
+ raw := fmt.Sprintf(`rolling dice for @%s ... "%d"`, c.authUser.Username, dice)
+ msg := fmt.Sprintf(`rolling dice for @%s ... "<span style="color: white;">%d</span>"`, c.authUser.Username, dice)
+ msg, _ = colorifyTaggedUsers(msg, database.GetUsersByUsername)
+ go func() {
+ time.Sleep(time.Second)
+ c.zeroPublicMsg(raw, msg)
+ }()
+ return true
+ }
+ return
+}
+
func handleChoiceCmd(c *Command) (handled bool) {
if strings.HasPrefix(c.message, "/choice ") {
tmp := strings.TrimPrefix(c.message, "/choice ")