-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
195 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |