mirror of
https://github.com/Luzifer/discord-community.git
synced 2024-12-20 18:31:23 +00:00
Fix: Lock live-posts to prevent double posting
when triggered through status and cron Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
68e590b8ce
commit
3e3af3a645
1 changed files with 12 additions and 6 deletions
|
@ -5,6 +5,7 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Luzifer/go_helpers/v2/str"
|
"github.com/Luzifer/go_helpers/v2/str"
|
||||||
|
@ -37,9 +38,11 @@ type modLivePosting struct {
|
||||||
attrs moduleAttributeStore
|
attrs moduleAttributeStore
|
||||||
discord *discordgo.Session
|
discord *discordgo.Session
|
||||||
id string
|
id string
|
||||||
|
|
||||||
|
lock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m modLivePosting) ID() string { return m.id }
|
func (m *modLivePosting) ID() string { return m.id }
|
||||||
|
|
||||||
func (m *modLivePosting) Initialize(id string, crontab *cron.Cron, discord *discordgo.Session, attrs moduleAttributeStore) error {
|
func (m *modLivePosting) Initialize(id string, crontab *cron.Cron, discord *discordgo.Session, attrs moduleAttributeStore) error {
|
||||||
m.attrs = attrs
|
m.attrs = attrs
|
||||||
|
@ -70,9 +73,9 @@ func (m *modLivePosting) Initialize(id string, crontab *cron.Cron, discord *disc
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m modLivePosting) Setup() error { return nil }
|
func (m *modLivePosting) Setup() error { return nil }
|
||||||
|
|
||||||
func (m modLivePosting) cronFetchChannelStatus() {
|
func (m *modLivePosting) cronFetchChannelStatus() {
|
||||||
// @attr poll_usernames optional []string "[]" Check these usernames for active streams when executing the `cron` (at most 100 users can be checked)
|
// @attr poll_usernames optional []string "[]" Check these usernames for active streams when executing the `cron` (at most 100 users can be checked)
|
||||||
usernames, err := m.attrs.StringSlice("poll_usernames")
|
usernames, err := m.attrs.StringSlice("poll_usernames")
|
||||||
switch err {
|
switch err {
|
||||||
|
@ -93,7 +96,7 @@ func (m modLivePosting) cronFetchChannelStatus() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m modLivePosting) fetchAndPostForUsername(usernames ...string) error {
|
func (m *modLivePosting) fetchAndPostForUsername(usernames ...string) error {
|
||||||
twitch := newTwitchAdapter(
|
twitch := newTwitchAdapter(
|
||||||
// @attr twitch_client_id required string "" Twitch client ID the token was issued for
|
// @attr twitch_client_id required string "" Twitch client ID the token was issued for
|
||||||
m.attrs.MustString("twitch_client_id", nil),
|
m.attrs.MustString("twitch_client_id", nil),
|
||||||
|
@ -155,7 +158,7 @@ func (m modLivePosting) fetchAndPostForUsername(usernames ...string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m modLivePosting) handlePresenceUpdate(d *discordgo.Session, p *discordgo.PresenceUpdate) {
|
func (m *modLivePosting) handlePresenceUpdate(d *discordgo.Session, p *discordgo.PresenceUpdate) {
|
||||||
if p.User == nil {
|
if p.User == nil {
|
||||||
// The frick? Non-user presence?
|
// The frick? Non-user presence?
|
||||||
return
|
return
|
||||||
|
@ -216,7 +219,10 @@ func (m modLivePosting) handlePresenceUpdate(d *discordgo.Session, p *discordgo.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m modLivePosting) sendLivePost(username, displayName, title, game, previewImage, profileImage string) error {
|
func (m *modLivePosting) sendLivePost(username, displayName, title, game, previewImage, profileImage string) error {
|
||||||
|
m.lock.Lock()
|
||||||
|
defer m.lock.Unlock()
|
||||||
|
|
||||||
postText := strings.NewReplacer(
|
postText := strings.NewReplacer(
|
||||||
"${displayname}", displayName,
|
"${displayname}", displayName,
|
||||||
"${username}", username,
|
"${username}", username,
|
||||||
|
|
Loading…
Reference in a new issue