|
| 1 | +package bot |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/celestix/gotgproto/dispatcher" |
| 9 | + "github.com/celestix/gotgproto/ext" |
| 10 | + "github.com/duke-git/lancet/v2/slice" |
| 11 | + "github.com/gotd/td/telegram/message/styling" |
| 12 | + "github.com/krau/SaveAny-Bot/common" |
| 13 | + "github.com/krau/SaveAny-Bot/dao" |
| 14 | + "github.com/krau/SaveAny-Bot/types" |
| 15 | +) |
| 16 | + |
| 17 | +func sendRuleHelp(ctx *ext.Context, update *ext.Update, userChatID int64) error { |
| 18 | + user, err := dao.GetUserByChatID(userChatID) |
| 19 | + if err != nil { |
| 20 | + common.Log.Errorf("获取用户规则失败: %s", err) |
| 21 | + ctx.Reply(update, ext.ReplyTextString("获取用户规则失败"), nil) |
| 22 | + return dispatcher.EndGroups |
| 23 | + } |
| 24 | + ctx.Reply(update, ext.ReplyTextStyledTextArray( |
| 25 | + []styling.StyledTextOption{ |
| 26 | + styling.Bold("使用方法: /rule <操作> <参数...>"), |
| 27 | + styling.Bold(fmt.Sprintf("\n当前已%s规则模式", map[bool]string{true: "启用", false: "禁用"}[user.ApplyRule])), |
| 28 | + styling.Plain("\n\n可用操作:\n"), |
| 29 | + styling.Code("switch"), |
| 30 | + styling.Plain(" - 开关规则模式\n"), |
| 31 | + styling.Code("add"), |
| 32 | + styling.Plain(" <类型> <数据> <存储名> <路径> - 添加规则\n"), |
| 33 | + styling.Code("del"), |
| 34 | + styling.Plain(" <规则ID> - 删除规则\n"), |
| 35 | + styling.Plain("\n当前已添加的规则:\n"), |
| 36 | + styling.Blockquote(func() string { |
| 37 | + var sb strings.Builder |
| 38 | + for _, rule := range user.Rules { |
| 39 | + ruleText := fmt.Sprintf("%s %s %s %s", rule.Type, rule.Data, rule.StorageName, rule.DirPath) |
| 40 | + sb.WriteString(fmt.Sprintf("%d: %s\n", rule.ID, ruleText)) |
| 41 | + } |
| 42 | + return sb.String() |
| 43 | + }(), true), |
| 44 | + }, |
| 45 | + ), nil) |
| 46 | + return dispatcher.EndGroups |
| 47 | +} |
| 48 | + |
| 49 | +func ruleCmd(ctx *ext.Context, update *ext.Update) error { |
| 50 | + args := strings.Split(update.EffectiveMessage.Text, " ") |
| 51 | + if len(args) < 2 { |
| 52 | + return sendRuleHelp(ctx, update, update.GetUserChat().GetID()) |
| 53 | + } |
| 54 | + user, err := dao.GetUserByChatID(update.GetUserChat().GetID()) |
| 55 | + if err != nil { |
| 56 | + common.Log.Errorf("获取用户失败: %s", err) |
| 57 | + ctx.Reply(update, ext.ReplyTextString("获取用户失败"), nil) |
| 58 | + return dispatcher.EndGroups |
| 59 | + } |
| 60 | + switch args[1] { |
| 61 | + case "switch": |
| 62 | + // /rule switch |
| 63 | + return switchApplyRule(ctx, update, user) |
| 64 | + case "add": |
| 65 | + // /rule add <type> <data> <storage> <dirpath> |
| 66 | + if len(args) < 6 { |
| 67 | + return sendRuleHelp(ctx, update, user.ChatID) |
| 68 | + } |
| 69 | + return addRule(ctx, update, user, args) |
| 70 | + case "del": |
| 71 | + // /rule del <id> |
| 72 | + if len(args) < 3 { |
| 73 | + return sendRuleHelp(ctx, update, user.ChatID) |
| 74 | + } |
| 75 | + ruleID := args[2] |
| 76 | + id, err := strconv.Atoi(ruleID) |
| 77 | + if err != nil { |
| 78 | + ctx.Reply(update, ext.ReplyTextString("无效的规则ID"), nil) |
| 79 | + return dispatcher.EndGroups |
| 80 | + } |
| 81 | + if err := dao.DeleteRule(uint(id)); err != nil { |
| 82 | + common.Log.Errorf("删除规则失败: %s", err) |
| 83 | + ctx.Reply(update, ext.ReplyTextString("删除规则失败"), nil) |
| 84 | + return dispatcher.EndGroups |
| 85 | + } |
| 86 | + ctx.Reply(update, ext.ReplyTextString("删除规则成功"), nil) |
| 87 | + return dispatcher.EndGroups |
| 88 | + default: |
| 89 | + return sendRuleHelp(ctx, update, user.ChatID) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func switchApplyRule(ctx *ext.Context, update *ext.Update, user *dao.User) error { |
| 94 | + applyRule := !user.ApplyRule |
| 95 | + if err := dao.UpdateUserApplyRule(user.ChatID, applyRule); err != nil { |
| 96 | + common.Log.Errorf("更新用户失败: %s", err) |
| 97 | + ctx.Reply(update, ext.ReplyTextString("更新用户失败"), nil) |
| 98 | + return dispatcher.EndGroups |
| 99 | + } |
| 100 | + if applyRule { |
| 101 | + ctx.Reply(update, ext.ReplyTextString("已启用规则模式"), nil) |
| 102 | + } else { |
| 103 | + ctx.Reply(update, ext.ReplyTextString("已禁用规则模式"), nil) |
| 104 | + } |
| 105 | + return dispatcher.EndGroups |
| 106 | +} |
| 107 | + |
| 108 | +func addRule(ctx *ext.Context, update *ext.Update, user *dao.User, args []string) error { |
| 109 | + // /rule add <type> <data> <storage> <dirpath> |
| 110 | + ruleType := args[2] |
| 111 | + ruleData := args[3] |
| 112 | + storageName := args[4] |
| 113 | + dirPath := args[5] |
| 114 | + |
| 115 | + if !slice.Contain(types.RuleTypes, types.RuleType(ruleType)) { |
| 116 | + var ruleTypesStylingArray []styling.StyledTextOption |
| 117 | + ruleTypesStylingArray = append(ruleTypesStylingArray, styling.Bold("无效的规则类型, 可用类型:\n")) |
| 118 | + for i, ruleType := range types.RuleTypes { |
| 119 | + ruleTypesStylingArray = append(ruleTypesStylingArray, styling.Code(string(ruleType))) |
| 120 | + if i != len(types.RuleTypes)-1 { |
| 121 | + ruleTypesStylingArray = append(ruleTypesStylingArray, styling.Plain(", ")) |
| 122 | + } |
| 123 | + } |
| 124 | + ctx.Reply(update, ext.ReplyTextStyledTextArray(ruleTypesStylingArray), nil) |
| 125 | + return dispatcher.EndGroups |
| 126 | + } |
| 127 | + rule := &dao.Rule{ |
| 128 | + Type: ruleType, |
| 129 | + Data: ruleData, |
| 130 | + StorageName: storageName, |
| 131 | + DirPath: dirPath, |
| 132 | + UserID: user.ID, |
| 133 | + } |
| 134 | + if err := dao.CreateRule(rule); err != nil { |
| 135 | + common.Log.Errorf("添加规则失败: %s", err) |
| 136 | + ctx.Reply(update, ext.ReplyTextString("添加规则失败"), nil) |
| 137 | + return dispatcher.EndGroups |
| 138 | + } |
| 139 | + ctx.Reply(update, ext.ReplyTextString("添加规则成功"), nil) |
| 140 | + return dispatcher.EndGroups |
| 141 | +} |
0 commit comments