Instead of scanning for message, use store to save its ID

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-08-07 17:00:49 +02:00
parent d8abfbac35
commit 218a202fa7
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
2 changed files with 21 additions and 15 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
config.yaml config.yaml
discord-community discord-community
.env .env
store.json

View file

@ -19,7 +19,6 @@ import (
const ( const (
streamScheduleDefaultColor = 0x2ECC71 streamScheduleDefaultColor = 0x2ECC71
streamScheduleNumberOfMessagesToLoad = 100
) )
var ( var (
@ -85,6 +84,9 @@ func (m modStreamSchedule) cronUpdateSchedule() {
return return
} }
// @attr discord_channel_id required string "" ID of the Discord channel to post the message to
channelID := m.attrs.MustString("discord_channel_id", nil)
msgEmbed := &discordgo.MessageEmbed{ msgEmbed := &discordgo.MessageEmbed{
// @attr embed_color optional int64 "0x2ECC71" Integer representation of the hex color for the embed // @attr embed_color optional int64 "0x2ECC71" Integer representation of the hex color for the embed
Color: int(m.attrs.MustInt64("embed_color", ptrInt64(streamScheduleDefaultColor))), Color: int(m.attrs.MustInt64("embed_color", ptrInt64(streamScheduleDefaultColor))),
@ -127,20 +129,18 @@ func (m modStreamSchedule) cronUpdateSchedule() {
} }
} }
// @attr discord_channel_id required string "" ID of the Discord channel to post the message to
msgs, err := m.discord.ChannelMessages(m.attrs.MustString("discord_channel_id", nil), streamScheduleNumberOfMessagesToLoad, "", "", "")
if err != nil {
log.WithError(err).Error("Unable to fetch announcement channel messages")
return
}
var managedMsg *discordgo.Message var managedMsg *discordgo.Message
for _, msg := range msgs { if err = store.ReadWithLock(m.id, func(a moduleAttributeStore) error {
if len(msg.Embeds) == 0 || msg.Embeds[0].Title != msgEmbed.Title { mid, err := a.String("message_id")
continue if err == errValueNotSet {
return nil
} }
managedMsg = msg managedMsg, err = m.discord.ChannelMessage(channelID, mid)
return errors.Wrap(err, "fetching managed message")
}); err != nil {
log.WithError(err).Error("Unable to fetch managed message for stream schedule")
return
} }
if managedMsg != nil { if managedMsg != nil {
@ -151,15 +151,20 @@ func (m modStreamSchedule) cronUpdateSchedule() {
return return
} }
_, err = m.discord.ChannelMessageEditEmbed(m.attrs.MustString("discord_channel_id", nil), managedMsg.ID, msgEmbed) _, err = m.discord.ChannelMessageEditEmbed(channelID, managedMsg.ID, msgEmbed)
} else { } else {
_, err = m.discord.ChannelMessageSendEmbed(m.attrs.MustString("discord_channel_id", nil), msgEmbed) managedMsg, err = m.discord.ChannelMessageSendEmbed(channelID, msgEmbed)
} }
if err != nil { if err != nil {
log.WithError(err).Error("Unable to announce streamplan") log.WithError(err).Error("Unable to announce streamplan")
return return
} }
if err = store.Set(m.id, "message_id", managedMsg.ID); err != nil {
log.WithError(err).Error("Unable to store managed message id")
return
}
log.Info("Updated Stream Schedule") log.Info("Updated Stream Schedule")
} }