From 74e4f921da3cad5d45bfb93856264077d3bff75b Mon Sep 17 00:00:00 2001 From: Joren Date: Tue, 18 Jun 2024 16:29:58 +0200 Subject: [PATCH] aa --- go.mod | 5 ++++- main.go | 47 ++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index e12cebf..fcc43de 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module git.directme.in/Joren/SolMonitor go 1.22.4 -require github.com/bwmarrin/discordgo v0.28.1 +require ( + github.com/BurntSushi/toml v1.4.0 + github.com/bwmarrin/discordgo v0.28.1 +) require ( github.com/gorilla/websocket v1.4.2 // indirect diff --git a/main.go b/main.go index 49e4195..70ca308 100644 --- a/main.go +++ b/main.go @@ -4,18 +4,36 @@ import ( "fmt" "os" - + "github.com/BurntSushi/toml" "github.com/bwmarrin/discordgo" ) var ( - Token string + Token string + ServerID string ) func init() { - Token = os.Getenv("DISCORD_BOT_TOKEN") + var config struct { + Token string `toml:"DISCORD_BOT_TOKEN"` + ServerID string `toml:"DISCORD_SERVER_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 + if Token == "" { - fmt.Println("No token provided. Please set DISCORD_BOT_TOKEN environment variable.") + 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) } } @@ -28,6 +46,7 @@ func main() { } dg.AddHandler(ready) + dg.AddHandler(messageCreate) dg.Identify.Intents = discordgo.IntentsGuilds | discordgo.IntentsGuildMessages @@ -42,6 +61,24 @@ func main() { } func ready(s *discordgo.Session, event *discordgo.Ready) { - s.UpdateGameStatus(0, "Monitoring addresses") + 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) }