Issues (115)

bot/role.go (1 issue)

Severity
1
package bot
2
3
import (
4
	"fmt"
5
6
	"github.com/bwmarrin/discordgo"
7
)
8
9
// UserRoles struct with array of user roles in guild
10
type UserRoles struct {
11
	Roles []*discordgo.Role
12
}
13
14
// IsAdmin returns true if user is admin
15
func (ctx *Context) IsAdmin() bool {
16
	return ctx.User.ID == ctx.Conf.General.AdminID
17
}
18
19
func (ctx *Context) IsServerAdmin() bool {
0 ignored issues
show
exported method Context.IsServerAdmin should have comment or be unexported
Loading history...
20
	if ctx.User.ID == ctx.Guild.OwnerID || ctx.GetRoles().ExistsName("bot.admin") || ctx.IsAdmin() {
21
		return true
22
	}
23
	return false
24
}
25
26
// GetRoles returns UserRoles struct pointer
27
func (ctx *Context) GetRoles() *UserRoles {
28
	var userRoles = new(UserRoles)
29
	memb, err := ctx.Discord.GuildMember(ctx.Guild.ID, ctx.User.ID)
30
	if err != nil {
31
		fmt.Println("Getting member error: " + err.Error())
32
	}
33
	for _, grole := range ctx.Guild.Roles {
34
		for _, urole := range memb.Roles {
35
			if grole.ID == urole {
36
				userRoles.Roles = append(userRoles.Roles, grole)
37
			}
38
		}
39
	}
40
	return userRoles
41
}
42
43
// ExistsName checks if user role nema exists on user
44
func (r *UserRoles) ExistsName(name string) bool {
45
	for _, val := range r.Roles {
46
		if val.Name == name {
47
			return true
48
		}
49
	}
50
	return false
51
}
52