starboard: Record all starred messages

master
Luther Wen Xu 2019-11-20 17:25:18 +07:00
parent 4d46fb1960
commit 4bc658653b
Signed by: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
1 changed files with 27 additions and 0 deletions

@ -3,6 +3,7 @@ package main
import (
"fmt"
"strings"
"sync"
"time"
"github.com/bwmarrin/discordgo"
@ -11,6 +12,8 @@ import (
const starEmoji = "⭐"
const starChannel = "645551408283648000"
var starboardMutex sync.Mutex
func listenToStarboardReact(s *discordgo.Session, m *discordgo.MessageReactionAdd) {
if m.ChannelID == starChannel {
return
@ -26,6 +29,12 @@ func listenToStarboardReact(s *discordgo.Session, m *discordgo.MessageReactionAd
if time.Now().Sub(messageTimestamp) > 24*time.Hour {
return
}
starboardMutex.Lock()
defer starboardMutex.Unlock()
if checkStarredBefore(s, m.MessageID) {
return
}
reacter, err := s.MessageReactions(m.ChannelID, m.MessageID, starEmoji, 5)
if err != nil {
@ -78,7 +87,25 @@ func listenToStarboardReact(s *discordgo.Session, m *discordgo.MessageReactionAd
return
}
err = s.MessageReactionAdd(msg.ChannelID, msg.ID, starEmoji)
noteStarred(s, msg.ID)
if err != nil {
logError(s, "listenToStarboardReact", "messageReactionAdd", err)
}
}
func checkStarredBefore(s *discordgo.Session, discordID string) bool {
rows, err := db.Query("SELECT id FROM starred WHERE id=?", discordID)
if err != nil {
logError(s, "checkStarredBefore", "query", err)
return true
}
defer rows.Close()
return rows.Next()
}
func noteStarred(s *discordgo.Session, discordID string) {
_, err := db.Exec("INSERT INTO starred(id) VALUES(?)", discordID)
if err != nil {
logError(s, "noteStarred", "exec", err)
}
}