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
/
utilities.go
201 lines (174 loc) · 4.29 KB
/
utilities.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
package main
import (
"math/rand"
"net/http"
"strconv"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"github.com/go-chi/chi"
)
//From Necroforger's dgwidgets
func nextReactionAdd(s *discordgo.Session) chan *discordgo.MessageReactionAdd {
out := make(chan *discordgo.MessageReactionAdd)
s.AddHandlerOnce(func(_ *discordgo.Session, e *discordgo.MessageReactionAdd) {
out <- e
})
return out
}
func nextMessageCreate(s *discordgo.Session) chan *discordgo.MessageCreate {
out := make(chan *discordgo.MessageCreate)
s.AddHandlerOnce(func(_ *discordgo.Session, e *discordgo.MessageCreate) {
out <- e
})
return out
}
func randRange(min, max int) int {
rand.Seed(time.Now().Unix())
if max == 0 {
return 0
}
return rand.Intn(max-min) + min
}
func findIndex(s []string, f string) int {
for i, j := range s {
if j == f {
return i
}
}
return -1
}
func remove(s []string, i int) []string {
s[i] = s[len(s)-1]
return s[:len(s)-1]
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func getCreationTime(ID string) (t time.Time, err error) {
i, err := strconv.ParseInt(ID, 10, 64)
if err != nil {
return
}
timestamp := (i >> 22) + 1420070400000
t = time.Unix(timestamp/1000, 0)
return
}
func codeSeg(s ...string) string {
return "`" + strings.Join(s, " ") + "`"
}
func codeBlock(s ...string) string {
return "```" + strings.Join(s, " ") + "```"
}
func isIn(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func trimSlice(s []string) (ret []string) {
for _, i := range s {
ret = append(ret, strings.TrimSpace(i))
}
return
}
func deleteMessage(m *discordgo.Message, s *discordgo.Session) {
if m != nil {
s.ChannelMessageDelete(m.ChannelID, m.ID)
}
}
func channelDetails(channelID string, s *discordgo.Session) (channelDetails *discordgo.Channel, err error) {
channelDetails, err = s.State.Channel(channelID)
if err != nil {
if err == discordgo.ErrStateNotFound {
channelDetails, err = s.Channel(channelID)
if err != nil {
log.Error("error getting channel details", channelID, err)
}
}
}
return
}
func permissionDetails(authorID, channelID string, s *discordgo.Session) (perms int, err error) {
perms, err = s.State.UserChannelPermissions(authorID, channelID)
if err != nil {
if err == discordgo.ErrStateNotFound {
perms, err = s.UserChannelPermissions(authorID, channelID)
if err != nil {
log.Error("error getting perm details", err)
}
}
}
return
}
func userDetails(memberID string, s *discordgo.Session) (user *discordgo.User, err error) {
user, err = s.User(memberID)
if err != nil {
log.Error("error getting user details", err)
}
return
}
func activePrefix(channelID string, s *discordgo.Session) (prefix string, err error) {
prefix = conf.Prefix
guild, err := guildDetails(channelID, "", s)
if err != nil {
s.ChannelMessageSend(channelID, "There was an issue executing the command :( Try again please~")
return
} else if val, ok := sMap.server(guild.ID); ok && val.Prefix != "" {
prefix = val.Prefix
}
return prefix, nil
}
func memberDetails(guildID, memberID string, s *discordgo.Session) (member *discordgo.Member, err error) {
member, err = s.State.Member(guildID, memberID)
if err != nil {
if err == discordgo.ErrStateNotFound {
member, err = s.GuildMember(guildID, memberID)
if err != nil {
log.Error("error getting member details", err)
}
}
}
return
}
func guildDetails(channelID, guildID string, s *discordgo.Session) (guildDetails *discordgo.Guild, err error) {
if guildID == "" {
var channel *discordgo.Channel
channel, err = channelDetails(channelID, s)
if err != nil {
return
}
guildID = channel.GuildID
}
guildDetails, err = s.State.Guild(guildID)
if err != nil {
if err == discordgo.ErrStateNotFound {
guildDetails, err = s.Guild(guildID)
if err != nil {
log.Error("error getting guild details", guildID, err)
}
}
}
return
}
func isInServer(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
id := chi.URLParam(r, "id")
guild, err := guildDetails(serverID, "", dg)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
for _, member := range guild.Members {
if member.User.ID == id {
w.WriteHeader(http.StatusOK)
return
}
}
w.WriteHeader(http.StatusNotFound)
}