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