2019-11-20 08:14:36 +07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-11-20 09:18:58 +07:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2019-11-20 08:14:36 +07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
)
|
|
|
|
|
|
|
|
const starEmoji = "⭐"
|
2019-11-20 08:57:23 +07:00
|
|
|
const starChannel = "645551408283648000"
|
2019-11-20 08:14:36 +07:00
|
|
|
|
|
|
|
func listenToStarboardReact(s *discordgo.Session, m *discordgo.MessageReactionAdd) {
|
2019-11-20 08:57:23 +07:00
|
|
|
if m.ChannelID == starChannel {
|
|
|
|
return
|
|
|
|
}
|
2019-11-20 08:14:36 +07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
reacter, err := s.MessageReactions(m.ChannelID, m.MessageID, starEmoji, 5)
|
|
|
|
if err != nil {
|
|
|
|
logError(s, "listenToStarboardReact", "messageReactions", m.ChannelID, m.MessageID, err)
|
|
|
|
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 {
|
|
|
|
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
|
|
|
|
}
|
2019-11-20 09:18:58 +07:00
|
|
|
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
2019-11-20 08:57:23 +07:00
|
|
|
msg, err = s.ChannelMessageSendEmbed(
|
|
|
|
starChannel,
|
2019-11-20 08:14:36 +07:00
|
|
|
&discordgo.MessageEmbed{
|
2019-11-20 08:24:14 +07:00
|
|
|
Title: "Starred Message",
|
2019-11-20 09:18:58 +07:00
|
|
|
Description: content,
|
2019-11-20 08:24:14 +07:00
|
|
|
Color: color,
|
|
|
|
Timestamp: messageTimestamp.Format(time.RFC3339),
|
2019-11-20 08:14:36 +07:00
|
|
|
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)
|
2019-11-20 08:57:23 +07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = s.MessageReactionAdd(msg.ChannelID, msg.ID, starEmoji)
|
|
|
|
if err != nil {
|
|
|
|
logError(s, "listenToStarboardReact", "messageReactionAdd", err)
|
2019-11-20 08:14:36 +07:00
|
|
|
}
|
|
|
|
}
|