Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
V4NSH4J
GitHub Repository: V4NSH4J/discord-mass-DM-GO
Path: blob/main/utilities/misc.go
310 views
1
// Copyright (C) 2021 github.com/V4NSH4J
2
//
3
// This source code has been released under the GNU Affero General Public
4
// License v3.0. A copy of this license is available at
5
// https://www.gnu.org/licenses/agpl-3.0.en.html
6
7
package utilities
8
9
import (
10
"encoding/json"
11
"fmt"
12
"io/ioutil"
13
"math/rand"
14
"net/http"
15
"regexp"
16
"strconv"
17
"strings"
18
"time"
19
20
"github.com/Masterminds/semver"
21
)
22
23
func Snowflake() int64 {
24
snowflake := strconv.FormatInt((time.Now().UTC().UnixNano()/1000000)-1420070400000, 2) + "0000000000000000000000"
25
nonce, _ := strconv.ParseInt(snowflake, 2, 64)
26
return nonce
27
}
28
29
func ReverseSnowflake(snowflake string) time.Time {
30
snowflakei, err := strconv.Atoi(snowflake)
31
if err != nil {
32
return time.Time{}
33
}
34
base2 := strconv.FormatInt(int64(snowflakei), 2)
35
if len(base2) < 23 {
36
return time.Time{}
37
}
38
ageBase2 := base2[:len(base2)-22]
39
t, err := strconv.ParseInt(ageBase2, 2, 64)
40
if err != nil {
41
return time.Time{}
42
}
43
t = t + 1420070400000
44
tm := time.UnixMilli(t)
45
return tm
46
47
}
48
49
func Contains(s []string, e string) bool {
50
defer HandleOutOfBounds()
51
if len(s) == 0 {
52
return false
53
}
54
for _, a := range s {
55
if a == e {
56
return true
57
}
58
}
59
return false
60
}
61
62
// Inputs 2 slices of strings and returns a slice of strings which does not contain elements from the second slice
63
func RemoveSubset(s []string, r []string) []string {
64
var n []string
65
for _, v := range s {
66
if !Contains(r, v) {
67
n = append(n, v)
68
}
69
}
70
return n
71
}
72
73
func RemoveDuplicateStr(strSlice []string) []string {
74
allKeys := make(map[string]bool)
75
list := []string{}
76
for _, item := range strSlice {
77
if _, value := allKeys[item]; !value {
78
allKeys[item] = true
79
list = append(list, item)
80
}
81
}
82
return list
83
}
84
85
func HandleOutOfBounds() {
86
if r := recover(); r != nil {
87
fmt.Printf("Recovered from Panic %v", r)
88
}
89
}
90
91
func VersionCheck(version string) {
92
link := "https://pastebin.com/raw/CCaVBSPv"
93
client := &http.Client{Timeout: time.Second * 15}
94
req, err := http.NewRequest("GET", link, nil)
95
if err != nil {
96
return
97
}
98
resp, err := client.Do(req)
99
if err != nil {
100
return
101
}
102
if resp.StatusCode != 200 && resp.StatusCode != 201 && resp.StatusCode != 204 {
103
return
104
}
105
defer resp.Body.Close()
106
body, err := ioutil.ReadAll(resp.Body)
107
if err != nil {
108
return
109
}
110
var response map[string]interface{}
111
err = json.Unmarshal(body, &response)
112
if err != nil {
113
return
114
}
115
v := response["version"].(string)
116
if !strings.Contains(v, ".") {
117
return
118
}
119
message := response["message"].(string)
120
if isSame(v, version) {
121
LogSuccess(" You're Up-to-Date! You're using DMDGO V%v", version)
122
123
} else if isNewer(v, version) {
124
LogSuccess(" You're Up-to-Date! You're using DMDGO BETA V%v", version)
125
} else if isOlder(v, version) {
126
LogErr(" You're using DMDGO V%v, but the latest version is V%v. Consider updating at https://github.com/V4NSH4J/discord-mass-DM-GO/releases", version, v)
127
} else {
128
LogInfo("Unable to check versioning Information. You're on %v and the latest version is %v", version, v)
129
}
130
if message != "" {
131
LogInfo(" %v", message)
132
}
133
134
link = "https://pastebin.com/CCaVBSPv"
135
req, err = http.NewRequest("GET", link, nil)
136
if err != nil {
137
return
138
}
139
resp, err = client.Do(req)
140
if err != nil {
141
return
142
}
143
if resp.StatusCode != 200 && resp.StatusCode != 201 && resp.StatusCode != 204 {
144
return
145
}
146
defer resp.Body.Close()
147
body, err = ioutil.ReadAll(resp.Body)
148
if err != nil {
149
return
150
}
151
r := regexp.MustCompile(`<div class="visits" title="Unique visits to this paste">\n(.+)<\/div>`)
152
matches := r.FindStringSubmatch(string(body))
153
if len(matches) == 0 {
154
return
155
}
156
views := strings.ReplaceAll(matches[1], " ", "")
157
LogSuccess(" DMDGO Users: %v [21-February-2022 - %v]", views, time.Now().Format("02-January-2006"))
158
}
159
160
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()"
161
162
func RandStringBytes(n int) string {
163
b := make([]byte, n)
164
for i := range b {
165
b[i] = letterBytes[rand.Intn(len(letterBytes))]
166
}
167
return string(b)
168
}
169
170
func TimeDifference(t1, t2 time.Time) string {
171
d := t2.Sub(t1)
172
hoursSince := d.Hours()
173
years := hoursSince / (24 * 365)
174
intYears := int(years)
175
remainderYears := years - float64(intYears)
176
months := remainderYears * 12
177
intMonths := int(months)
178
remainderMonths := months - float64(intMonths)
179
days := remainderMonths * 30
180
intDays := int(days)
181
remainderDays := days - float64(intDays)
182
hours := remainderDays * 24
183
intHours := int(hours)
184
return fmt.Sprintf("%v years, %v months, %v days, %v hours", intYears, intMonths, intDays, intHours)
185
}
186
187
func isNewer(check string, current string) bool {
188
c, err := semver.NewConstraint(fmt.Sprintf(`>%v`, check))
189
if err != nil {
190
return false
191
}
192
v, err := semver.NewVersion(current)
193
if err != nil {
194
return false
195
}
196
b, _ := c.Validate(v)
197
return b
198
}
199
200
func isSame(check string, current string) bool {
201
c, err := semver.NewConstraint(fmt.Sprintf(`=%v`, check))
202
if err != nil {
203
return false
204
}
205
v, err := semver.NewVersion(current)
206
if err != nil {
207
return false
208
}
209
b, _ := c.Validate(v)
210
return b
211
}
212
213
func isOlder(check string, current string) bool {
214
c, err := semver.NewConstraint(fmt.Sprintf(`<%v`, check))
215
if err != nil {
216
return false
217
}
218
v, err := semver.NewVersion(current)
219
if err != nil {
220
return false
221
}
222
b, _ := c.Validate(v)
223
return b
224
}
225
226