-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (122 loc) · 4.05 KB
/
main.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
)
func main() {
if err := godotenv.Load(); err != nil {
log.Fatal("Error loading .env file")
}
dg, err := discordgo.New("Bot " + os.Getenv("LOREKEEPER_TOKEN"))
if err != nil {
log.Fatal(err)
}
dg.AddHandler(messageCreate)
dg.Identify.Intents = discordgo.IntentsGuildMessages
err = dg.Open()
if err != nil {
log.Fatal(err)
}
defer dg.Close()
log.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
}
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
if m.Author.ID == s.State.User.ID {
return
}
// Users tried [[cardname]], [[!cardname]], [[ cardname ]], [[! cardname ]], support them all
re := regexp.MustCompile(`\[\[\!?([^|\]]+)\|?([^\]]*)\]\]`)
cardQuerys := re.FindAllStringSubmatch(m.Content, -1)
for _, cardQuery := range cardQuerys {
cardName := strings.Replace(strings.TrimSpace(cardQuery[1]), "’", "'", -1)
imageFormat := strings.TrimSpace(cardQuery[2])
if strings.ToLower(cardName) == "help" {
s.ChannelMessageSend(m.ChannelID, getHelpText())
} else {
parsedImageFormat, errMessage := validateImageFormat(imageFormat)
if errMessage != "" {
s.ChannelMessageSend(m.ChannelID, errMessage)
}
cardUrl, err := getCardUrl(cardName, parsedImageFormat)
if err != nil {
log.Println(err)
}
s.ChannelMessageSend(m.ChannelID, cardUrl)
}
}
}
func validateImageFormat(imageFormat string) (string, string) {
switch imageFormat {
case "foil_full_art", "ffa":
return "foil_full_art", ""
case "foil_text", "ft":
return "foil_text", ""
case "full_art", "fa":
return "full_art", ""
case "text", "t", "":
return "text", ""
default:
return "text", "Unknown image format, using 'text' instead"
}
}
func getCardUrl(cardName, imageFormat string) (string, error) {
result, err := searchSpellslingerer(fmt.Sprintf(`(name~"%s")`, cardName))
if err != nil {
return "", err
}
if result.TotalItems < 1 {
return fmt.Sprintf(`No cards found for "%s", please check your spelling`, cardName), nil
}
if result.TotalItems == 1 {
return fmt.Sprintf("https://spellslingerer.com/cards/%s?image=%s", result.Items[0]["id"], imageFormat), nil
}
for _, item := range result.Items {
if strings.ToLower(item["name"].(string)) == strings.ToLower(cardName) {
return fmt.Sprintf("https://spellslingerer.com/cards/%s?image=%s", item["id"], imageFormat), nil
}
}
return fmt.Sprintf(`More than one card found for "%s", please be more specific`, cardName), nil
}
// Copied from https://github.com/pocketbase/pocketbase/blob/v0.13.2/tools/search/provider.go#L27-L33
type Result struct {
Page int `json:"page"`
PerPage int `json:"perPage"`
TotalItems int `json:"totalItems"`
TotalPages int `json:"totalPages"`
Items []map[string]interface{} `json:"items"`
}
func searchSpellslingerer(filter string) (Result, error) {
url := fmt.Sprintf(`https://spellslingerer.com/api/collections/cards/records?filter=%s`, filter)
res, err := http.Get(url)
if err != nil {
return Result{}, err
}
defer res.Body.Close()
var result Result
err = json.NewDecoder(res.Body).Decode(&result)
return result, err
}
func getHelpText() string {
return `Hey spellslinger! Here's how to ask me for a card:
- [[card name]] or [[!card name]] will work
- Only part of the name will work, as long as it uniquely identifies the card
- If you want fancy art, specify it like [[card name|format]]
- Format can be one of "text", "full_art", "foil_text", "foil_full_art"
- You can also use the abbreviations "t", "fa", "ft", "ffa" instead
- If you specify anything else I'll use "text"
- You can add spaces around the name and format if you like ([[ card name | format ]]), but don't put extra spaces in the middle of the name
`
}