add pinned command

This commit is contained in:
mimu
2022-10-09 11:00:05 +02:00
parent 3f84072e72
commit 5275d1f2ac
+38 -1
View File
@@ -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
}