-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
157 lines (122 loc) · 5.05 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"fmt"
"log"
"os"
"github.com/switchupcb/disgo"
"github.com/switchupcb/disgo/tools"
)
// Environment Variables.
var (
// token represents the bot's token.
token = os.Getenv("TOKEN")
// appid represents the bot's ApplicationID.
//
// Use Developer Mode to find it, or call GetCurrentUser (request) in your program
// and set it programmatically.
appid = os.Getenv("APPID")
)
func main() {
// enable the logger for the API Wrapper.
// zerolog.SetGlobalLevel(zerolog.DebugLevel)
log.Println("Program is started.")
// create a new Bot Client.
bot := &disgo.Client{
ApplicationID: appid, // REQUIRED (for this example).
Authentication: disgo.BotToken(token), // or BearerToken("TOKEN")
Config: disgo.DefaultConfig(),
Handlers: new(disgo.Handlers),
Sessions: disgo.NewSessionManager(),
}
log.Println("Creating an application command...")
// Create a Create Global Application Command request.
request := &disgo.CreateGlobalApplicationCommand{
Name: "main",
Description: disgo.Pointer("A basic command."),
// The following field is not required, but useful to understand.
// https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-types
Type: disgo.Pointer(disgo.FlagApplicationCommandTypeCHAT_INPUT),
}
// Register the new command by sending the request to Discord using the bot.
//
// returns a disgo.ApplicationCommand
newCommand, err := request.Send(bot)
if err != nil {
log.Printf("failure sending command to Discord: %v", err)
return
}
log.Println("Adding an event handler.")
// Add an event handler to the bot.
//
// confirm the event handler is added to the bot.
if err := bot.Handle(disgo.FlagGatewayEventNameInteractionCreate, func(i *disgo.InteractionCreate) {
log.Printf("main called by %s.", i.Interaction.User.Username)
// see func declaration below.
if err := onInteraction(bot, i.Interaction); err != nil {
log.Println(err)
}
}); err != nil {
// when the Handle(eventname, function) parameters are not configured correctly.
log.Printf("Failed to add event handler to bot: %v", err)
os.Exit(1)
}
log.Println("Connecting to the Discord Gateway...")
// Connect a new session to the Discord Gateway (WebSocket Connection).
session := disgo.NewSession()
if err := session.Connect(bot); err != nil {
log.Printf("can't open websocket session to Discord Gateway: %v", err)
return
}
log.Println("Successfully connected to the Discord Gateway. Waiting for an interaction...")
// end the program using a SIGINT call via `Ctrl + C` from the terminal.
if err := tools.InterceptSignal(tools.Signals, session); err != nil {
log.Printf("error exiting program: %v", err)
}
log.Println("Exiting program due to signal...")
// tools.InterceptSignal() calls s.Disconnect() which disconnects the Session from the Discord Gateway.
log.Println("Disconnected from the Discord Gateway.")
// Deleting the Global Application Command is not required, but useful for the cleanup of this program.
log.Println("Deleting the application command...")
requestDeleteGlobalApplicationCommand := &disgo.DeleteGlobalApplicationCommand{CommandID: newCommand.ID}
if err := requestDeleteGlobalApplicationCommand.Send(bot); err != nil {
log.Printf("error deleting Global Application Command: %v", err)
return
}
log.Printf("Program executed successfully.")
}
// onInteraction deletes the Global Application Command, then disconnects the bot.
//
// In this example, onInteraction is called when a user sends a `/main` interaction to the bot.
func onInteraction(bot *disgo.Client, interaction *disgo.Interaction) error {
log.Println("Creating a response to the interaction...")
// send an interaction response to reply to the user.
requestCreateInteractionResponse := &disgo.CreateInteractionResponse{
InteractionID: interaction.ID,
// Interaction tokens are valid for 15 minutes,
// but an initial response to an interaction must be sent within 3 seconds (of receiving it),
// otherwise the token is invalidated.
InteractionToken: interaction.Token,
// https://discord.com/developers/docs/interactions/receiving-and-responding#responding-to-an-interaction
InteractionResponse: &disgo.InteractionResponse{
Type: disgo.FlagInteractionCallbackTypeCHANNEL_MESSAGE_WITH_SOURCE,
// Any of the following objects can be used.
//
// Messages
// https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-response-object-messages
//
// Autocomplete
// https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-response-object-autocomplete
//
// Modal
// https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-response-object-modal
Data: &disgo.Messages{
Content: disgo.Pointer("Hello!"),
},
},
}
if err := requestCreateInteractionResponse.Send(bot); err != nil {
return fmt.Errorf("error sending interaction response: %w", err)
}
log.Println("Sent a response to the interaction.")
return nil
}