-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
180 lines (153 loc) · 4.71 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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"crabot/crabtalk"
"crabot/formatting/markdown"
"internal/env"
"github.com/bwmarrin/discordgo"
)
// Bot parameters
var (
GuildID = flag.String("guild", "", "Test guild ID. If not passed - bot registers commands globally")
BotToken = flag.String("token", "", "Bot access token")
RemoveCommands = flag.Bool("rmcmd", true, "Remove all commands after shutdowning or not")
)
// Session
var session *discordgo.Session
// Parse flags
func init() { flag.Parse() }
// Start session
func init() {
var err error
// Check if token is entered.
if *BotToken == "" {
// If token isn't entered, get token from env file.
*BotToken, err = env.GetEnvValue("TOKEN", "")
if err != nil {
log.Fatalln("Error occurred while getting the token", err)
}
}
session, err = discordgo.New("Bot " + *BotToken)
if err != nil {
log.Fatalln("Invaild bot parameters:", err)
}
}
var (
// Not needed for only basic command
/*
integerOptionMinValue = 1.0
dmPermission = false
defaultMemberPermissions int64 = discordgo.PermissionManageServer
*/
// Bot commands
commands = []*discordgo.ApplicationCommand{
{
// Basic command for testing/learning
Name: "basic-command",
Description: "A basic test command",
},
{
// Crab talk command
Name: "crabtalk",
Description: "Translate your message to crab!",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "text-to-translate",
Description: "Message to translate",
Required: true,
},
},
},
}
// Handles what to do when a command is ran
commandHandlers = map[string]func(session *discordgo.Session, interaction *discordgo.InteractionCreate){
// basic-command handler
"basic-command": func(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "This is a basic test command.",
},
})
},
"crabtalk": func(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
// Access input
options := interaction.ApplicationCommandData().Options
// Convert slice (options) into map
optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(options))
for _, opt := range options {
optionMap[opt.Name] = opt
}
margs := make([]interface{}, 0, len(options))
msgformat := "\nMessage in Crab: \n"
if option, ok := optionMap["text-to-translate"]; ok {
margs = append(margs, option.StringValue())
margsString := fmt.Sprint(margs)
translatedText, _ := crabtalk.Get(margsString)
msgformat += markdown.Blockquote(translatedText)
msgformat += "\n"
}
session.InteractionRespond(interaction.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: msgformat,
},
})
},
}
)
// Add handlers
func init() {
session.AddHandler(func(session *discordgo.Session, interaction *discordgo.InteractionCreate) {
// Confirm handlers are added correctly
if h, ok := commandHandlers[interaction.ApplicationCommandData().Name]; ok {
h(session, interaction)
}
})
}
func main() {
// Logged in notification
session.AddHandler(func(session *discordgo.Session, ready *discordgo.Ready) {
log.Printf("Logged in as: %v#%v\n", session.State.User.Username, session.State.User.Discriminator)
})
// Throw error if session can't be opened
err := session.Open()
if err != nil {
log.Fatalln("Can't open the session:", err)
}
// Create commands
log.Println("Adding commands...")
registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
// Loop for adding each interaction
for i, v := range commands {
cmd, err := session.ApplicationCommandCreate(session.State.User.ID, *GuildID, v)
if err != nil {
log.Panicf("Can't create '%v' command: %v\n", v.Name, err)
}
registeredCommands[i] = cmd
}
defer session.Close()
// Handles ending bot session
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
log.Println("Bot is running... (Press CTRL-C to exit)")
<-stop
// Check if we want to remove commands
if *RemoveCommands {
log.Println("Removing commands...")
}
// Loop to delete commands
for _, v := range registeredCommands {
err := session.ApplicationCommandDelete(session.State.User.ID, *GuildID, v.ID)
if err != nil {
log.Panicf("Can't delete '%v' command: %v\n", v.Name, err)
}
}
// Shut down log
log.Println("Gracefully shutting down.")
}