MoyaiReactBot/main.go
2020-02-17 12:43:21 +01:00

73 lines
1.3 KiB
Go

package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
)
var (
token string
reactUserID string
)
func init() {
err := godotenv.Load()
if err != nil {
fmt.Println("Could not load .env file: ", err)
os.Exit(1)
}
token = os.Getenv("DISCORD_BOT_TOKEN")
reactUserID = os.Getenv("REACT_USER_ID")
if token == "" || reactUserID == "" {
fmt.Println("DISCORD_BOT_TOKEN and REACT_USER_ID variables have to be set.")
os.Exit(1)
}
}
func main() {
bot, err := discordgo.New("Bot " + token)
if err != nil {
fmt.Println("Could not create Discord bot instance:", err)
return
}
bot.AddHandler(messageCreate)
err = bot.Open()
if err != nil {
fmt.Println("Error opening connection to Discord:", err)
return
}
fmt.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
bot.Close()
}
func messageCreate(sess *discordgo.Session, msg *discordgo.MessageCreate) {
if msg.Author.ID != reactUserID {
return
}
channelID := msg.ChannelID
msgID := msg.ID
err := sess.MessageReactionAdd(channelID, msgID, "🗿")
if err != nil {
fmt.Println("Error occurred reacting to message ID: "+msgID, err)
}
}