From c282ebb32c07f2297ae5598ec9b30f126972ca7f Mon Sep 17 00:00:00 2001 From: mimu Date: Tue, 11 Oct 2022 00:10:57 +0200 Subject: [PATCH] store data --- commands/pinned.go | 61 ++++++++++++++++++++++++++++++++++------------ main.go | 19 ++++++++++++--- 2 files changed, 62 insertions(+), 18 deletions(-) diff --git a/commands/pinned.go b/commands/pinned.go index ec7d22e..3c42eba 100644 --- a/commands/pinned.go +++ b/commands/pinned.go @@ -9,37 +9,68 @@ import ( tempest "github.com/Amatsagu/Tempest" ) -const timeout time.Duration = 10000000000 +type MessageOfTheDay struct { + Date time.Time + Message tempest.Message +} + +type PinnedData struct { + Messages []tempest.Message + Count int + MessageOfTheDay *MessageOfTheDay +} + +// not ideal but good enough for this +var data map[string]*PinnedData = make(map[string]*PinnedData) + +func InitPinned(c tempest.Client, channelIDs []tempest.Snowflake) error { + for _, cid := range channelIDs { + messages, err := getPinnedMessages(c.Rest, cid.String()) + if err != nil { + return err + } + + data[cid.String()] = &PinnedData{ + Messages: messages, + Count: len(messages), + } + } + + return nil +} var Pinned tempest.Command = tempest.Command{ Name: "pinned", Description: "will show a random pinned message", Options: []tempest.Option{}, SlashCommandHandler: func(itx tempest.CommandInteraction) { - - _, close := itx.Client.AwaitComponent([]string{itx.Data.CustomId}, timeout) - - messages, err := getPinnedMessages(itx.Client.Rest, itx.ChannelId.String()) - if err != nil { - itx.SendLinearReply(err.Error(), false) - return + channelData, exist := data[itx.ChannelId.String()] + if !exist { + itx.SendLinearReply("no data for this channel", false) } - messagesCount := len(messages) - - if messagesCount == 0 { + if channelData.Count == 0 { itx.SendLinearReply("no pinned messages", false) return } - idx := rand.Intn(messagesCount) + today := time.Now() + todayDate := time.Now().Format("2006/01/02") + currentMessageDate := channelData.MessageOfTheDay.Date.Format("2006/01/02") - if _, err = itx.Client.SendMessage(itx.ChannelId, messages[idx]); err != nil { - itx.SendLinearReply(err.Error(), false) + if channelData.MessageOfTheDay == nil || todayDate != currentMessageDate { + newMessage := channelData.Messages[rand.Intn(channelData.Count)] + + channelData.MessageOfTheDay = &MessageOfTheDay{ + Date: today, + Message: newMessage, + } } - close() + if _, err := itx.Client.SendMessage(itx.ChannelId, channelData.MessageOfTheDay.Message); err != nil { + itx.SendLinearReply(err.Error(), false) + } }, } diff --git a/main.go b/main.go index 46ca897..4169eb0 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,10 @@ import ( tempest "github.com/Amatsagu/Tempest" ) +var channelIDs []tempest.Snowflake = []tempest.Snowflake{ + 992760761812258868, // test server +} + func main() { env := getEnvVariables() @@ -32,12 +36,13 @@ func main() { }, }) + if err := initialize(client); err != nil { + panic(err) + } client.RegisterCommand(commands.Pinned) - client.SyncCommands([]tempest.Snowflake{ - 992760761812258868, // test server - }, nil, false) + client.SyncCommands(channelIDs, nil, false) addr := fmt.Sprintf("%s:%s", env.Addr, env.Port) @@ -49,6 +54,14 @@ func main() { } +func initialize(c tempest.Client) error { + if err := commands.InitPinned(c, channelIDs); err != nil { + return err + } + + return nil +} + func getEnvVariables() Env { if os.Getenv("RAILWAY_ENVIRONMENT") == "production" { return Env{