-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
168 lines (136 loc) · 4.13 KB
/
handlers.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"fmt"
"strings"
dg "github.com/bwmarrin/discordgo"
)
func hi(s *dg.Session, g *dg.GuildCreate) {
// fmt.Println(g.Name, ":", g.JoinedAt)
if flag, err := isNewGuild(g.ID); err == nil && !flag {
return
} else if err != nil {
fmt.Println("err in isNewGuild ", err)
return
}
for _, v := range g.Channels {
p, err := s.State.UserChannelPermissions(botID, v.ID)
psm := err == nil && p&dg.PermissionSendMessages == dg.PermissionSendMessages
prm := p&dg.PermissionReadMessages == dg.PermissionReadMessages
if v.Type == dg.ChannelTypeGuildText && psm && prm {
if strings.ToLower(v.Name) == "general" || v.ID == g.ID || !(strings.Contains(v.Name, "rule") || strings.Contains(v.Name, "info")) {
if _, err := s.ChannelMessageSend(v.ID, "Thank you for inviting Iniesto in this guild. This bot was written as a simple way to keep tabs on soccer score. The default prefix is **>**. Type `> help` for list of commands."); err == nil {
break
}
}
}
}
_ = addNewGuild(g.ID, g.Name)
}
func bye(s *dg.Session, g *dg.GuildDelete) {
if g.Unavailable {
return
}
removeGuild(g.ID)
// owner, err := s.User(g.OwnerID)
// fmt.Println("Owner: ", owner.Username)
dm, err := s.UserChannelCreate(g.OwnerID)
if err != nil {
fmt.Printf("err in dming owner: %v\n", err)
return
}
_, err = s.ChannelMessageSend(dm.ID, "Thanks for using Iniesto. If you have any feedback or suggestions on how to improve Iniesto, please consider leaving a message on the support server. You can use `> info` in this dm channel to get the support server invite. Hope you have a good day. Bye!")
if err != nil {
fmt.Printf("err in dming owner: %v\n", err)
}
}
func pingPong(s *dg.Session, m *dg.MessageCreate) {
if m.Content == "ping" && m.Author.ID == "399951813237014528" {
s.ChannelMessageSend(m.ChannelID, "Pong!")
}
}
func commands(s *dg.Session, m *dg.MessageCreate) {
if u, _ := s.User(m.Author.ID); m.Author.ID == botID || u.Bot {
return
}
ch, _ := s.Channel(m.ChannelID)
prefix := getPrefix(ch.GuildID)
ms := m.Mentions
mentionsBot := len(ms) == 1 && ms[0].ID == botID
hasPrefix := strings.HasPrefix(m.Content, prefix)
if !mentionsBot && !hasPrefix {
return
}
trim := strings.TrimSpace
cnt := trim(m.Content)
if hasPrefix {
cnt = trim(m.Content[len(prefix):])
}
cmd := strings.Split(cnt, " ")
if mentionsBot {
temp := []string{}
for _, w := range cmd {
if !strings.HasPrefix(w, "<@") {
temp = append(temp, trim(w))
}
}
cmd = temp
}
f := func(*dg.Session, string) {}
switch trim(cmd[0]) {
case "info":
f = info
case "matches":
args := []string{""}
if len(cmd) > 1 {
args = cmd[1:]
}
f = matches(args)
case "score":
f = score
case "prefix":
f = func(s *dg.Session, cID string) {
c, err := s.Channel(cID)
if err != nil {
e := errorEmbed("Internal Error. Try again later or report it in support server.")
fmt.Println("Err in getting Guild Channel", err)
s.ChannelMessageSendEmbed(cID, &e)
return
}
p, err := s.State.UserChannelPermissions(m.Author.ID, m.ChannelID)
// fmt.Println(err)
pmc := err == nil && p&dg.PermissionManageChannels == dg.PermissionManageChannels
if !pmc {
e := errorEmbed("Only higher level members can set new prefix.")
s.ChannelMessageSendEmbed(cID, &e)
return
}
if len(cmd) != 2 {
e := errorEmbed("`prefix` needs a second argument to set as the new prefix.")
s.ChannelMessageSendEmbed(cID, &e)
return
}
err = setPrefix(c.GuildID, cmd[1])
if err != nil {
e := errorEmbed("Error setting new prefix for Guild. Try again later.")
s.ChannelMessageSendEmbed(cID, &e)
return
}
e := msgEmbed(fmt.Sprintf("**%v** is the new prefix.", cmd[1]))
s.ChannelMessageSendEmbed(cID, &e)
return
}
case "help":
f = func(s *dg.Session, cID string) {
c, err := s.Channel(cID)
if err != nil {
e := errorEmbed("Internal Error. Try again later or report it in support server.")
fmt.Println("Err in getting Guild Channel", err)
s.ChannelMessageSendEmbed(cID, &e)
return
}
e := helpEmbed(c.GuildID)
s.ChannelMessageSendEmbed(cID, &e)
}
}
go f(s, m.ChannelID)
}