This repository has been archived by the owner on Sep 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmsgPurge.go
186 lines (152 loc) · 4.28 KB
/
msgPurge.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
package main
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/bwmarrin/discordgo"
)
func init() {
newCommand("purge",
discordgo.PermissionAdministrator|discordgo.PermissionManageMessages|discordgo.PermissionManageServer,
true, msgPurge).setHelp("Args: [number] [@user]\n\nPurges 'number' amount of messages. Optionally, purge only the messages from a given user!\nAdmin only\n\nExample:\n`!owo purge 300`\n" +
"Example 2:\n`!owo purge 300 @Strum355#1180`").add()
}
func msgPurge(s *discordgo.Session, m *discordgo.MessageCreate, msglist []string) {
if len(msglist) < 2 {
s.ChannelMessageSend(m.ChannelID, "Gotta specify a number of messages to delete~")
return
}
purgeAmount, err := strconv.Atoi(msglist[1])
if err != nil {
if strings.HasPrefix(msglist[1], "@") {
msglist[1] = "@" + zerowidth + strings.TrimPrefix(msglist[1], "@")
}
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("How do i delete %s messages? Please only give numbers!", msglist[1]))
return
}
var userToPurge string
if len(msglist) == 3 {
submatch := userIDRegex.FindStringSubmatch(msglist[2])
if len(submatch) == 0 {
s.ChannelMessageSend(m.ChannelID, "Couldn't find that user :(")
return
}
userToPurge = submatch[1]
}
deleteMessage(m.Message, s)
if userToPurge == "" {
err = standardPurge(purgeAmount, s, m)
} else {
err = userPurge(purgeAmount, s, m, userToPurge)
}
if err == nil {
msg, _ := s.ChannelMessageSend(m.ChannelID, "Successfully deleted :ok_hand:")
time.Sleep(time.Second * 5)
deleteMessage(msg, s)
}
}
func getMessages(amount int, id string, s *discordgo.Session) (list []*discordgo.Message, err error) {
list, err = s.ChannelMessages(id, amount, "", "", "")
if err != nil {
log.Error("error getting messages to delete", err)
}
return
}
func standardPurge(purgeAmount int, s *discordgo.Session, m *discordgo.MessageCreate) error {
var outOfDate bool
for purgeAmount > 0 {
list, err := getMessages(purgeAmount%100, m.ChannelID, s)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "There was an issue deleting messages :(")
return err
}
//if more was requested to be deleted than exists
if len(list) == 0 {
break
}
var purgeList []string
for _, msg := range list {
timeSince, err := getMessageAge(msg, s, m)
if err != nil {
//if the time is malformed for whatever reason, we'll try the next message
continue
}
if timeSince.Hours()/24 >= 14 {
outOfDate = true
break
}
purgeList = append(purgeList, msg.ID)
}
if err := massDelete(purgeList, s, m); err != nil {
return err
}
if outOfDate {
break
}
purgeAmount -= len(purgeList)
}
return nil
}
func userPurge(purgeAmount int, s *discordgo.Session, m *discordgo.MessageCreate, userToPurge string) error {
var outOfDate bool
for purgeAmount > 0 {
del := purgeAmount % 100
var purgeList []string
for len(purgeList) < del {
list, err := getMessages(100, m.ChannelID, s)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "There was an issue deleting messages :(")
return err
}
//if more was requested to be deleted than exists
if len(list) == 0 {
break
}
for _, msg := range list {
if len(purgeList) == del {
break
}
if msg.Author.ID != userToPurge {
continue
}
timeSince, err := getMessageAge(msg, s, m)
if err != nil {
//if the time is malformed for whatever reason, we'll try the next message
continue
}
if timeSince.Hours()/24 >= 14 {
outOfDate = true
break
}
purgeList = append(purgeList, msg.ID)
}
if outOfDate {
break
}
}
if err := massDelete(purgeList, s, m); err != nil {
return err
}
if outOfDate {
break
}
purgeAmount -= len(purgeList)
}
return nil
}
func massDelete(list []string, s *discordgo.Session, m *discordgo.MessageCreate) (err error) {
if err = s.ChannelMessagesBulkDelete(m.ChannelID, list); err != nil {
s.ChannelMessageSend(m.ChannelID, "There was an issue deleting messages :(")
log.Error("error purging", err)
}
return
}
func getMessageAge(msg *discordgo.Message, s *discordgo.Session, m *discordgo.MessageCreate) (time.Duration, error) {
then, err := msg.Timestamp.Parse()
if err != nil {
log.Error("error parsing time", err)
return time.Duration(0), err
}
return time.Since(then), nil
}