starboard: Initial implementation

master 0.5
Luther Wen Xu 2019-11-20 16:14:36 +07:00
parent 05e08ce216
commit 46cee1af47
Signed by: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
2 changed files with 70 additions and 0 deletions

@ -21,9 +21,12 @@ func main() {
} }
dg.AddHandler(messageCreate) dg.AddHandler(messageCreate)
dg.AddHandler(checkPrestigeReact) dg.AddHandler(checkPrestigeReact)
dg.AddHandler(starboardAutoReact)
dg.AddHandler(listenToStarboardReact)
err = dg.Open() err = dg.Open()
if err != nil { if err != nil {
fmt.Println("Error opening Discord session:", err) fmt.Println("Error opening Discord session:", err)
return
} }
go listenToLevelUpdate(dg) go listenToLevelUpdate(dg)

@ -0,0 +1,67 @@
package main
import (
"time"
"github.com/bwmarrin/discordgo"
)
const starEmoji = "⭐"
func starboardAutoReact(s *discordgo.Session, m *discordgo.MessageCreate) {
err := s.MessageReactionAdd(m.ChannelID, m.ID, starEmoji)
if err != nil {
logError(s, "starboardAutoReact", m.ChannelID, m.ID, err)
}
}
func listenToStarboardReact(s *discordgo.Session, m *discordgo.MessageReactionAdd) {
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
}
if len(reacter) < 5 {
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
}
_, err = s.ChannelMessageSendEmbed(
"645551408283648000",
&discordgo.MessageEmbed{
Title: "Starred Message",
URL: "https://discordapp.com/channels/" + m.GuildID + "/" + m.ChannelID + "/" + m.MessageID,
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)
}
}