()
| 20 | ) |
| 21 | |
| 22 | func init() { |
| 23 | engine := control.AutoRegister(&ctrl.Options[*zero.Ctx]{ |
| 24 | DisableOnDefault: false, |
| 25 | Brief: "投票", |
| 26 | Help: "- 投票选择可口可乐还是百事可乐\n", |
| 27 | PrivateDataFolder: "vote", |
| 28 | }) |
| 29 | |
| 30 | engine.OnPrefix("投票选择", zero.OnlyGroup, getPara).SetBlock(true). |
| 31 | Handle(func(ctx *zero.Ctx) { |
| 32 | rawOptions := strings.Split(ctx.State["args"].(string), "还是") |
| 33 | options := make([]string, 0, 16) |
| 34 | for count, option := range rawOptions { |
| 35 | options = append(options, strconv.Itoa(count)+". "+option) |
| 36 | } |
| 37 | if len(options) == 0 || len(options) == 1 { |
| 38 | ctx.SendChain(message.Text("投票的选项太少, 退出投票")) |
| 39 | return |
| 40 | } |
| 41 | ctx.SendChain(message.Text("投票开始, 设定的投票时间到或者60秒内无人投票就停止投票\n请按序号投票:\n", strings.Join(options, "\n"))) |
| 42 | voteMap := make(map[int]int, 0) |
| 43 | repeatMap := make(map[int64]int, 0) |
| 44 | mode := ctx.State["vote_paras"].([2]int)[0] |
| 45 | voteDuration := ctx.State["vote_paras"].([2]int)[1] |
| 46 | next := zero.NewFutureEvent("message", 999, false, zero.RegexRule(`^\d{1,2}$`), |
| 47 | zero.OnlyGroup, zero.CheckGroup(ctx.Event.GroupID)) |
| 48 | recv, cancel := next.Repeat() |
| 49 | defer cancel() |
| 50 | after := time.NewTimer(time.Duration(voteDuration) * time.Second) |
| 51 | EXIT: |
| 52 | for { |
| 53 | select { |
| 54 | case <-time.After(60 * time.Second): |
| 55 | ctx.SendChain(message.Text("投票结束")) |
| 56 | break EXIT |
| 57 | case <-after.C: |
| 58 | ctx.SendChain(message.Text("投票结束")) |
| 59 | break EXIT |
| 60 | case c := <-recv: |
| 61 | uid := c.Event.UserID |
| 62 | choose, err := strconv.Atoi(c.Event.Message.String()) |
| 63 | if err != nil { |
| 64 | ctx.SendChain(message.Text("ERROR:", err)) |
| 65 | } |
| 66 | if _, ok := repeatMap[uid]; !ok && choose >= 0 && choose < len(options) { |
| 67 | voteMap[choose]++ |
| 68 | } |
| 69 | if mode == 2 { |
| 70 | repeatMap[uid] = 1 |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | vc := rankByVote(voteMap) |
| 75 | if len(vc) > 20 { |
| 76 | vc = vc[:20] |
| 77 | } |
| 78 | // 绘图 |
| 79 | b, err := os.ReadFile(text.FontFile) |
nothing calls this directly
no test coverage detected