Skip to content

Commit 6ddacac

Browse files
committed
add read command
1 parent b799664 commit 6ddacac

File tree

9 files changed

+195
-1
lines changed

9 files changed

+195
-1
lines changed

commands/commands.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var CommandCreates = []discord.ApplicationCommandCreate{
1212
info,
1313
latest,
1414
music,
15+
read,
1516
}
1617

1718
type Commands struct {

commands/read.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/disgoorg/disgo/discord"
7+
"github.com/disgoorg/disgo/handler"
8+
"go.deanishe.net/fuzzy"
9+
10+
"github.com/lavalink-devs/lavalink-bot/internal/res"
11+
)
12+
13+
var read = discord.SlashCommandCreate{
14+
Name: "read",
15+
Description: "Tells someone to read something",
16+
Options: []discord.ApplicationCommandOption{
17+
discord.ApplicationCommandOptionString{
18+
Name: "thing",
19+
Description: "The thing someone should read",
20+
Required: true,
21+
Autocomplete: true,
22+
},
23+
discord.ApplicationCommandOptionUser{
24+
Name: "user",
25+
Description: "The user to tell to read something",
26+
Required: false,
27+
},
28+
},
29+
Contexts: []discord.InteractionContextType{
30+
discord.InteractionContextTypeGuild,
31+
discord.InteractionContextTypeBotDM,
32+
},
33+
}
34+
35+
func (c *Commands) Read(data discord.SlashCommandInteractionData, e *handler.CommandEvent) error {
36+
var msg discord.MessageCreate
37+
user, ok := data.OptUser("user")
38+
if ok {
39+
msg.Content += fmt.Sprintf("Hey %s,\n", user.Mention())
40+
}
41+
42+
thing, ok := c.Things[data.String("thing")]
43+
if !ok {
44+
return e.CreateMessage(discord.MessageCreate{
45+
Content: "I don't know that thing",
46+
Flags: discord.MessageFlagEphemeral,
47+
})
48+
}
49+
msg.Content += thing.Content
50+
51+
for _, file := range thing.Files {
52+
msg.Files = append(msg.Files, discord.NewFile(file.Name, "", file.Reader()))
53+
}
54+
55+
return e.CreateMessage(msg)
56+
}
57+
58+
func (c *Commands) ReadAutocomplete(e *handler.AutocompleteEvent) error {
59+
thing := e.Data.String("thing")
60+
61+
var choices []discord.AutocompleteChoice
62+
for _, t := range c.Things {
63+
choices = append(choices, discord.AutocompleteChoiceString{
64+
Name: res.Trim(t.Name, 100),
65+
Value: t.FileName,
66+
})
67+
}
68+
69+
fuzzy.Sort(Choices(choices), thing)
70+
71+
if len(choices) > 25 {
72+
choices = choices[:25]
73+
}
74+
75+
return e.AutocompleteResult(choices)
76+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/lavalink-devs/lavalink-bot
33
go 1.22
44

55
require (
6+
github.com/adrg/frontmatter v0.2.0
67
github.com/disgoorg/disgo v0.18.8
78
github.com/disgoorg/disgolink/v3 v3.0.1-0.20240311001109-56f250c13235
89
github.com/disgoorg/json v1.1.0
@@ -20,6 +21,7 @@ require (
2021
)
2122

2223
require (
24+
github.com/BurntSushi/toml v1.4.0 // indirect
2325
github.com/ProtonMail/go-crypto v1.0.0 // indirect
2426
github.com/cloudflare/circl v1.3.9 // indirect
2527
github.com/google/go-querystring v1.1.0 // indirect
@@ -31,4 +33,5 @@ require (
3133
golang.org/x/oauth2 v0.21.0 // indirect
3234
golang.org/x/sys v0.22.0 // indirect
3335
golang.org/x/text v0.16.0 // indirect
36+
gopkg.in/yaml.v2 v2.4.0 // indirect
3437
)

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
2+
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
3+
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
14
github.com/ProtonMail/go-crypto v1.0.0 h1:LRuvITjQWX+WIfr930YHG2HNfjR1uOfyf5vE0kC2U78=
25
github.com/ProtonMail/go-crypto v1.0.0/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0=
6+
github.com/adrg/frontmatter v0.2.0 h1:/DgnNe82o03riBd1S+ZDjd43wAmC6W35q67NHeLkPd4=
7+
github.com/adrg/frontmatter v0.2.0/go.mod h1:93rQCj3z3ZlwyxxpQioRKC1wDLto4aXHrbqIsnH9wmE=
38
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
49
github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
510
github.com/cloudflare/circl v1.3.9 h1:QFrlgFYf2Qpi8bSpVPK1HBvWpx16v/1TZivyo7pGuBE=
@@ -108,6 +113,9 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
108113
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
109114
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
110115
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
116+
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
117+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
118+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
111119
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
112120
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
113121
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

lavalinkbot/bot.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Bot struct {
2424
GitHub *github.Client
2525
MusicQueue *PlayerManager
2626
Webhooks map[string]webhook.Client
27+
Things map[string]Thing
2728
}
2829

2930
func (b *Bot) Start() error {

lavalinkbot/thing.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package lavalinkbot
2+
3+
import (
4+
"bytes"
5+
"cmp"
6+
"embed"
7+
"fmt"
8+
"io"
9+
"path/filepath"
10+
11+
"github.com/adrg/frontmatter"
12+
)
13+
14+
func ReadThings(things embed.FS) (map[string]Thing, error) {
15+
files, err := things.ReadDir("things")
16+
if err != nil {
17+
return nil, err
18+
}
19+
20+
thingMap := make(map[string]Thing, len(files))
21+
for _, file := range files {
22+
if !file.IsDir() {
23+
continue
24+
}
25+
thingFiles, err := things.ReadDir(filepath.Join("things", file.Name()))
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
var thing Thing
31+
for _, f := range thingFiles {
32+
if f.Name() == "index.md" {
33+
data, err := things.ReadFile(filepath.Join("things", file.Name(), "index.md"))
34+
if err != nil {
35+
return nil, fmt.Errorf("failed to read index.md for %s: %w", file.Name(), err)
36+
}
37+
38+
var matter thingMatter
39+
data, err = frontmatter.Parse(bytes.NewBuffer(data), &matter)
40+
if err != nil {
41+
return nil, fmt.Errorf("failed to parse frontmatter for %s: %w", file.Name(), err)
42+
}
43+
44+
thing.Name = cmp.Or(matter.Name, file.Name())
45+
thing.FileName = file.Name()
46+
thing.Content = string(data)
47+
continue
48+
}
49+
50+
data, err := things.ReadFile(filepath.Join("things", file.Name(), f.Name()))
51+
if err != nil {
52+
return nil, fmt.Errorf("failed to read file %s for %s: %w", f.Name(), file.Name(), err)
53+
}
54+
55+
thing.Files = append(thing.Files, ThingFile{
56+
Name: f.Name(),
57+
Buf: data,
58+
})
59+
}
60+
61+
thingMap[file.Name()] = thing
62+
}
63+
64+
return thingMap, nil
65+
}
66+
67+
type thingMatter struct {
68+
Name string `yaml:"name"`
69+
}
70+
71+
type Thing struct {
72+
Name string
73+
FileName string
74+
Content string
75+
Files []ThingFile
76+
}
77+
78+
type ThingFile struct {
79+
Name string
80+
Buf []byte
81+
}
82+
83+
func (t ThingFile) Reader() io.Reader {
84+
return bytes.NewReader(t.Buf)
85+
}

main.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"embed"
45
"errors"
56
"flag"
67
"log/slog"
@@ -30,6 +31,9 @@ import (
3031
"github.com/lavalink-devs/lavalink-bot/routes"
3132
)
3233

34+
//go:embed things
35+
var Things embed.FS
36+
3337
func main() {
3438
path := flag.String("config", "config.yml", "path to config.yml")
3539
flag.Parse()
@@ -43,21 +47,30 @@ func main() {
4347
slog.Info("starting lavalink-bot...", slog.String("disgo_version", disgo.Version), slog.String("disgolink_version", disgolink.Version))
4448
slog.Info("Config", slog.String("path", *path), slog.String("config", cfg.String()))
4549

50+
things, err := lavalinkbot.ReadThings(Things)
51+
if err != nil {
52+
slog.Error("failed to read things", tint.Err(err))
53+
os.Exit(-1)
54+
}
55+
4656
b := &lavalinkbot.Bot{
4757
Cfg: cfg,
4858
GitHub: github.NewClient(nil),
4959
Maven: maven.New(&http.Client{
5060
Timeout: 10 * time.Second,
5161
}),
5262
MusicQueue: lavalinkbot.NewPlayerManager(),
53-
Webhooks: map[string]webhook.Client{},
63+
Webhooks: make(map[string]webhook.Client),
64+
Things: things,
5465
}
5566

5667
cmds := &commands.Commands{Bot: b}
5768
r := handler.New()
5869
r.Use(middleware.Go)
5970
r.SlashCommand("/info/bot", cmds.InfoBot)
6071
r.SlashCommand("/info/lavalink", cmds.InfoLavalink)
72+
r.SlashCommand("/read", cmds.Read)
73+
r.Autocomplete("/read", cmds.ReadAutocomplete)
6174
r.SlashCommand("/latest", cmds.Latest)
6275
r.Autocomplete("/latest", cmds.LatestAutocomplete)
6376
r.SlashCommand("/decode", cmds.Decode)

things/post_guidelines/img.png

384 KB
Loading

things/post_guidelines/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
name: Post Guidelines
3+
---
4+
5+
Please make sure to read and follow the post guidelines before opening your post.
6+
This will help you get the best possible answers to your questions while minimizing the questions we need to ask you.
7+
Thanks!

0 commit comments

Comments
 (0)