Add simple /ping command
This commit is contained in:
1
.env.example
Normal file
1
.env.example
Normal file
@ -0,0 +1 @@
|
|||||||
|
TOKEN=
|
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"cSpell.words": ["bwmarrin", "discordgo", "godotenv", "joho"]
|
||||||
|
}
|
@ -1,2 +1,9 @@
|
|||||||
# ANewDawn
|
# ANewDawn
|
||||||
|
|
||||||
Shit Discord bot
|
Shit Discord bot
|
||||||
|
|
||||||
|
## Environment variables
|
||||||
|
|
||||||
|
| Env var | Desc | Example |
|
||||||
|
| ------- | ----------------- | ----------------------------------------------------------- |
|
||||||
|
| TOKEN | Discord bot token | MzQ2MDAwODAxNDk5ODk0Nzk1.XkK-7A.w18w6Z99c5DXi8ubbSNbj32lMZo |
|
||||||
|
11
go.mod
Normal file
11
go.mod
Normal file
@ -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
|
||||||
|
)
|
14
go.sum
Normal file
14
go.sum
Normal file
@ -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=
|
108
main.go
Normal file
108
main.go
Normal file
@ -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.")
|
||||||
|
}
|
Reference in New Issue
Block a user