-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
210 lines (168 loc) · 6.81 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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: "hello",
// Be sure to adhere to Application Command Naming rules.
// https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-naming
NameLocalizations: &map[string]string{
// Top 10. Locales by Population
//
// Discord doesn't support every locale.
// https://discord.com/developers/docs/reference#locales
disgo.FlagLocalesEnglishUS: "hello",
disgo.FlagLocalesEnglishUK: "mate",
disgo.FlagLocalesChineseChina: "你好",
disgo.FlagLocalesHindi: "नमस्ते",
disgo.FlagLocalesSpanish: "hola",
disgo.FlagLocalesFrench: "bonjour",
// 6. Arabic
// 7. Bengali
disgo.FlagLocalesRussian: "привет",
disgo.FlagLocalesPortugueseBrazilian: "olá",
// 10. Indonesian
// 11. Pronouns
},
Description: disgo.Pointer("Say hello."),
DescriptionLocalizations: &map[string]string{
disgo.FlagLocalesEnglishUS: "Say hello.",
disgo.FlagLocalesEnglishUK: "Say hello.",
disgo.FlagLocalesChineseChina: "问好。",
disgo.FlagLocalesHindi: "नमस्ते बोलो।",
disgo.FlagLocalesSpanish: "Di hola.",
disgo.FlagLocalesFrench: "Dis bonjour.",
disgo.FlagLocalesRussian: "Скажи привет.",
disgo.FlagLocalesPortugueseBrazilian: "Diga olá.",
},
// Localization is also supported in Application Command Options.
// https://discord.com/developers/docs/interactions/application-commands#localization
Options: nil,
}
// 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
}
// save the map defined in the returned command from Discord for later usage.
if newCommand.NameLocalizations == nil {
log.Println("error: returned command from Discord does not contain localizations.")
return
}
locales := newCommand.NameLocalizations
log.Println("Adding an event handler.")
// Add an event handler to the bot.
//
// ensure that the event handler is added to the bot.
if err := bot.Handle(disgo.FlagGatewayEventNameInteractionCreate, func(i *disgo.InteractionCreate) {
log.Printf("hello called by %s.", i.Interaction.User.Username)
// see func declaration below.
if err := onInteraction(bot, i.Interaction, *locales); 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.")
// The following code is not necessarily required, but useful for the cleanup of this program.
//
// delete the Global Application Command.
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 responds to the user based on their locale.
//
// In this example, onInteraction is called when a user sends a `/hello` interaction to the bot.
func onInteraction(bot *disgo.Client, interaction *disgo.Interaction, locales map[string]string) error {
log.Println("Creating a response to the interaction...")
// determine the response.
var locale string
if interaction.Locale != nil {
locale = locales[*interaction.Locale]
}
if locale == "" {
locale = "The current locale is not supported by this command."
}
// 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(locale), // or &locale
},
},
}
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
}