From 5275d1f2acb29ed4e2e007b78878bccd86da2b50 Mon Sep 17 00:00:00 2001 From: mimu Date: Sun, 9 Oct 2022 11:00:05 +0200 Subject: [PATCH] add pinned command --- commands/pinned.go | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/commands/pinned.go b/commands/pinned.go index 42ec14b..d89c3f2 100644 --- a/commands/pinned.go +++ b/commands/pinned.go @@ -1,12 +1,49 @@ package commands -import tempest "github.com/Amatsagu/Tempest" +import ( + "encoding/json" + "fmt" + "math/rand" + + tempest "github.com/Amatsagu/Tempest" +) var Pinned tempest.Command = tempest.Command{ Name: "pinned", Description: "will show a random pinned message", Options: []tempest.Option{}, SlashCommandHandler: func(itx tempest.CommandInteraction) { + messages, err := getPinnedMessages(itx.Client.Rest, itx.ChannelId.String()) + if err != nil { + itx.SendLinearReply(err.Error(), false) + return + } + messagesCount := len(messages) + + if messagesCount == 0 { + itx.SendLinearReply("no pinned messages", false) + return + } + + idx := rand.Intn(messagesCount) + + itx.Client.SendMessage(itx.ChannelId, messages[idx]) }, } + +func getPinnedMessages(rest tempest.Rest, channelID string) ([]tempest.Message, error) { + route := fmt.Sprintf("/channels/%s/pins", channelID) + bytes, err := rest.Request("GET", route, nil) + if err != nil { + return nil, err + } + + messages := []tempest.Message{} + if err = json.Unmarshal(bytes, &messages); err != nil { + return nil, err + } + + return messages, nil +} +