forked from Clinet/clinet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathothers.go
102 lines (90 loc) · 2.56 KB
/
others.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
package main
import (
"math"
"regexp"
"strconv"
"time"
"github.com/bwmarrin/discordgo"
)
// MemberHasPermission checks if a member has the given permission
// for example, If you would like to check if user has the administrator
// permission you would use
// --- MemberHasPermission(s, guildID, userID, discordgo.PermissionAdministrator)
// If you want to check for multiple permissions you would use the bitwise OR
// operator to pack more bits in. (e.g): PermissionAdministrator|PermissionAddReactions
// =================================================================================
// s : discordgo session
// guildID : guildID of the member you wish to check the roles of
// userID : userID of the member you wish to retrieve
// permission : the permission you wish to check for
func MemberHasPermission(s *discordgo.Session, guildID string, userID string, permission int) (bool, error) {
member, err := s.State.Member(guildID, userID)
if err != nil {
if member, err = s.GuildMember(guildID, userID); err != nil {
return false, err
}
}
// Iterate through the role IDs stored in member.Roles
// to check permissions
for _, roleID := range member.Roles {
role, err := s.State.Role(guildID, roleID)
if err != nil {
return false, err
}
if role.Permissions&permission != 0 {
return true, nil
}
}
return false, nil
}
type CaseInsensitiveReplacer struct {
toReplace *regexp.Regexp
replaceWith string
}
func NewCaseInsensitiveReplacer(toReplace, with string) *CaseInsensitiveReplacer {
return &CaseInsensitiveReplacer{
toReplace: regexp.MustCompile("(?i)" + toReplace),
replaceWith: with,
}
}
func (cir *CaseInsensitiveReplacer) Replace(str string) string {
return cir.toReplace.ReplaceAllString(str, cir.replaceWith)
}
func zeroPad(str string) (result string) {
if len(str) < 2 {
result = "0" + str
} else {
result = str
}
return
}
func secondsToHuman(input float64) (result string) {
hours := math.Floor(float64(input) / 60 / 60)
seconds := int(input) % (60 * 60)
minutes := math.Floor(float64(seconds) / 60)
seconds = int(input) % 60
if hours > 0 {
result = strconv.Itoa(int(hours)) + ":" + zeroPad(strconv.Itoa(int(minutes))) + ":" + zeroPad(strconv.Itoa(int(seconds)))
} else {
result = zeroPad(strconv.Itoa(int(minutes))) + ":" + zeroPad(strconv.Itoa(int(seconds)))
}
return
}
func roundTime(d, r time.Duration) time.Duration {
if r <= 0 {
return d
}
neg := d < 0
if neg {
d = -d
}
if m := d % r; m+m < r {
d = d - m
} else {
d = d + r - m
}
if neg {
return -d
}
return d
}