From bf3b267ecd5a9d6d93b33cf1b6a4b0e016a58e3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Hells=C3=A9n?= Date: Sun, 21 Jan 2024 14:16:45 +0100 Subject: [PATCH] Add simple /ping command --- .env.example | 1 + .vscode/settings.json | 3 ++ README.md | 7 +++ go.mod | 11 +++++ go.sum | 14 ++++++ main.go | 108 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 144 insertions(+) create mode 100644 .env.example create mode 100644 .vscode/settings.json create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..f656e0f --- /dev/null +++ b/.env.example @@ -0,0 +1 @@ +TOKEN= diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a133670 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "cSpell.words": ["bwmarrin", "discordgo", "godotenv", "joho"] +} diff --git a/README.md b/README.md index fa40cf2..08dec58 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,9 @@ # ANewDawn + Shit Discord bot + +## Environment variables + +| Env var | Desc | Example | +| ------- | ----------------- | ----------------------------------------------------------- | +| TOKEN | Discord bot token | MzQ2MDAwODAxNDk5ODk0Nzk1.XkK-7A.w18w6Z99c5DXi8ubbSNbj32lMZo | diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e95f217 --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module github.com/TheLovinator1/ANewDawn + +go 1.21.6 + +require ( + github.com/bwmarrin/discordgo v0.27.1 // indirect + github.com/gorilla/websocket v1.4.2 // indirect + github.com/joho/godotenv v1.5.1 // indirect + golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect + golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7d1da0c --- /dev/null +++ b/go.sum @@ -0,0 +1,14 @@ +github.com/bwmarrin/discordgo v0.27.1 h1:ib9AIc/dom1E/fSIulrBwnez0CToJE113ZGt4HoliGY= +github.com/bwmarrin/discordgo v0.27.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= +github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg= +golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..6b4e2e7 --- /dev/null +++ b/main.go @@ -0,0 +1,108 @@ +package main + +import ( + "log" + "os" + "os/signal" + + "github.com/bwmarrin/discordgo" + "github.com/joho/godotenv" +) + +// A Session represents a connection to the Discord API. +var s *discordgo.Session + +/* +/ping - The bot responds with "Pong!". +*/ +var ( + commands = []*discordgo.ApplicationCommand{ + { + Name: "ping", + Description: "Pong!", + }, + } + + commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){ + // Ping command + "ping": func(s *discordgo.Session, i *discordgo.InteractionCreate) { + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Content: "Pong!", + }, + }) + }, + } +) + +/* +init() is automatically called when the package is initialized. +It adds a handler function to the discordgo.Session that is triggered when a slash command is received. +The handler function checks if the interaction's application command name exists in the commandHandlers map. +If it does, it calls the corresponding function from the map with the session and interaction as arguments. +*/ +func init() { + // Load .env file. + err := godotenv.Load() + if err != nil { + log.Fatalf("Cannot load .env file: %v", err) + } + + // Get the bot token from the environment. + token := os.Getenv("TOKEN") + if token == "" { + log.Fatalln("No token provided. Please set the TOKEN environment variable.") + } + + // Create a new Discord session using the provided bot token. + s, err = discordgo.New("Bot " + token) + if err != nil { + log.Fatalf("Cannot create a new Discord session: %v", err) + } + + // Add a handler function to the discordgo.Session that is triggered when a slash command is received. + s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) { + if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok { + log.Printf("Handling '%v' command.", i.ApplicationCommandData().Name) + h(s, i) + } + }) +} + +func main() { + // Print the user we are logging in as. + s.AddHandler(func(s *discordgo.Session, _ *discordgo.Ready) { + log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator) + }) + + // Open a websocket connection to Discord and begin listening. + err := s.Open() + if err != nil { + log.Fatalf("Cannot open the session: %v", err) + } + + // Register the commands. + log.Println("Adding commands...") + registeredCommands := make([]*discordgo.ApplicationCommand, len(commands)) + for i, v := range commands { + cmd, err := s.ApplicationCommandCreate(s.State.User.ID, "341001473661992962", v) + if err != nil { + log.Panicf("Cannot create '%v' command: %v", v.Name, err) + } + registeredCommands[i] = cmd + log.Printf("Registered '%v' command.", cmd.Name) + } + + // Run s.Close() when the program exits. + defer s.Close() + + // Wait here until CTRL-C or other term signal is received. + stop := make(chan os.Signal, 1) + signal.Notify(stop, os.Interrupt) + log.Println("Press Ctrl+C to exit") + <-stop + + // Bye bye! + log.Println("Gracefully shutting down.") +}