/
hanamaru.go
141 lines (119 loc) · 3.29 KB
/
hanamaru.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package framework
import (
"database/sql"
"fmt"
"log"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/ninjawarrior1337/hanamaru-go/framework/voice"
)
type Hanamaru struct {
Prefix string
ownerId string
*discordgo.Session
VoiceContext *voice.Context
Commands []*Command
EventListeners []*EventListener
Db *sql.DB
}
func New(token, prefix, ownerid string) (bot *Hanamaru) {
s, err := discordgo.New(token)
if err != nil {
log.Fatalf("Failed to create bot: %v", err)
}
s.Identify.Intents = discordgo.IntentsAllWithoutPrivileged | discordgo.IntentsGuildMembers | discordgo.IntentsMessageContent
err = s.Open()
if err != nil {
log.Fatalf("Failed to open webhook: %v", err)
}
voiceContext := voice.NewContext()
log.Printf("Ready and logged in as %v zura!", s.State.User.Username+"#"+s.State.User.Discriminator)
return &Hanamaru{
Prefix: prefix,
Session: s,
VoiceContext: voiceContext,
ownerId: ownerid,
Commands: []*Command{},
}
}
func HasPermission(s *discordgo.Session, userID string, channelID string, reqPerm int64) (bool, error) {
userPerms, err := s.State.UserChannelPermissions(userID, channelID)
if err != nil {
return false, err
}
if (userPerms | reqPerm) == userPerms {
return true, nil
}
return false, nil
}
type ErrAddCommand struct {
cmd *Command
reason error
}
func (e ErrAddCommand) Error() string {
return fmt.Sprintf("failed to add command: %v: reason: %v", e.cmd.Name, e.reason)
}
func (h *Hanamaru) AddCommand(cmd *Command) error {
if cmd.Name == "" {
panic("A command must not have an empty name!")
}
if cmd.Setup != nil {
err := cmd.Setup()
if err != nil {
return ErrAddCommand{
cmd: cmd,
reason: err,
}
}
}
var handleFunc = func(s *discordgo.Session, m *discordgo.MessageCreate) {
if !strings.HasPrefix(m.Content, h.Prefix+cmd.Name) {
return
}
if m.Author.ID == s.State.User.ID || m.Author.Bot {
return
}
if cmd.OwnerOnly && h.ownerId != m.Author.ID {
s.ChannelMessageSend(m.ChannelID, "ERROR: You must be the owner of this instance to run this command")
return
}
if ok, _ := HasPermission(s, m.Author.ID, m.ChannelID, cmd.PermissionRequired); cmd.PermissionRequired > 0 && !ok {
s.ChannelMessageSend(m.ChannelID, "ERROR: You don't have the required permissions to run this command")
return
}
ctx := NewContext(h, cmd, m)
h.Session.ChannelTyping(ctx.ChannelID)
err := cmd.Exec(ctx)
if err != nil {
s.ChannelMessageSendReply(m.ChannelID, "ERROR: "+err.Error(), ctx.Reference())
}
}
h.Session.AddHandler(handleFunc)
h.Commands = append(h.Commands, cmd)
return nil
}
// AddEventListener adds and event listener to the bot.
func (h *Hanamaru) AddEventListener(listener *EventListener) error {
h.Session.AddHandler(listener.HandlerConstructor(h))
h.EventListeners = append(h.EventListeners, listener)
return nil
}
func (h *Hanamaru) AddEventListenerOnce(listener *EventListener) error {
h.Session.AddHandlerOnce(listener.HandlerConstructor(h))
return nil
}
func (h *Hanamaru) EnableHelpCommand() {
h.AddCommand(help)
}
func (h *Hanamaru) Close() {
for _, vc := range h.VoiceContext.VCs {
vc.Disconnect()
}
err := h.Session.Close()
if err != nil {
log.Fatalf("Failed to exit the bot correctly: %v", err)
}
if h.Db != nil {
h.Db.Close()
}
}