112 lines
2.7 KiB
Go
112 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
)
|
|
|
|
const starEmoji = "⭐"
|
|
const starChannel = "645551408283648000"
|
|
|
|
var starboardMutex sync.Mutex
|
|
|
|
func listenToStarboardReact(s *discordgo.Session, m *discordgo.MessageReactionAdd) {
|
|
if m.ChannelID == starChannel {
|
|
return
|
|
}
|
|
if m.Emoji.Name != starEmoji {
|
|
return
|
|
}
|
|
messageTimestamp, err := discordgo.SnowflakeTimestamp(m.MessageID)
|
|
if err != nil {
|
|
logError(s, "listenToStarboardReact", "snowflakeTimestamp", m.MessageID, err)
|
|
return
|
|
}
|
|
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 {
|
|
logError(s, "listenToStarboardReact", "messageReactions", m.ChannelID, m.MessageID, err)
|
|
return
|
|
}
|
|
if len(reacter) < 3 {
|
|
return
|
|
}
|
|
|
|
msg, err := s.ChannelMessage(m.ChannelID, m.MessageID)
|
|
if err != nil {
|
|
logError(s, "listenToStarboardReact", "channelMessage", m.ChannelID, m.MessageID, err)
|
|
return
|
|
}
|
|
|
|
color := s.State.UserColor(msg.Author.ID, m.ChannelID)
|
|
if color == 0 {
|
|
//White as fallback
|
|
color = 0xFFFFFF
|
|
}
|
|
|
|
content := msg.Content[:1000]
|
|
//Up to 10 line
|
|
splittedContent := strings.SplitN(content, "\n", 11)
|
|
content = strings.Join(splittedContent[:10], "\n")
|
|
if len(splittedContent) > 10 || len(content) > 1000 {
|
|
content += "..."
|
|
}
|
|
content += fmt.Sprintf(
|
|
"\n\n[Original Message](https://discordapp.com/channels/%s/%s/%s)",
|
|
m.GuildID, m.ChannelID, m.MessageID,
|
|
)
|
|
|
|
msg, err = s.ChannelMessageSendEmbed(
|
|
starChannel,
|
|
&discordgo.MessageEmbed{
|
|
Title: "Starred Message",
|
|
Description: content,
|
|
Color: color,
|
|
Timestamp: messageTimestamp.Format(time.RFC3339),
|
|
Author: &discordgo.MessageEmbedAuthor{
|
|
Name: msg.Author.Username + "#" + msg.Author.Discriminator,
|
|
IconURL: "https://cdn.discordapp.com/avatars/" + msg.Author.ID + "/" + msg.Author.Avatar + ".png",
|
|
},
|
|
},
|
|
)
|
|
if err != nil {
|
|
logError(s, "listenToStarboardReact", "channelMessageSendEmbed", err)
|
|
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)
|
|
}
|
|
}
|