Skip to content

Commit

Permalink
add read command
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Jul 18, 2024
1 parent b799664 commit 6ddacac
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 1 deletion.
1 change: 1 addition & 0 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var CommandCreates = []discord.ApplicationCommandCreate{
info,
latest,
music,
read,
}

type Commands struct {
Expand Down
76 changes: 76 additions & 0 deletions commands/read.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package commands

import (
"fmt"

"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/handler"
"go.deanishe.net/fuzzy"

"github.com/lavalink-devs/lavalink-bot/internal/res"
)

var read = discord.SlashCommandCreate{
Name: "read",
Description: "Tells someone to read something",
Options: []discord.ApplicationCommandOption{
discord.ApplicationCommandOptionString{
Name: "thing",
Description: "The thing someone should read",
Required: true,
Autocomplete: true,
},
discord.ApplicationCommandOptionUser{
Name: "user",
Description: "The user to tell to read something",
Required: false,
},
},
Contexts: []discord.InteractionContextType{
discord.InteractionContextTypeGuild,
discord.InteractionContextTypeBotDM,
},
}

func (c *Commands) Read(data discord.SlashCommandInteractionData, e *handler.CommandEvent) error {
var msg discord.MessageCreate
user, ok := data.OptUser("user")
if ok {
msg.Content += fmt.Sprintf("Hey %s,\n", user.Mention())
}

thing, ok := c.Things[data.String("thing")]
if !ok {
return e.CreateMessage(discord.MessageCreate{
Content: "I don't know that thing",
Flags: discord.MessageFlagEphemeral,
})
}
msg.Content += thing.Content

for _, file := range thing.Files {
msg.Files = append(msg.Files, discord.NewFile(file.Name, "", file.Reader()))
}

return e.CreateMessage(msg)
}

func (c *Commands) ReadAutocomplete(e *handler.AutocompleteEvent) error {
thing := e.Data.String("thing")

var choices []discord.AutocompleteChoice
for _, t := range c.Things {
choices = append(choices, discord.AutocompleteChoiceString{
Name: res.Trim(t.Name, 100),
Value: t.FileName,
})
}

fuzzy.Sort(Choices(choices), thing)

if len(choices) > 25 {
choices = choices[:25]
}

return e.AutocompleteResult(choices)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/lavalink-devs/lavalink-bot
go 1.22

require (
github.com/adrg/frontmatter v0.2.0
github.com/disgoorg/disgo v0.18.8
github.com/disgoorg/disgolink/v3 v3.0.1-0.20240311001109-56f250c13235
github.com/disgoorg/json v1.1.0
Expand All @@ -20,6 +21,7 @@ require (
)

require (
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/cloudflare/circl v1.3.9 // indirect
github.com/google/go-querystring v1.1.0 // indirect
Expand All @@ -31,4 +33,5 @@ require (
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/ProtonMail/go-crypto v1.0.0 h1:LRuvITjQWX+WIfr930YHG2HNfjR1uOfyf5vE0kC2U78=
github.com/ProtonMail/go-crypto v1.0.0/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0=
github.com/adrg/frontmatter v0.2.0 h1:/DgnNe82o03riBd1S+ZDjd43wAmC6W35q67NHeLkPd4=
github.com/adrg/frontmatter v0.2.0/go.mod h1:93rQCj3z3ZlwyxxpQioRKC1wDLto4aXHrbqIsnH9wmE=
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
github.com/cloudflare/circl v1.3.9 h1:QFrlgFYf2Qpi8bSpVPK1HBvWpx16v/1TZivyo7pGuBE=
Expand Down Expand Up @@ -108,6 +113,9 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1 change: 1 addition & 0 deletions lavalinkbot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Bot struct {
GitHub *github.Client
MusicQueue *PlayerManager
Webhooks map[string]webhook.Client
Things map[string]Thing
}

func (b *Bot) Start() error {
Expand Down
85 changes: 85 additions & 0 deletions lavalinkbot/thing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package lavalinkbot

import (
"bytes"
"cmp"
"embed"
"fmt"
"io"
"path/filepath"

"github.com/adrg/frontmatter"
)

func ReadThings(things embed.FS) (map[string]Thing, error) {
files, err := things.ReadDir("things")
if err != nil {
return nil, err
}

thingMap := make(map[string]Thing, len(files))
for _, file := range files {
if !file.IsDir() {
continue
}
thingFiles, err := things.ReadDir(filepath.Join("things", file.Name()))
if err != nil {
return nil, err
}

var thing Thing
for _, f := range thingFiles {
if f.Name() == "index.md" {
data, err := things.ReadFile(filepath.Join("things", file.Name(), "index.md"))
if err != nil {
return nil, fmt.Errorf("failed to read index.md for %s: %w", file.Name(), err)
}

var matter thingMatter
data, err = frontmatter.Parse(bytes.NewBuffer(data), &matter)
if err != nil {
return nil, fmt.Errorf("failed to parse frontmatter for %s: %w", file.Name(), err)
}

thing.Name = cmp.Or(matter.Name, file.Name())
thing.FileName = file.Name()
thing.Content = string(data)
continue
}

data, err := things.ReadFile(filepath.Join("things", file.Name(), f.Name()))
if err != nil {
return nil, fmt.Errorf("failed to read file %s for %s: %w", f.Name(), file.Name(), err)
}

thing.Files = append(thing.Files, ThingFile{
Name: f.Name(),
Buf: data,
})
}

thingMap[file.Name()] = thing
}

return thingMap, nil
}

type thingMatter struct {
Name string `yaml:"name"`
}

type Thing struct {
Name string
FileName string
Content string
Files []ThingFile
}

type ThingFile struct {
Name string
Buf []byte
}

func (t ThingFile) Reader() io.Reader {
return bytes.NewReader(t.Buf)
}
15 changes: 14 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"embed"
"errors"
"flag"
"log/slog"
Expand Down Expand Up @@ -30,6 +31,9 @@ import (
"github.com/lavalink-devs/lavalink-bot/routes"
)

//go:embed things
var Things embed.FS

func main() {
path := flag.String("config", "config.yml", "path to config.yml")
flag.Parse()
Expand All @@ -43,21 +47,30 @@ func main() {
slog.Info("starting lavalink-bot...", slog.String("disgo_version", disgo.Version), slog.String("disgolink_version", disgolink.Version))
slog.Info("Config", slog.String("path", *path), slog.String("config", cfg.String()))

things, err := lavalinkbot.ReadThings(Things)
if err != nil {
slog.Error("failed to read things", tint.Err(err))
os.Exit(-1)
}

b := &lavalinkbot.Bot{
Cfg: cfg,
GitHub: github.NewClient(nil),
Maven: maven.New(&http.Client{
Timeout: 10 * time.Second,
}),
MusicQueue: lavalinkbot.NewPlayerManager(),
Webhooks: map[string]webhook.Client{},
Webhooks: make(map[string]webhook.Client),
Things: things,
}

cmds := &commands.Commands{Bot: b}
r := handler.New()
r.Use(middleware.Go)
r.SlashCommand("/info/bot", cmds.InfoBot)
r.SlashCommand("/info/lavalink", cmds.InfoLavalink)
r.SlashCommand("/read", cmds.Read)
r.Autocomplete("/read", cmds.ReadAutocomplete)
r.SlashCommand("/latest", cmds.Latest)
r.Autocomplete("/latest", cmds.LatestAutocomplete)
r.SlashCommand("/decode", cmds.Decode)
Expand Down
Binary file added things/post_guidelines/img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions things/post_guidelines/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
name: Post Guidelines
---

Please make sure to read and follow the post guidelines before opening your post.
This will help you get the best possible answers to your questions while minimizing the questions we need to ask you.
Thanks!

0 comments on commit 6ddacac

Please sign in to comment.