This repository was archived by the owner on Aug 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.go
110 lines (97 loc) · 2.38 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"flag"
"strings"
"github.com/whyrusleeping/hellabot"
"github.com/softwareniagara/irc_bot/store"
)
func UserTrigger(s *store.Store) hbot.Trigger {
return hbot.Trigger{
Condition: HasCommand("!user"),
Action: func(bot *hbot.Bot, msg *hbot.Message) bool {
if err := s.Authorized(msg.From, store.RoleAdmin); err != nil {
ErrorReply(bot, msg, err)
return true
}
var (
create bool
remove bool
update bool
info bool
role = store.RoleNone
greeting FlagString
)
fset := flag.NewFlagSet("", flag.ContinueOnError)
fset.BoolVar(&create, "create", false, "create a new user")
fset.BoolVar(&remove, "remove", false, "remove an existing user")
fset.BoolVar(&update, "update", false, "update existing user")
fset.BoolVar(&info, "info", false, "show user info")
fset.Var(&role, "role", "admin|regular|banned")
fset.Var(&greeting, "greeting", "greeting message")
if err := ParseFlags(msg, fset); err != nil {
ErrorReply(bot, msg, err)
return true
}
nick = strings.TrimSpace(strings.Join(fset.Args(), " "))
if nick == "" {
bot.Reply(msg, "you need to provide a nick as a positional argument")
return true
}
if create {
if err := s.InsertUser(&store.User{
Nick: nick,
Role: role.WithDefault(),
Greeting: greeting.Value,
}); err != nil {
ErrorReply(bot, msg, err)
return true
}
ReplyTo(bot, msg, "ok")
return true
}
if remove {
u, err := s.FindUserByNick(nick)
if err != nil {
ErrorReply(bot, msg, err)
return true
}
if err := s.RemoveUser(u.RowID); err != nil {
ErrorReply(bot, msg, err)
return true
}
ReplyTo(bot, msg, "ok")
return true
}
if update {
u, err := s.FindUserByNick(nick)
if err != nil {
ErrorReply(bot, msg, err)
return true
}
if role != store.RoleNone {
u.Role = role
}
if greeting.HasValue {
u.Greeting = greeting.Value
}
if err := s.UpdateUser(u); err != nil {
ErrorReply(bot, msg, err)
return true
}
ReplyTo(bot, msg, "ok")
return true
}
if info {
u, err := s.FindUserByNick(nick)
if err != nil {
ErrorReply(bot, msg, err)
return true
}
ReplyTo(bot, msg, u.String())
return true
}
ReplyTo(bot, msg, "you need to specify the action")
return true
},
}
}