/
suggest.go
114 lines (102 loc) · 2.32 KB
/
suggest.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
package elements
import (
"database/sql"
"strings"
"github.com/Nv7-Github/Nv7Haven/eod/base"
"github.com/Nv7-Github/Nv7Haven/eod/types"
"github.com/Nv7-Github/Nv7Haven/eod/util"
"github.com/Nv7-Github/sevcord/v2"
"github.com/lib/pq"
)
func (e *Elements) Suggest(c sevcord.Ctx, opts []any) {
c.Acknowledge()
// Autocapitalization
autocap := false
if strings.ToLower(opts[0].(string)) == opts[0].(string) { // Autocapitalize if all lowercase
autocap = true
}
if opts[1] != nil {
autocap = opts[1].(bool)
}
if autocap {
opts[0] = util.Capitalize(opts[0].(string))
}
// Check if result exists already
var id int
var name string
err := e.db.QueryRow(`SELECT id, name FROM elements WHERE guild=$1 AND LOWER(name)=$2`, c.Guild(), strings.ToLower(opts[0].(string))).Scan(&id, &name)
if err != nil {
if err == sql.ErrNoRows {
id = -1
} else {
e.base.Error(c, err)
return
}
}
// Get els
v, res := e.base.GetCombCache(c)
if !res.Ok {
c.Respond(res.Response())
return
}
// Check if combo has result
var exists bool
err = e.db.QueryRow(`SELECT EXISTS(SELECT 1 FROM combos WHERE guild=$1 AND els=$2)`, c.Guild(), pq.Array(v.Elements)).Scan(&exists)
if err != nil {
e.base.Error(c, err)
return
}
if exists {
c.Respond(sevcord.NewMessage("This combo already has a result! " + types.RedCircle))
return
}
// Get res
var idV any
if id == -1 {
idV = opts[0].(string)
// Check if valid
var ok types.Resp
idV, ok = base.CheckName(idV.(string))
if !ok.Ok {
c.Respond(ok.Response())
return
}
} else {
idV = float64(id)
}
// Create suggestion
res = e.polls.CreatePoll(c, &types.Poll{
Kind: types.PollKindCombo,
Data: types.PgData{
"els": util.Map(v.Elements, func(a int) any { return float64(a) }),
"result": idV,
},
})
if !res.Ok {
c.Respond(res.Response())
return
}
// Make text
names, err := e.base.GetNames(v.Elements, c.Guild())
if err != nil {
e.base.Error(c, err)
return
}
text := &strings.Builder{}
text.WriteString("Suggested **")
text.WriteString(strings.Join(names, " + "))
text.WriteString(" = ")
if id != -1 {
text.WriteString(name)
} else {
text.WriteString(opts[0].(string))
}
text.WriteString("** ")
if id != -1 {
text.WriteString("🌟")
} else {
text.WriteString("✨")
}
// Message
c.Respond(sevcord.NewMessage(text.String()))
}