forked from Clinet/clinet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd-moderation.go
163 lines (147 loc) · 6.05 KB
/
cmd-moderation.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
package main
import (
"strconv"
"strings"
"github.com/bwmarrin/discordgo"
)
//Moderator commands
func commandPurge(args []string, env *CommandEnvironment) *discordgo.MessageEmbed {
amount, err := strconv.Atoi(args[0])
if err != nil {
return NewErrorEmbed("Purge Error", "``"+args[0]+"`` is not a valid number.")
}
if amount <= 0 || amount > 100 {
return NewErrorEmbed("Purge Error", "Amount of messages to purge must be between 1 and 100.")
}
messages, err := botData.DiscordSession.ChannelMessages(env.Channel.ID, amount, env.Message.ID, "", "")
if err != nil {
return NewErrorEmbed("Purge Error", "An error occurred fetching the last "+args[0]+" messages.")
}
messageIDs := make([]string, 0)
if len(args) > 1 {
for i := 0; i < len(messages); i++ {
for j := 0; j < len(env.Message.Mentions); j++ {
if env.Message.Mentions[j].ID == messages[i].Author.ID {
messageIDs = append(messageIDs, messages[i].ID)
break
}
}
}
err = botData.DiscordSession.ChannelMessagesBulkDelete(env.Channel.ID, messageIDs)
if err != nil {
return NewErrorEmbed("Purge Error", "An error occurred deleting the last "+args[0]+" messages from the specified user(s).")
}
return NewGenericEmbed("Purge", "Successfully purged the last "+args[0]+" messages from the specified user(s).")
}
for i := 0; i < len(messages); i++ {
messageIDs = append(messageIDs, messages[i].ID)
}
err = botData.DiscordSession.ChannelMessagesBulkDelete(env.Channel.ID, messageIDs)
if err != nil {
return NewErrorEmbed("Purge Error", "An error occurred deleting the last "+args[0]+" messages.")
}
return NewGenericEmbed("Purge", "Successfully purged the last "+args[0]+" messages.")
}
func commandKick(args []string, env *CommandEnvironment) *discordgo.MessageEmbed {
if len(env.Message.Mentions) == 0 {
NewErrorEmbed("Kick Error", "You must specify which user(s) to kick from the server.")
}
reasonMessage := ""
usersToKick := make([]string, 0)
for i, part := range args {
if strings.HasPrefix(part, "<@") && strings.HasSuffix(part, ">") {
usersToKick = append(usersToKick, strings.TrimRight(strings.TrimLeft(part, "<@!"), ">"))
continue
}
reasonMessage = strings.Join(args[i:], " ")
break
}
if len(usersToKick) == 0 {
return NewErrorEmbed("Kick Error", "You must specify which user(s) to kick.")
}
if reasonMessage == "" {
for i := range usersToKick {
err := botData.DiscordSession.GuildMemberDelete(env.Guild.ID, usersToKick[i])
if err != nil {
return NewErrorEmbed("Kick Error", "An error occurred kicking <@"+usersToKick[i]+">. Please consider manually kicking and report this issue to a developer.")
}
}
return NewGenericEmbed("Kick", "Successfully kicked the selected user(s).")
}
for i := range usersToKick {
err := botData.DiscordSession.GuildMemberDeleteWithReason(env.Guild.ID, usersToKick[i], reasonMessage)
if err != nil {
return NewErrorEmbed("Kick Error", "An error occurred kicking <@"+usersToKick[i]+">. Please consider manually kicking and report this issue to a developer.")
}
}
return NewGenericEmbed("Kick", "Successfully kicked the selected user(s) for the following reason:\n**"+reasonMessage+"**")
}
func commandBan(args []string, env *CommandEnvironment) *discordgo.MessageEmbed {
if len(env.Message.Mentions) == 0 {
return NewErrorEmbed("Ban Error", "You must specify which user(s) to ban from the server.")
}
reasonMessage := ""
usersToBan := make([]string, 0)
messagesDaysToDelete := 0
for i, part := range args {
if i == 0 {
days, err := strconv.Atoi(part)
if err == nil {
messagesDaysToDelete = days
continue
} else {
return NewErrorEmbed("Ban Error", "``"+part+"`` is not a valid number.")
}
}
if strings.HasPrefix(part, "<@") && strings.HasSuffix(part, ">") {
usersToBan = append(usersToBan, strings.TrimRight(strings.TrimLeft(part, "<@!"), ">"))
continue
}
reasonMessage = strings.Join(args[i:], " ")
break
}
if len(usersToBan) == 0 {
return NewErrorEmbed("Ban Error", "You must specify which user(s) to ban.")
}
if messagesDaysToDelete > 7 {
return NewErrorEmbed("Ban Error", "You may only delete up to and including 7 days worth of messages from these users.")
}
if reasonMessage == "" {
for i := range usersToBan {
err := botData.DiscordSession.GuildBanCreate(env.Guild.ID, usersToBan[i], messagesDaysToDelete)
if err != nil {
return NewErrorEmbed("Ban Error", "An error occurred banning <@"+usersToBan[i]+">. Please consider manually banning and report this issue to a developer.")
}
}
return NewGenericEmbed("Ban", "Successfully banned the selected user(s).")
}
for i := range usersToBan {
err := botData.DiscordSession.GuildBanCreateWithReason(env.Guild.ID, usersToBan[i], reasonMessage, messagesDaysToDelete)
if err != nil {
return NewErrorEmbed("Ban Error", "An error occurred banning <@"+usersToBan[i]+">. Please consider manually banning and report this issue to a developer.")
}
}
return NewGenericEmbed("Ban", "Successfully banned the selected user(s) for the following reason:\n**"+reasonMessage+"**")
}
func commandSettingsServer(args []string, env *CommandEnvironment) *discordgo.MessageEmbed {
switch args[0] {
case "joinmsg":
guildSettings[env.Guild.ID].UserJoinMessage = strings.Join(args[1:], " ")
guildSettings[env.Guild.ID].UserJoinMessageChannel = env.Channel.ID
return NewGenericEmbed("Server Settings - Join Message", "Successfully set the join message to this channel.")
case "leavemsg":
guildSettings[env.Guild.ID].UserLeaveMessage = strings.Join(args[1:], " ")
guildSettings[env.Guild.ID].UserLeaveMessageChannel = env.Channel.ID
return NewGenericEmbed("Server Settings - Leave Message", "Successfully set the leave message to this channel.")
case "reset":
switch args[1] {
case "joinmsg":
guildSettings[env.Guild.ID].UserJoinMessage = ""
guildSettings[env.Guild.ID].UserJoinMessageChannel = ""
case "leavemsg":
guildSettings[env.Guild.ID].UserLeaveMessage = ""
guildSettings[env.Guild.ID].UserLeaveMessageChannel = ""
}
}
return NewErrorEmbed("Server Settings Error", "Error finding the setting "+args[0]+".")
}