This commit is contained in:
Joren 2024-06-18 16:29:58 +02:00
parent eceb0ecdef
commit 74e4f921da
Signed by: Joren
GPG Key ID: 280E33DFBC0F1B55
2 changed files with 46 additions and 6 deletions

5
go.mod
View File

@ -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

47
main.go
View File

@ -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)
}