85 lines
1.5 KiB
Go
85 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/BurntSushi/toml"
|
|
"github.com/bwmarrin/discordgo"
|
|
)
|
|
|
|
var (
|
|
Token string
|
|
ServerID string
|
|
)
|
|
|
|
func init() {
|
|
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 in config.toml.")
|
|
os.Exit(1)
|
|
}
|
|
|
|
if ServerID == "" {
|
|
fmt.Println("No server ID provided in config.toml.")
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
dg, err := discordgo.New("Bot " + Token)
|
|
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 {}
|
|
}
|
|
|
|
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)
|
|
}
|
|
|