2019-11-27 09:07:48 +07:00
|
|
|
package starboard
|
2019-11-20 08:14:36 +07:00
|
|
|
|
|
|
|
import (
|
2019-11-20 09:18:58 +07:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2019-11-20 09:25:18 +07:00
|
|
|
"sync"
|
2019-11-20 08:14:36 +07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
2019-11-20 12:59:48 +07:00
|
|
|
|
|
|
|
"PermissionGacha/db"
|
2019-11-27 12:06:42 +07:00
|
|
|
"PermissionGacha/modules/config"
|
2019-11-27 09:07:48 +07:00
|
|
|
"PermissionGacha/modules/log"
|
2019-11-20 08:14:36 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
const starEmoji = "⭐"
|
|
|
|
|
2019-11-20 09:25:18 +07:00
|
|
|
var starboardMutex sync.Mutex
|
|
|
|
|
2019-11-27 09:07:48 +07:00
|
|
|
func ReactListener(s *discordgo.Session, m *discordgo.MessageReactionAdd) {
|
2019-11-27 12:06:42 +07:00
|
|
|
if m.ChannelID == config.StarChannel {
|
2019-11-20 08:57:23 +07:00
|
|
|
return
|
|
|
|
}
|
2019-11-20 08:14:36 +07:00
|
|
|
if m.Emoji.Name != starEmoji {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
messageTimestamp, err := discordgo.SnowflakeTimestamp(m.MessageID)
|
|
|
|
if err != nil {
|
2019-11-27 09:07:48 +07:00
|
|
|
log.Error(s, "listenToStarboardReact", "snowflakeTimestamp", m.MessageID, err)
|
2019-11-20 08:14:36 +07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if time.Now().Sub(messageTimestamp) > 24*time.Hour {
|
|
|
|
return
|
|
|
|
}
|
2019-11-20 09:25:18 +07:00
|
|
|
starboardMutex.Lock()
|
|
|
|
defer starboardMutex.Unlock()
|
|
|
|
|
2019-11-20 12:59:48 +07:00
|
|
|
starredBefore, err := db.HasStarredBefore(m.MessageID)
|
|
|
|
if err != nil {
|
2019-11-27 09:07:48 +07:00
|
|
|
log.Error(s, err)
|
2019-11-20 12:59:48 +07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if starredBefore {
|
2019-11-20 09:25:18 +07:00
|
|
|
return
|
|
|
|
}
|
2019-11-20 08:14:36 +07:00
|
|
|
|
|
|
|
reacter, err := s.MessageReactions(m.ChannelID, m.MessageID, starEmoji, 5)
|
|
|
|
if err != nil {
|
2019-11-27 09:07:48 +07:00
|
|
|
log.Error(s, "listenToStarboardReact", "messageReactions", m.ChannelID, m.MessageID, err)
|
2019-11-20 08:14:36 +07:00
|
|
|
return
|
|
|
|
}
|
2019-11-20 08:25:13 +07:00
|
|
|
if len(reacter) < 3 {
|
2019-11-20 08:14:36 +07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msg, err := s.ChannelMessage(m.ChannelID, m.MessageID)
|
|
|
|
if err != nil {
|
2019-11-27 09:07:48 +07:00
|
|
|
log.Error(s, "listenToStarboardReact", "channelMessage", m.ChannelID, m.MessageID, err)
|
2019-11-20 08:14:36 +07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
color := s.State.UserColor(msg.Author.ID, m.ChannelID)
|
|
|
|
if color == 0 {
|
|
|
|
//White as fallback
|
|
|
|
color = 0xFFFFFF
|
|
|
|
}
|
2019-11-20 09:18:58 +07:00
|
|
|
|
2019-11-20 09:34:56 +07:00
|
|
|
content := msg.Content
|
|
|
|
if len(content) > 1000 {
|
|
|
|
content = content[:1000]
|
|
|
|
}
|
2019-11-20 09:18:58 +07:00
|
|
|
//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,
|
|
|
|
)
|
|
|
|
|
2019-12-12 04:53:18 +07:00
|
|
|
embed := 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",
|
2019-11-20 08:14:36 +07:00
|
|
|
},
|
2019-12-12 04:53:18 +07:00
|
|
|
}
|
2019-12-12 05:32:38 +07:00
|
|
|
if len(msg.Attachments) > 0 && msg.Attachments[0] != nil && msg.Attachments[0].Width > 0 {
|
|
|
|
img := msg.Attachments[0]
|
2019-12-12 04:53:18 +07:00
|
|
|
embed.Thumbnail = &discordgo.MessageEmbedThumbnail{
|
|
|
|
URL: img.URL,
|
|
|
|
ProxyURL: img.ProxyURL,
|
|
|
|
Width: img.Width,
|
|
|
|
Height: img.Height,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
msg, err = s.ChannelMessageSendEmbed(config.StarChannel, &embed)
|
2019-11-20 08:14:36 +07:00
|
|
|
if err != nil {
|
2019-11-27 09:07:48 +07:00
|
|
|
log.Error(s, "listenToStarboardReact", "channelMessageSendEmbed", err)
|
2019-11-20 08:57:23 +07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = s.MessageReactionAdd(msg.ChannelID, msg.ID, starEmoji)
|
|
|
|
if err != nil {
|
2019-11-27 09:07:48 +07:00
|
|
|
log.Error(s, "listenToStarboardReact", "messageReactionAdd", err)
|
2019-11-20 12:59:48 +07:00
|
|
|
return
|
2019-11-20 08:14:36 +07:00
|
|
|
}
|
2019-12-12 04:37:38 +07:00
|
|
|
err = db.SetStarred(m.MessageID)
|
2019-11-20 09:25:18 +07:00
|
|
|
if err != nil {
|
2019-11-27 09:07:48 +07:00
|
|
|
log.Error(s, err)
|
2019-11-20 12:59:48 +07:00
|
|
|
return
|
2019-11-20 09:25:18 +07:00
|
|
|
}
|
|
|
|
}
|
2019-11-27 09:07:48 +07:00
|
|
|
|
|
|
|
func AddReact(s *discordgo.Session, channelID, messageID string) error {
|
|
|
|
return s.MessageReactionAdd(channelID, messageID, starEmoji)
|
|
|
|
}
|