2024-06-18 17:06:13 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
Token string
|
|
|
|
ServerID string
|
|
|
|
OutputChannelID string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var config struct {
|
|
|
|
Token string `toml:"DISCORD_BOT_TOKEN"`
|
|
|
|
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)
|
|
|
|
}
|
2024-06-18 16:29:58 +02:00
|
|
|
}
|
|
|
|
|
2024-06-18 17:06:13 +02:00
|
|
|
func main() {
|
|
|
|
dg, err := discordgo.New("Bot " + Token)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error creating Discord session:", err)
|
|
|
|
return
|
|
|
|
}
|
2024-06-18 16:29:58 +02:00
|
|
|
|
2024-06-18 17:06:13 +02:00
|
|
|
dg.AddHandler(ready)
|
|
|
|
dg.AddHandler(messageCreate)
|
2024-06-18 16:29:58 +02:00
|
|
|
|
2024-06-18 17:06:13 +02:00
|
|
|
dg.Identify.Intents = discordgo.IntentsGuilds | discordgo.IntentsGuildMessages | discordgo.IntentMessageContent
|
2024-06-18 16:29:58 +02:00
|
|
|
|
2024-06-18 17:06:13 +02:00
|
|
|
err = dg.Open()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error opening connection:", err)
|
|
|
|
return
|
|
|
|
}
|
2024-06-18 16:26:10 +02:00
|
|
|
|
2024-06-18 17:06:13 +02:00
|
|
|
fmt.Println("Bot is now running. Press CTRL+C to exit.")
|
|
|
|
select {}
|
2024-06-18 16:26:10 +02:00
|
|
|
}
|
|
|
|
|
2024-06-18 17:06:13 +02:00
|
|
|
func ready(s *discordgo.Session, event *discordgo.Ready) {
|
|
|
|
s.UpdateGameStatus(0, "Monitoring messages")
|
2024-06-18 16:26:10 +02:00
|
|
|
}
|
|
|
|
|
2024-06-18 17:06:13 +02:00
|
|
|
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|
|
|
if m.Author.ID == s.State.User.ID {
|
|
|
|
return
|
|
|
|
}
|
2024-06-18 16:26:10 +02:00
|
|
|
|
2024-06-18 17:06:13 +02:00
|
|
|
channel, err := s.State.Channel(m.ChannelID)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error getting channel:", err)
|
|
|
|
return
|
|
|
|
}
|
2024-06-18 16:29:58 +02:00
|
|
|
|
2024-06-18 17:06:13 +02:00
|
|
|
if channel.GuildID != ServerID {
|
|
|
|
return
|
|
|
|
}
|
2024-06-18 16:29:58 +02:00
|
|
|
|
2024-06-18 17:06:13 +02:00
|
|
|
fmt.Printf("Message from %s: %s\n", m.Author.Username, m.Content)
|
2024-06-18 16:29:58 +02:00
|
|
|
|
|
|
|
|
2024-06-18 17:06:13 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2024-06-18 16:26:10 +02:00
|
|
|
|