-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
216 lines (173 loc) · 5.9 KB
/
main.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"github.com/bwmarrin/discordgo"
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
)
// Variables used for command line parameters.
var (
Token string
)
// Custom variables
var (
helpMsg = "Prefix: `$`\nHelp\nRole\nRoles\nBug\nGithub\nMute\nMuted\nMembers"
splitMsgLowered = []string{}
botOwnerID = "121105861539135490" // Change to your id on discord
)
func makeSplitMessage(s *discordgo.Session, m *discordgo.MessageCreate) []string {
// The message, split up
splitMessage := strings.Fields(strings.ToLower(m.Content))
return splitMessage
}
func init() {
flag.StringVar(&Token, "t", "", "Bot Token")
flag.Parse()
}
func main() {
// Create a new Discord sessions using the provided bot token
dg, err := discordgo.New("Bot " + Token)
if err != nil {
fmt.Println("error creating Discord session", err)
return
}
// Register the messageCreate func as a callback for MessageCreate events
dg.AddHandler(messageCreate)
// Register the guildMemberAddHandler func as a callback for GuildMemberAdd events
dg.AddHandler(guildMemberAddHandler)
// Register the guildMemberRemoveHandler func as a callback for GuildMemberRemove events
dg.AddHandler(guildMemberRemoveHandler)
// Register the guildMemberBannedHandler func as a callback for GuildBanAdd events
dg.AddHandler(guildMemberBannedHandler)
// Open a websocket connection to Discord and begin listening
err = dg.Open()
if err != nil {
fmt.Println("error opening connection", err)
return
}
loadCommands()
// Wait here until CTRL-C or other term signal is received
fmt.Println("The bot is now running. Press CTRL-C to stop")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
// Cleanly close down the Discord session
defer dg.Close()
}
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { // Message handling
// Ignore all messages created by the bot itself
if m.Author.ID == s.State.User.ID {
return
}
splitMsgLowered = makeSplitMessage(s, m)
checkHereEveryone(s, m)
checkAyy(s, m)
if len(splitMsgLowered) > 0 { // Prevented a really rare and weird bug about going out of index.
parseCommand(s, m, splitMsgLowered[0]) // Really shouldnt happen since `MessageCreate` is about
} // messages made on create...
}
func contains(s []string, e string) bool { // Go has no contains for slices :)
for _, a := range s {
if a == e {
return true
}
}
return false
}
func checkAyy(s *discordgo.Session, m *discordgo.MessageCreate) {
if contains(splitMsgLowered, "ayy") {
s.ChannelMessageSend(m.ChannelID, "lmao")
}
}
func checkHereEveryone(s *discordgo.Session, m *discordgo.MessageCreate) { // Doesn't let members who aren't mods, admins, or owners @here or @everyone. Please change to your needs.
if strings.Contains(m.Content, "@here") || strings.Contains(m.Content, "@everyone") { // [Yes, discord has functionality for this but I was instructed to add this for whatever stupid reason]
currentGuild, err := getGuild(s, m)
if err != nil {
fmt.Println("Unabled to grab guild, ")
fmt.Println(err)
return
}
currentMember, err := getMember(s, m)
if err != nil {
fmt.Println("Unabled to grab member, ")
fmt.Println(err)
return
}
rolesNeeded := []string{"Dark Mod", "Dark Admins", "Dark Overlord", "Staff"}
canHere := false
for i := 0; i < len(rolesNeeded); i++ { // Checks if the muter has roles above
tempRoleID := findRoleID(rolesNeeded[i], currentGuild)
hasRoleID := memberHasRole(currentMember, tempRoleID)
if hasRoleID {
canHere = true
}
}
if !canHere {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("<@%s>, please don't use `@here` or `@everyone`, if the message is important, talk to an admin or mod", m.Author.ID))
s.ChannelMessageDelete(m.ChannelID, m.ID)
}
}
}
func guildMemberAddHandler(s *discordgo.Session, e *discordgo.GuildMemberAdd) { // Handles GuildMemberAdd'ing
if e.User.Bot {
return
}
welcomeMessage(s, e)
}
func guildMemberRemoveHandler(s *discordgo.Session, e *discordgo.GuildMemberRemove) {
if e.User.Bot {
return
}
goodbyeMessage(s, e)
}
func guildMemberBannedHandler(s *discordgo.Session, e *discordgo.GuildBanAdd) {
if e.User.Bot {
return
}
banMessage(s, e)
}
func getGuild(s *discordgo.Session, m *discordgo.MessageCreate) (*discordgo.Guild, error) { // Returns guild
currentChannel, err := getChannel(s, m)
currentGuild, err := s.Guild(currentChannel.GuildID) // Create the current guild object
return currentGuild, err
}
func getChannel(s *discordgo.Session, m *discordgo.MessageCreate) (*discordgo.Channel, error) { // Returns channel
currentChannel, err := s.Channel(m.ChannelID) // Create the current channel object
return currentChannel, err
}
func getMember(s *discordgo.Session, m *discordgo.MessageCreate) (*discordgo.Member, error) { // Returns member
currentGuild, err := getGuild(s, m)
member, err := s.State.Member(currentGuild.ID, m.Author.ID)
return member, err
}
func getState(s *discordgo.Session) *discordgo.State { // Returns state
state := s.State
return state
}
func findRoleID(roleNeeded string, currentGuild *discordgo.Guild) string { // Returns a role ID from a list of roles based off of a string
rID := ""
for i := 0; i < len(currentGuild.Roles); i++ {
if currentGuild.Roles[i].Name == roleNeeded {
rID = currentGuild.Roles[i].ID
}
}
return rID
}
func memberHasRole(currentMember *discordgo.Member, tempRoleID string) bool { // Returns true if the member has a role, otherwise, false
for i := 0; i < len(currentMember.Roles); i++ { // If the user doesn't have that role, add it, otherwise, remove it
if currentMember.Roles[i] == tempRoleID {
return true
}
}
return false
}
func createChannel(s *discordgo.Session, m *discordgo.MessageCreate, ID string) *discordgo.Channel {
channel, err := s.UserChannelCreate(ID)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "Unable to create new channel.")
}
return channel
}