@@ -3,8 +3,12 @@ package app
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.echol.cn/loser/st/server/global"
|
||||
"git.echol.cn/loser/st/server/model/app"
|
||||
@@ -236,14 +240,74 @@ func (s *RegexScriptService) ExecuteScript(script *app.RegexScript, text string,
|
||||
// substituteMacros 替换宏变量
|
||||
func (s *RegexScriptService) substituteMacros(text string, userName string, charName string) string {
|
||||
result := text
|
||||
|
||||
// 保存原始文本
|
||||
result = strings.ReplaceAll(result, "{{original}}", text)
|
||||
|
||||
// 用户名变量
|
||||
if userName != "" {
|
||||
result = strings.ReplaceAll(result, "{{user}}", userName)
|
||||
result = strings.ReplaceAll(result, "{{User}}", userName)
|
||||
}
|
||||
|
||||
// 角色名变量
|
||||
if charName != "" {
|
||||
result = strings.ReplaceAll(result, "{{char}}", charName)
|
||||
result = strings.ReplaceAll(result, "{{Char}}", charName)
|
||||
}
|
||||
|
||||
// 时间变量
|
||||
now := time.Now()
|
||||
result = strings.ReplaceAll(result, "{{time}}", now.Format("15:04:05"))
|
||||
result = strings.ReplaceAll(result, "{{date}}", now.Format("2006-01-02"))
|
||||
result = strings.ReplaceAll(result, "{{datetime}}", now.Format("2006-01-02 15:04:05"))
|
||||
result = strings.ReplaceAll(result, "{{timestamp}}", fmt.Sprintf("%d", now.Unix()))
|
||||
result = strings.ReplaceAll(result, "{{time_12h}}", now.Format("03:04:05 PM"))
|
||||
result = strings.ReplaceAll(result, "{{date_short}}", now.Format("01/02/06"))
|
||||
result = strings.ReplaceAll(result, "{{weekday}}", now.Weekday().String())
|
||||
result = strings.ReplaceAll(result, "{{month}}", now.Month().String())
|
||||
result = strings.ReplaceAll(result, "{{year}}", fmt.Sprintf("%d", now.Year()))
|
||||
|
||||
// 随机数变量
|
||||
result = regexp.MustCompile(`\{\{random:(\d+)-(\d+)\}\}`).ReplaceAllStringFunc(result, func(match string) string {
|
||||
re := regexp.MustCompile(`\{\{random:(\d+)-(\d+)\}\}`)
|
||||
matches := re.FindStringSubmatch(match)
|
||||
if len(matches) == 3 {
|
||||
min, _ := strconv.Atoi(matches[1])
|
||||
max, _ := strconv.Atoi(matches[2])
|
||||
if max > min {
|
||||
return fmt.Sprintf("%d", rand.Intn(max-min+1)+min)
|
||||
}
|
||||
}
|
||||
return match
|
||||
})
|
||||
|
||||
// 简单随机数 {{random}}
|
||||
result = regexp.MustCompile(`\{\{random\}\}`).ReplaceAllStringFunc(result, func(match string) string {
|
||||
return fmt.Sprintf("%d", rand.Intn(100))
|
||||
})
|
||||
|
||||
// 随机选择 {{pick:option1|option2|option3}}
|
||||
result = regexp.MustCompile(`\{\{pick:([^}]+)\}\}`).ReplaceAllStringFunc(result, func(match string) string {
|
||||
re := regexp.MustCompile(`\{\{pick:([^}]+)\}\}`)
|
||||
matches := re.FindStringSubmatch(match)
|
||||
if len(matches) == 2 {
|
||||
options := strings.Split(matches[1], "|")
|
||||
if len(options) > 0 {
|
||||
return options[rand.Intn(len(options))]
|
||||
}
|
||||
}
|
||||
return match
|
||||
})
|
||||
|
||||
// 换行符变量
|
||||
result = strings.ReplaceAll(result, "{{newline}}", "\n")
|
||||
result = strings.ReplaceAll(result, "{{tab}}", "\t")
|
||||
result = strings.ReplaceAll(result, "{{space}}", " ")
|
||||
|
||||
// 空值变量
|
||||
result = strings.ReplaceAll(result, "{{empty}}", "")
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user