80 lines
1.8 KiB
Go
80 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/bwmarrin/discordgo"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
)
|
|
|
|
import _ "github.com/joho/godotenv/autoload"
|
|
|
|
func main() {
|
|
token := os.Getenv("DISCORD_TOKEN")
|
|
if token == "" {
|
|
fmt.Println("Missing discord token (DISCORD_TOKEN), so bye bye!")
|
|
return
|
|
}
|
|
discord, err := discordgo.New("Bot " + token)
|
|
if err != nil {
|
|
fmt.Println("Error creating Discord session: ", err)
|
|
return
|
|
}
|
|
discord.AddHandler(ready)
|
|
discord.AddHandler(reactAdd)
|
|
discord.AddHandler(reactRemove)
|
|
|
|
err = discord.Open()
|
|
if err != nil {
|
|
fmt.Println("Error opening Discord session: ", err)
|
|
}
|
|
|
|
// Wait here until CTRL-C or other term signal is received.
|
|
fmt.Println("PinBot is running! Press CTRL-C to exit.")
|
|
sc := make(chan os.Signal, 1)
|
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
|
|
<-sc
|
|
|
|
// Cleanly close down the Discord session.
|
|
_ = discord.Close()
|
|
|
|
}
|
|
|
|
func ready(s *discordgo.Session, event *discordgo.Ready) {
|
|
_ = s.UpdateStatus(0, "5x📌 pins a message!")
|
|
}
|
|
|
|
func reactAdd(s *discordgo.Session, event *discordgo.MessageReactionAdd) {
|
|
messageReac(s, event.MessageReaction)
|
|
}
|
|
|
|
func reactRemove(s *discordgo.Session, event *discordgo.MessageReactionRemove) {
|
|
messageReac(s, event.MessageReaction)
|
|
}
|
|
|
|
func messageReac(s *discordgo.Session, event *discordgo.MessageReaction) {
|
|
if event.Emoji.Name == "📌" {
|
|
|
|
msg, err := s.ChannelMessage(event.ChannelID, event.MessageID)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
for _, reaction := range msg.Reactions {
|
|
if reaction.Emoji.Name == "📌" {
|
|
var msgErr error
|
|
if reaction.Count >= 5 {
|
|
msgErr = s.ChannelMessagePin(event.ChannelID, event.MessageID)
|
|
} else if reaction.Count <= 3 {
|
|
msgErr = s.ChannelMessageUnpin(event.ChannelID, event.MessageID)
|
|
}
|
|
if msgErr != nil {
|
|
fmt.Println("Error: ", msgErr)
|
|
}
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|