Skip to content

Commit

Permalink
fix(config): 修复部分配置项的保存不生效问题
Browse files Browse the repository at this point in the history
  • Loading branch information
JustAnotherID committed Oct 20, 2024
1 parent c4fee59 commit 0f64253
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 136 deletions.
41 changes: 21 additions & 20 deletions api/ban.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,21 @@ func banConfigSet(c echo.Context) error {
v.ThresholdBan = 200
}

myDice.Config.BanList.BanBehaviorRefuseReply = v.BanBehaviorRefuseReply
myDice.Config.BanList.BanBehaviorRefuseInvite = v.BanBehaviorRefuseInvite
myDice.Config.BanList.BanBehaviorQuitLastPlace = v.BanBehaviorQuitLastPlace
myDice.Config.BanList.BanBehaviorQuitPlaceImmediately = v.BanBehaviorQuitPlaceImmediately
myDice.Config.BanList.BanBehaviorQuitIfAdmin = v.BanBehaviorQuitIfAdmin
myDice.Config.BanList.ScoreReducePerMinute = v.ScoreReducePerMinute
myDice.Config.BanList.ThresholdWarn = v.ThresholdWarn
myDice.Config.BanList.ThresholdBan = v.ThresholdBan
myDice.Config.BanList.ScoreGroupMuted = v.ScoreGroupMuted
myDice.Config.BanList.ScoreGroupKicked = v.ScoreGroupKicked
myDice.Config.BanList.ScoreTooManyCommand = v.ScoreTooManyCommand

myDice.Config.BanList.JointScorePercentOfGroup = v.JointScorePercentOfGroup
myDice.Config.BanList.JointScorePercentOfInviter = v.JointScorePercentOfInviter
config := &myDice.Config
config.BanList.BanBehaviorRefuseReply = v.BanBehaviorRefuseReply
config.BanList.BanBehaviorRefuseInvite = v.BanBehaviorRefuseInvite
config.BanList.BanBehaviorQuitLastPlace = v.BanBehaviorQuitLastPlace
config.BanList.BanBehaviorQuitPlaceImmediately = v.BanBehaviorQuitPlaceImmediately
config.BanList.BanBehaviorQuitIfAdmin = v.BanBehaviorQuitIfAdmin
config.BanList.ScoreReducePerMinute = v.ScoreReducePerMinute
config.BanList.ThresholdWarn = v.ThresholdWarn
config.BanList.ThresholdBan = v.ThresholdBan
config.BanList.ScoreGroupMuted = v.ScoreGroupMuted
config.BanList.ScoreGroupKicked = v.ScoreGroupKicked
config.BanList.ScoreTooManyCommand = v.ScoreTooManyCommand

config.BanList.JointScorePercentOfGroup = v.JointScorePercentOfGroup
config.BanList.JointScorePercentOfInviter = v.JointScorePercentOfInviter
myDice.MarkModified()

return c.JSON(http.StatusOK, myDice.Config.BanList)
Expand All @@ -75,7 +76,7 @@ func banMapDeleteOne(c echo.Context) error {
if err != nil {
return c.String(430, err.Error())
}
myDice.Config.BanList.DeleteByID(myDice, v.ID)
(&myDice.Config).BanList.DeleteByID(myDice, v.ID)
return c.JSON(http.StatusOK, nil)
}

Expand All @@ -102,7 +103,7 @@ func banMapAddOne(c echo.Context) error {
platform := strings.Replace(prefix, "-Group", "", 1)
for _, i := range myDice.ImSession.EndPoints {
if i.Platform == platform && i.Enable {
v2 := myDice.Config.BanList.AddScoreBase(v.ID, score, "海豹后台", reason, &dice.MsgContext{Dice: myDice, EndPoint: i})
v2 := (&myDice.Config).BanList.AddScoreBase(v.ID, score, "海豹后台", reason, &dice.MsgContext{Dice: myDice, EndPoint: i})
if v2 != nil {
if v.Name != "" {
v2.Name = v.Name
Expand All @@ -112,7 +113,7 @@ func banMapAddOne(c echo.Context) error {
}
}
if v.Rank == dice.BanRankTrusted {
myDice.Config.BanList.SetTrustByID(v.ID, "海豹后台", "骰主后台设置")
(&myDice.Config).BanList.SetTrustByID(v.ID, "海豹后台", "骰主后台设置")
}

return c.JSON(http.StatusOK, nil)
Expand All @@ -132,7 +133,7 @@ func banMapAddOne(c echo.Context) error {
// return c.String(430, err.Error())
// }
//
// myDice.Config.BanList.LoadMapFromJSON(v.data)
// (&myDice.Config).BanList.LoadMapFromJSON(v.data)
// return c.JSON(http.StatusOK, nil)
//}

Expand Down Expand Up @@ -199,9 +200,9 @@ func banImport(c echo.Context) error {
}
}
item.Reasons = newReasons
myDice.Config.BanList.Map.Store(item.ID, item)
(&myDice.Config).BanList.Map.Store(item.ID, item)
}
myDice.Config.BanList.SaveChanged(myDice)
(&myDice.Config).BanList.SaveChanged(myDice)

return Success(&c, Response{})
}
38 changes: 21 additions & 17 deletions api/censor.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ func censorRestart(c echo.Context) error {
}

myDice.NewCensorManager()
myDice.Config.EnableCensor = true
(&myDice.Config).EnableCensor = true
myDice.MarkModified()

return Success(&c, Response{
"enable": myDice.Config.EnableCensor,
Expand All @@ -61,7 +62,8 @@ func censorStop(c echo.Context) error {
return err
}

myDice.Config.EnableCensor = false
(&myDice.Config).EnableCensor = false
myDice.MarkModified()
_ = myDice.CensorManager.DB.Close()
myDice.CensorManager = nil

Expand All @@ -80,17 +82,18 @@ func censorGetStatus(c echo.Context) error {
}

func censorGetConfig(c echo.Context) error {
config := myDice.Config
levelConfig := map[string]LevelConfig{
"notice": getLevelConfig(censor.Notice, myDice.Config.CensorThresholds, myDice.Config.CensorHandlers, myDice.Config.CensorScores),
"caution": getLevelConfig(censor.Caution, myDice.Config.CensorThresholds, myDice.Config.CensorHandlers, myDice.Config.CensorScores),
"warning": getLevelConfig(censor.Warning, myDice.Config.CensorThresholds, myDice.Config.CensorHandlers, myDice.Config.CensorScores),
"danger": getLevelConfig(censor.Danger, myDice.Config.CensorThresholds, myDice.Config.CensorHandlers, myDice.Config.CensorScores),
"notice": getLevelConfig(censor.Notice, config.CensorThresholds, config.CensorHandlers, config.CensorScores),
"caution": getLevelConfig(censor.Caution, config.CensorThresholds, config.CensorHandlers, config.CensorScores),
"warning": getLevelConfig(censor.Warning, config.CensorThresholds, config.CensorHandlers, config.CensorScores),
"danger": getLevelConfig(censor.Danger, config.CensorThresholds, config.CensorHandlers, config.CensorScores),
}
return Success(&c, Response{
"mode": myDice.Config.CensorMode,
"caseSensitive": myDice.Config.CensorCaseSensitive,
"matchPinyin": myDice.Config.CensorMatchPinyin,
"filterRegex": myDice.Config.CensorFilterRegexStr,
"mode": config.CensorMode,
"caseSensitive": config.CensorCaseSensitive,
"matchPinyin": config.CensorMatchPinyin,
"filterRegex": config.CensorFilterRegexStr,
"levelConfig": levelConfig,
})
}
Expand Down Expand Up @@ -153,32 +156,33 @@ func censorSetConfig(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, err)
}

config := &myDice.Config
if val, ok := jsonMap["filterRegex"]; ok {
filterRegex, ok := val.(string)
if ok {
_, err := regexp.Compile(filterRegex)
if err != nil {
return Error(&c, "过滤字符正则不是合法的正则表达式", Response{})
}
myDice.Config.CensorFilterRegexStr = filterRegex
config.CensorFilterRegexStr = filterRegex
}
}
if val, ok := jsonMap["mode"]; ok {
mode, ok := val.(float64)
if ok {
myDice.Config.CensorMode = dice.CensorMode(mode)
config.CensorMode = dice.CensorMode(mode)
}
}
if val, ok := jsonMap["caseSensitive"]; ok {
caseSensitive, ok := val.(bool)
if ok {
myDice.Config.CensorCaseSensitive = caseSensitive
config.CensorCaseSensitive = caseSensitive
}
}
if val, ok := jsonMap["matchPinyin"]; ok {
matchPinyin, ok := val.(bool)
if ok {
myDice.Config.CensorMatchPinyin = matchPinyin
config.CensorMatchPinyin = matchPinyin
}
}
if val, ok := jsonMap["levelConfig"]; ok { //nolint:nestif
Expand Down Expand Up @@ -212,15 +216,15 @@ func censorSetConfig(c echo.Context) error {
if ok {
if val, ok = confMap["threshold"]; ok {
threshold := val.(float64)
myDice.Config.CensorThresholds[level] = int(threshold)
config.CensorThresholds[level] = int(threshold)
}
if val, ok = confMap["handlers"]; ok {
handlers := stringConvert(val)
setLevelHandlers(level, handlers)
}
if val, ok = confMap["score"]; ok {
score := val.(float64)
myDice.Config.CensorScores[level] = int(score)
config.CensorScores[level] = int(score)
}
}
}
Expand Down Expand Up @@ -259,7 +263,7 @@ func setLevelHandlers(level censor.Level, handlers []string) {
handlerVal = newHandlerVal(handlerVal, dice.BanInviter, newHandlers)
handlerVal = newHandlerVal(handlerVal, dice.AddScore, newHandlers)

myDice.Config.CensorHandlers[level] = handlerVal
(&myDice.Config).CensorHandlers[level] = handlerVal
}

func newHandlerVal(val uint8, handle dice.CensorHandler, newHandlers map[dice.CensorHandler]bool) uint8 {
Expand Down
Loading

0 comments on commit 0f64253

Please sign in to comment.