-
Notifications
You must be signed in to change notification settings - Fork 908
/
findserver.go
82 lines (68 loc) · 2.02 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
package findserver
import (
"fmt"
"strings"
"github.com/botlabs-gg/yagpdb/v2/bot/models"
"github.com/botlabs-gg/yagpdb/v2/commands"
"github.com/botlabs-gg/yagpdb/v2/lib/dcmd"
"github.com/botlabs-gg/yagpdb/v2/lib/dstate"
"github.com/botlabs-gg/yagpdb/v2/stdcommands/util"
"github.com/volatiletech/sqlboiler/v4/queries/qm"
)
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. Bot Admin Only",
HideFromHelp: true,
ArgSwitches: []*dcmd.ArgDef{
{Name: "name", Type: dcmd.String, Default: ""},
{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.GuildSet, nameToMatch string, userToMatch int64) *Candidate {
if nameToMatch != "" {
gl := strings.ToLower(gs.Name)
if gl != nameToMatch && !strings.Contains(gl, nameToMatch) {
return nil
}
}
candidate := &Candidate{
ID: gs.ID,
Name: gs.Name,
}
return candidate
}