|
| 1 | +package driftbottle |
| 2 | + |
| 3 | +import ( |
| 4 | + "strconv" |
| 5 | + "strings" |
| 6 | + "sync" |
| 7 | + |
| 8 | + "github.com/FloatTech/zbputils/control" |
| 9 | + "github.com/FloatTech/zbputils/control/order" |
| 10 | + "github.com/FloatTech/zbputils/ctxext" |
| 11 | + "github.com/sirupsen/logrus" |
| 12 | + zero "github.com/wdvxdr1123/ZeroBot" |
| 13 | + "github.com/wdvxdr1123/ZeroBot/message" |
| 14 | +) |
| 15 | + |
| 16 | +func init() { |
| 17 | + en := control.Register("driftbottle", order.AcquirePrio(), &control.Options{ |
| 18 | + DisableOnDefault: false, |
| 19 | + Help: "漂流瓶\n- (在群xxx)丢漂流瓶(到频道xxx) [消息]\n- (从频道xxx)捡漂流瓶\n- 创建频道 xxx\n- 跳入(频道)海中\n- 注:不显式限制时,私聊发送可在所有群抽到,群聊发送仅可在本群抽到,默认频道为 global", |
| 20 | + }) |
| 21 | + en.OnRegex(`^(在群\d+)?丢漂流瓶(到频道\w+)?\s+(.*)$`).SetBlock(true). |
| 22 | + Handle(func(ctx *zero.Ctx) { |
| 23 | + msgs := ctx.State["regex_matched"].([]string) |
| 24 | + grp := ctx.Event.GroupID |
| 25 | + channel := "global" |
| 26 | + msg := msgs[3] |
| 27 | + var err error |
| 28 | + if msgs[1] != "" { |
| 29 | + grp, err = strconv.ParseInt(msgs[1][6:], 10, 64) |
| 30 | + if err != nil { |
| 31 | + ctx.SendChain(message.Text("群号非法!")) |
| 32 | + return |
| 33 | + } |
| 34 | + } |
| 35 | + if msgs[2] != "" { |
| 36 | + channel = msgs[2][9:] |
| 37 | + } |
| 38 | + if msg == "" { |
| 39 | + ctx.SendChain(message.Text("消息为空!")) |
| 40 | + return |
| 41 | + } |
| 42 | + logrus.Debugln("[driftbottle]", grp, channel, msg) |
| 43 | + err = newBottle( |
| 44 | + ctx.Event.UserID, |
| 45 | + grp, |
| 46 | + ctxext.CardOrNickName(ctx, ctx.Event.UserID), |
| 47 | + msg, |
| 48 | + ).throw(sea, channel) |
| 49 | + if err != nil { |
| 50 | + ctx.SendChain(message.Text("ERROR:", err)) |
| 51 | + return |
| 52 | + } |
| 53 | + ctx.Send(message.ReplyWithMessage(ctx.Event.MessageID, message.Text("你将它扔进大海,希望有人捞到吧~"))) |
| 54 | + }) |
| 55 | + en.OnRegex(`^(从频道\w+)?捡漂流瓶$`).SetBlock(true). |
| 56 | + Handle(func(ctx *zero.Ctx) { |
| 57 | + msgs := ctx.State["regex_matched"].([]string) |
| 58 | + grp := ctx.Event.GroupID |
| 59 | + if grp == 0 { |
| 60 | + grp = -ctx.Event.UserID |
| 61 | + } |
| 62 | + if grp == 0 { |
| 63 | + ctx.SendChain(message.Text("找不到对象!")) |
| 64 | + return |
| 65 | + } |
| 66 | + channel := "global" |
| 67 | + if msgs[1] != "" { |
| 68 | + channel = msgs[1][9:] |
| 69 | + } |
| 70 | + logrus.Debugln("[driftbottle]", grp, channel) |
| 71 | + b, err := fetchBottle(sea, channel, grp) |
| 72 | + if err != nil { |
| 73 | + ctx.SendChain(message.Text("ERROR:", err)) |
| 74 | + return |
| 75 | + } |
| 76 | + var wg sync.WaitGroup |
| 77 | + wg.Add(1) |
| 78 | + go func() { |
| 79 | + err = b.destroy(sea, channel) |
| 80 | + wg.Done() |
| 81 | + }() |
| 82 | + ctx.Send( |
| 83 | + message.ReplyWithMessage( |
| 84 | + ctx.Event.MessageID, |
| 85 | + message.Text("你在海边捡到了一个来自 ", b.Name, " 的漂流瓶,打开瓶子,里面有一张纸条,写着:"), |
| 86 | + message.Text(b.Msg), |
| 87 | + ), |
| 88 | + ) |
| 89 | + wg.Wait() |
| 90 | + if err != nil { |
| 91 | + ctx.SendChain(message.Text("ERROR:", err)) |
| 92 | + return |
| 93 | + } |
| 94 | + }) |
| 95 | + en.OnPrefix("创建频道", zero.SuperUserPermission, zero.OnlyToMe).SetBlock(true). |
| 96 | + Handle(func(ctx *zero.Ctx) { |
| 97 | + channel := strings.TrimRight(ctx.State["args"].(string), " ") |
| 98 | + if channel == "" { |
| 99 | + ctx.SendChain(message.Text("频道名为空!")) |
| 100 | + return |
| 101 | + } |
| 102 | + err := createChannel(sea, channel) |
| 103 | + if err != nil { |
| 104 | + ctx.SendChain(message.Text("ERROR:", err)) |
| 105 | + return |
| 106 | + } |
| 107 | + ctx.Send(message.ReplyWithMessage(ctx.Event.MessageID, message.Text("成功~"))) |
| 108 | + }) |
| 109 | + en.OnRegex(`^跳入(\w+)?海中$`).SetBlock(true). |
| 110 | + Handle(func(ctx *zero.Ctx) { |
| 111 | + msgs := ctx.State["regex_matched"].([]string) |
| 112 | + channel := "global" |
| 113 | + if msgs[1] != "" { |
| 114 | + channel = msgs[1] |
| 115 | + } |
| 116 | + seamu.RLock() |
| 117 | + c, err := sea.Count(channel) |
| 118 | + seamu.RUnlock() |
| 119 | + if err != nil { |
| 120 | + ctx.SendChain(message.Text("ERROR:", err)) |
| 121 | + return |
| 122 | + } |
| 123 | + ctx.Send(message.ReplyWithMessage(ctx.Event.MessageID, message.Text("你缓缓走入大海,感受着海浪轻柔地拍打着你的小腿,膝盖……\n波浪卷着你的腰腹,你感觉有些把握不住平衡了……\n……\n你沉入海中,", c, " 个物体与你一同沉浮。\n不知何处涌来一股暗流,你失去了意识。"))) |
| 124 | + }) |
| 125 | +} |
0 commit comments