more commands will be added as we continue working on this bot, including service restarts, log monitors
85 lines
2.1 KiB
Go
85 lines
2.1 KiB
Go
package bot
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
)
|
|
|
|
func TheBot() {
|
|
|
|
Token := os.Getenv("BOT_KEY")
|
|
if Token == "" {
|
|
fmt.Println("You must set DISCORD_TOKEN environment variable.")
|
|
return
|
|
}
|
|
|
|
// Create bot session
|
|
dg, err := discordgo.New("Bot " + Token)
|
|
if err != nil {
|
|
fmt.Println("Error creating Discord session,", err)
|
|
return
|
|
}
|
|
|
|
dg.Identify.Intents = discordgo.IntentsGuildMessages |
|
|
discordgo.IntentsDirectMessages |
|
|
discordgo.IntentsMessageContent | discordgo.IntentsGuilds
|
|
|
|
// Register messageCreate as a callback for MessageCreate events
|
|
dg.AddHandler(interactionCreate)
|
|
|
|
// // Open the websocket and begin listening
|
|
err = dg.Open()
|
|
if err != nil {
|
|
fmt.Println("Error opening Discord session,", err)
|
|
return
|
|
}
|
|
|
|
// Wait here until CTRL-C or other term signal is received.
|
|
fmt.Println("Bot is now running. Press CTRL-C to exit.")
|
|
// Register commands, passing the session
|
|
registerCommands(dg)
|
|
|
|
// sc := make(chan os.Signal, 1)
|
|
// signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
|
// <-sc
|
|
|
|
// // Cleanly close down the Discord session.
|
|
// dg.Close()
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
|
|
defer stop()
|
|
|
|
<-ctx.Done()
|
|
|
|
cleanupCommands(dg)
|
|
dg.Close()
|
|
}
|
|
|
|
// This function will be called (due to event handler registration above)
|
|
// every time a new message is created on any channel that the authed bot has access to.
|
|
// func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|
// // ignore bots (including self)
|
|
// if m.Author == nil || m.Author.Bot {
|
|
// return
|
|
// }
|
|
|
|
// // debug: log incoming messages so you can see them in console
|
|
// fmt.Printf("msg from %s: %s\n", m.Author.Username, m.Content)
|
|
|
|
// // choose the prefix you want — here I check for "!ping"
|
|
// if m.Content == "!ping" {
|
|
// if _, err := s.ChannelMessageSend(m.ChannelID, "Pong!"); err != nil {
|
|
// fmt.Println("send error:", err)
|
|
// }
|
|
// }
|
|
// if m.Content == "!pong" {
|
|
// if _, err := s.ChannelMessageSend(m.ChannelID, "Ping!"); err != nil {
|
|
// fmt.Println("send error:", err)
|
|
// }
|
|
// }
|
|
// }
|