Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: 重构 ban 指令 #405

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 65 additions & 70 deletions dice/builtin_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,29 @@ func (d *Dice) registerCoreCommands() {
return CmdExecuteResult{Matched: true, Solved: true}
}

val := cmdArgs.GetArgN(1)
getID := func() string {
if cmdArgs.IsArgEqual(2, "user") {
user := cmdArgs.GetArgN(3)
if user == "" {
return ""
}
return FormatDiceID(ctx, user, false)
} else if cmdArgs.IsArgEqual(2, "group") {
group := cmdArgs.GetArgN(3)
if group == "" {
if cmdArgs.IsArgEqual(2, "user") || cmdArgs.IsArgEqual(2, "group") {
id := cmdArgs.GetArgN(3)
if id == "" {
return ""
}
return FormatDiceID(ctx, group, true)

isGroup := cmdArgs.IsArgEqual(2, "group")
return FormatDiceID(ctx, id, isGroup)
}
ret := cmdArgs.GetArgN(2)
if !strings.Contains(ret, ":") {
// 如果不是这种格式,那么放弃
ret = ""

arg := cmdArgs.GetArgN(2)
if !strings.Contains(arg, ":") {
return ""
}
return ret
return arg
}

var val = cmdArgs.GetArgN(1)
var uid string
switch strings.ToLower(val) {
case "add":
uid := getID()
uid = getID()
if uid == "" {
return CmdExecuteResult{Matched: true, Solved: true, ShowHelp: true}
}
Expand All @@ -82,89 +79,87 @@ func (d *Dice) registerCoreCommands() {
d.BanList.AddScoreBase(uid, d.BanList.ThresholdBan, "骰主指令", reason, ctx)
ReplyToSender(ctx, msg, fmt.Sprintf("已将用户/群组 %s 加入黑名单,原因: %s", uid, reason))
case "rm", "del":
uid := getID()
uid = getID()
if uid == "" {
return CmdExecuteResult{Matched: true, Solved: true, ShowHelp: true}
}

item := d.BanList.GetByID(uid)
if item == nil || !(item.Rank == BanRankBanned || item.Rank == BanRankTrusted || item.Rank == BanRankWarn) {
if item == nil || (item.Rank != BanRankBanned && item.Rank != BanRankTrusted && item.Rank != BanRankWarn) {
ReplyToSender(ctx, msg, "找不到用户/群组")
} else {
ReplyToSender(ctx, msg, fmt.Sprintf("已将用户/群组 %s 移出%s列表", uid, BanRankText[item.Rank]))
item.Score = 0
item.Rank = BanRankNormal
break
}

ReplyToSender(ctx, msg, fmt.Sprintf("已将用户/群组 %s 移出%s列表", uid, BanRankText[item.Rank]))
item.Score = 0
item.Rank = BanRankNormal
case "trust":
uid := cmdArgs.GetArgN(2)
uid = cmdArgs.GetArgN(2)
if !strings.Contains(uid, ":") {
// 如果不是这种格式,那么放弃
uid = ""
}
if uid == "" {
return CmdExecuteResult{Matched: true, Solved: true, ShowHelp: true}
}

d.BanList.SetTrustByID(uid, "骰主指令", "骰主指令")
ReplyToSender(ctx, msg, fmt.Sprintf("已将用户/群组 %s 加入信任列表", uid))
case "list", "show":
// ban/warn/trust
extra := cmdArgs.GetArgN(2)
text := ""
var extra, text string

extra = cmdArgs.GetArgN(2)
d.BanList.Map.Range(func(k string, v *BanListInfoItem) bool {
if v.Rank != BanRankNormal {
if extra != "" {
if extra == "trust" && v.Rank == BanRankTrusted {
text += v.toText(d) + "\n"
}
if extra == "ban" && v.Rank == BanRankBanned {
text += v.toText(d) + "\n"
}
if extra == "warn" && v.Rank == BanRankWarn {
text += v.toText(d) + "\n"
}
} else {
text += v.toText(d) + "\n"
}
if v.Rank == BanRankNormal {
return true
}

match := (extra == "trust" && v.Rank == BanRankTrusted) ||
(extra == "ban" && v.Rank == BanRankBanned) ||
(extra == "warn" && v.Rank == BanRankWarn)
if extra == "" || match {
text += v.toText(d) + "\n"
}
return true
})

if text == "" {
text = "当前名单:\n<无内容>"
} else {
text = "当前名单:\n" + text
}
ReplyToSender(ctx, msg, text)
case "query":
targetID := cmdArgs.GetArgN(2)
var targetID = cmdArgs.GetArgN(2)
if targetID == "" {
ReplyToSender(ctx, msg, "未指定要查询的对象!")
} else {
var text string
v, exists := d.BanList.Map.Load(targetID)
if exists {
text += fmt.Sprintf("所查询的<%s>情况:", targetID)
switch v.Rank {
case BanRankBanned:
text += "禁止(-30)"
case BanRankWarn:
text += "警告(-10)"
case BanRankTrusted:
text += "信任(30)"
default:
text += "正常(0)"
}
for i, reason := range v.Reasons {
text += fmt.Sprintf("\n%s在「%s」,原因:%s",
carbon.CreateFromTimestamp(v.Times[i]).ToDateTimeString(),
v.Places[i],
reason,
)
}
} else {
text = fmt.Sprintf("所查询的<%s>情况:正常(0)", targetID)
}
ReplyToSender(ctx, msg, text)
break
}

v, exists := d.BanList.Map.Load(targetID)
if !exists {
ReplyToSender(ctx, msg, fmt.Sprintf("所查询的<%s>情况:正常(0)", targetID))
break
}

var text = fmt.Sprintf("所查询的<%s>情况:", targetID)
switch v.Rank {
case BanRankBanned:
text += "禁止(-30)"
case BanRankWarn:
text += "警告(-10)"
case BanRankTrusted:
text += "信任(30)"
default:
text += "正常(0)"
}
for i, reason := range v.Reasons {
text += fmt.Sprintf(
"\n%s在「%s」,原因:%s",
carbon.CreateFromTimestamp(v.Times[i]).ToDateTimeString(),
v.Places[i],
reason,
)
}
ReplyToSender(ctx, msg, text)
default:
return CmdExecuteResult{Matched: true, Solved: true, ShowHelp: true}
}
Expand Down