-
Notifications
You must be signed in to change notification settings - Fork 1
/
permissions.go
120 lines (97 loc) · 3.09 KB
/
permissions.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
package permissions
import (
"fmt"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/nicolito128/waffer/pkg/config"
"github.com/nicolito128/waffer/pkg/plugins/commands"
)
// MemberHasPermission checks if a member has the given permission
func MemberHasPermission(s *discordgo.Session, m *discordgo.MessageCreate, cmd *commands.WafferCommand) bool {
p, err := s.UserChannelPermissions(m.Author.ID, m.ChannelID)
if err != nil {
return false
}
if int64(cmd.Data.Permissions.Require)&p == int64(cmd.Data.Permissions.Require) {
return true
}
return false
}
// AllowDM returns true if the command comes from a DM and cmd.AllowDM is true.
func AllowDM(s *discordgo.Session, m *discordgo.MessageCreate, cmd *commands.WafferCommand) bool {
ok, _ := ComesFromDM(s, m)
if cmd.Data != nil {
if ok && !cmd.Data.Permissions.AllowDM {
return false
}
}
return true
}
// ComesFromDM returns true if a message comes from a DM channel
func ComesFromDM(s *discordgo.Session, m *discordgo.MessageCreate) (bool, error) {
channel, err := s.State.Channel(m.ChannelID)
if err != nil {
if channel, err = s.Channel(m.ChannelID); err != nil {
return false, err
}
}
return channel.Type == discordgo.ChannelTypeDM, nil
}
// ValidPrefix returns true if the message starts with the prefix
func ValidPrefix(s *discordgo.Session, m *discordgo.MessageCreate) bool {
prefix := config.Config.Prefix
if strings.HasPrefix(strings.Trim(m.Content, " "), prefix) {
return true
}
return false
}
// Author verified if the message author is a bot or not
func ValidAuthor(s *discordgo.Session, m *discordgo.MessageCreate) bool {
// Ignore all messages created by the bot itself
if m.Author.ID == s.State.User.ID {
return false
}
// Ignore other bots
if m.Author.Bot {
return false
}
return true
}
// MessageHasArguments returns true if the message has the correct arguments length.
func MessageHasArguments(s *discordgo.Session, m *discordgo.MessageCreate, cmd *commands.WafferCommand) bool {
if cmd.Data != nil {
if cmd.Data.RequiredArgs > 0 {
args := strings.Split(strings.Trim(m.Content, " "), " ")
if len(args) < int(cmd.Data.RequiredArgs) {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("You must provide more arguments. Use `%shelp %s` for more information.", config.Config.Prefix, cmd.Data.Name))
return false
}
}
}
return true
}
func OwnerOnly(s *discordgo.Session, m *discordgo.MessageCreate, cmd *commands.WafferCommand) bool {
if cmd.Data != nil {
if cmd.Data.Permissions.OwnerOnly {
if m.Author.ID == config.Config.OwnerID {
return true
} else {
return false
}
}
}
return true
}
func HasHelpPetition(s *discordgo.Session, m *discordgo.MessageCreate, cmd *commands.WafferCommand) bool {
if cmd.Data != nil {
embed, err := cmd.HelpEmbed()
if err != nil {
return true
}
if strings.HasSuffix(m.Content, "--help") || strings.HasSuffix(m.Content, "-h") {
s.ChannelMessageSendEmbed(m.ChannelID, embed)
return false
}
}
return true
}