-
Notifications
You must be signed in to change notification settings - Fork 927
/
findserver.go
113 lines (94 loc) · 2.66 KB
/
findserver.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
package findserver
import (
"fmt"
"github.com/jonas747/dcmd"
"github.com/jonas747/discordgo"
"github.com/jonas747/dstate"
"github.com/jonas747/yagpdb/bot/models"
"github.com/jonas747/yagpdb/commands"
"github.com/jonas747/yagpdb/stdcommands/util"
"github.com/volatiletech/sqlboiler/queries/qm"
"strings"
)
type Candidate struct {
ID int64
Name string
UserMatch bool
Owner bool
Admin bool
Mod bool
}
var Command = &commands.YAGCommand{
Cooldown: 2,
CmdCategory: commands.CategoryDebug,
HideFromCommandsPage: true,
Name: "findserver",
Aliases: []string{"findservers"},
Description: "Looks for a server by server name or the servers a user owns",
HideFromHelp: true,
ArgSwitches: []*dcmd.ArgDef{
&dcmd.ArgDef{Switch: "name", Name: "name", Type: dcmd.String, Default: ""},
&dcmd.ArgDef{Switch: "user", Name: "user", Type: dcmd.UserID, Default: 0},
},
RunFunc: util.RequireBotAdmin(func(data *dcmd.Data) (interface{}, error) {
nameToMatch := strings.ToLower(data.Switch("name").Str())
userIDToMatch := data.Switch("user").Int64()
if userIDToMatch == 0 && nameToMatch == "" {
return "-name or -user not provided", nil
}
var whereQM qm.QueryMod
if userIDToMatch != 0 {
whereQM = qm.Where("owner_id = ?", userIDToMatch)
} else {
whereQM = qm.Where("name ILIKE ?", "%"+nameToMatch+"%")
}
results, err := models.JoinedGuilds(qm.Where("left_at is null"), whereQM, qm.OrderBy("id desc"), qm.Limit(250)).AllG(data.Context())
if err != nil {
return nil, err
}
resp := ""
for _, v := range results {
resp += fmt.Sprintf("`%d`: **%s**\n", v.ID, v.Name)
}
resp += fmt.Sprintf("%d results", len(results))
return resp, nil
}),
}
func CheckGuild(gs *dstate.GuildState, nameToMatch string, userToMatch int64) *Candidate {
if nameToMatch != "" {
gl := strings.ToLower(gs.Guild.Name)
if gl != nameToMatch && !strings.Contains(gl, nameToMatch) {
return nil
}
}
foundUser := false
if userToMatch != 0 {
for _, ms := range gs.Members {
if ms.ID == userToMatch {
foundUser = true
break
}
}
if !foundUser {
return nil
}
}
candidate := &Candidate{
ID: gs.ID,
Name: gs.Guild.Name,
}
if foundUser {
if gs.Guild.OwnerID == userToMatch {
candidate.Owner = true
}
perms, _ := gs.MemberPermissions(false, 0, userToMatch)
if perms&discordgo.PermissionAdministrator != 0 {
candidate.Admin = true
}
if perms&discordgo.PermissionManageServer != 0 || perms&discordgo.PermissionKickMembers != 0 || perms&discordgo.PermissionBanMembers != 0 {
candidate.Mod = true
}
candidate.UserMatch = true
}
return candidate
}