Add monitor

This commit is contained in:
Joren 2024-06-18 17:06:13 +02:00
parent 74ddfb5b52
commit 1b9cbd88c4
Signed by: Joren
GPG Key ID: 280E33DFBC0F1B55

160
main.go
View File

@ -1,84 +1,102 @@
package main package main
import ( import (
"fmt" "fmt"
"os" "os"
"regexp"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
var ( var (
Token string Token string
ServerID string ServerID string
) OutputChannelID string
)
func init() { func init() {
var config struct { var config struct {
Token string `toml:"DISCORD_BOT_TOKEN"` Token string `toml:"DISCORD_BOT_TOKEN"`
ServerID string `toml:"DISCORD_SERVER_ID"` ServerID string `toml:"DISCORD_SERVER_ID"`
OutputChannelID string `toml:"OUTPUT_CHANNEL_ID"`
}
if _, err := toml.DecodeFile("config.toml", &config); err != nil {
fmt.Println("Error loading config:", err)
os.Exit(1)
}
Token = config.Token
ServerID = config.ServerID
OutputChannelID = config.OutputChannelID
if Token == "" {
fmt.Println("No token provided in config.toml.")
os.Exit(1)
}
if ServerID == "" {
fmt.Println("No server ID provided in config.toml.")
os.Exit(1)
}
if OutputChannelID == "" {
fmt.Println("No output channel ID in config.toml")
os.Exit(1)
}
} }
if _, err := toml.DecodeFile("config.toml", &config); err != nil { func main() {
fmt.Println("Error loading config:", err) dg, err := discordgo.New("Bot " + Token)
os.Exit(1) if err != nil {
fmt.Println("Error creating Discord session:", err)
return
}
dg.AddHandler(ready)
dg.AddHandler(messageCreate)
dg.Identify.Intents = discordgo.IntentsGuilds | discordgo.IntentsGuildMessages | discordgo.IntentMessageContent
err = dg.Open()
if err != nil {
fmt.Println("Error opening connection:", err)
return
}
fmt.Println("Bot is now running. Press CTRL+C to exit.")
select {}
} }
Token = config.Token func ready(s *discordgo.Session, event *discordgo.Ready) {
ServerID = config.ServerID s.UpdateGameStatus(0, "Monitoring messages")
if Token == "" {
fmt.Println("No token provided in config.toml.")
os.Exit(1)
} }
if ServerID == "" { func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
fmt.Println("No server ID provided in config.toml.") if m.Author.ID == s.State.User.ID {
os.Exit(1) return
} }
}
func main() { channel, err := s.State.Channel(m.ChannelID)
dg, err := discordgo.New("Bot " + Token) if err != nil {
if err != nil { fmt.Println("Error getting channel:", err)
fmt.Println("Error creating Discord session:", err) return
return }
if channel.GuildID != ServerID {
return
}
fmt.Printf("Message from %s: %s\n", m.Author.Username, m.Content)
re := regexp.MustCompile(`\b\w{36,44}\b`)
match := re.FindStringSubmatch(m.Content)
if len(match) > 0 {
matchedWord := match[0]
message := fmt.Sprintf("%s", matchedWord)
s.ChannelMessageSend(OutputChannelID, message)
}
} }
dg.AddHandler(ready)
dg.AddHandler(messageCreate)
dg.Identify.Intents = discordgo.IntentsGuilds | discordgo.IntentsGuildMessages | discordgo.IntentMessageContent
err = dg.Open()
if err != nil {
fmt.Println("Error opening connection:", err)
return
}
fmt.Println("Bot is now running. Press CTRL+C to exit.")
select {}
}
func ready(s *discordgo.Session, event *discordgo.Ready) {
s.UpdateGameStatus(0, "Monitoring messages")
}
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
channel, err := s.State.Channel(m.ChannelID)
if err != nil {
fmt.Println("Error getting channel:", err)
return
}
if channel.GuildID != ServerID {
return
}
fmt.Printf("Message from %s: %s\n", m.Author.Username, m.Content)
}