store data

This commit is contained in:
mimu
2022-10-11 00:10:57 +02:00
parent 28c9b2aeae
commit c282ebb32c
2 changed files with 62 additions and 18 deletions
+46 -15
View File
@@ -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)
}
},
}
+16 -3
View File
@@ -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{