-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (66 loc) · 1.8 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"log"
"os"
"os/signal"
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/caarlos0/env/v6"
)
type envVars struct {
BotToken string `env:"BOT_TOKEN"`
GuildID string `env:"GUILD_ID"`
}
func main() {
envs := envVars{}
if err := env.Parse(&envs); err != nil {
fmt.Printf("%+v\n", err)
}
fmt.Printf("%+v\n", envs)
dg, err := discordgo.New("Bot " + envs.BotToken)
if err != nil {
panic(err.Error())
}
if err != nil { panic(err.Error()) }
// dg.Identify.Intents = discordgo.
dg.AddHandler(SlashCommandHandler);
/* grab the bot user's id */
botUser, err := dg.User("@me")
if err != nil { log.Panic(err.Error()) }
_ = botUser.ID
err = dg.Open()
if err != nil { log.Panic(err.Error()) }
for _, c := range SlashCommands {
_, err := dg.ApplicationCommandCreate(dg.State.User.ID, envs.GuildID, c)
if err != nil {
log.Printf("Failed to create slash command: %v", c.Name)
}
}
defer dg.Close()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
log.Println("Press Ctrl+C to exit")
<-stop
}
var SlashCommands = []*discordgo.ApplicationCommand{
{
Name: "hello",
Type: discordgo.ChatApplicationCommand,
Description: "hello world",
},
}
func SlashCommandHandler(s *discordgo.Session, m *discordgo.InteractionCreate) {
if handler, ok := SlashCommandHandlers[m.ApplicationCommandData().Name]; ok {
handler(s, m)
}
}
var SlashCommandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"hello": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "hello world slash command",
},
})
},
}