-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavina.go
227 lines (218 loc) · 6.22 KB
/
avina.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"github.com/bwmarrin/discordgo"
"log"
"log/slog"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
)
var (
commands = []*discordgo.ApplicationCommand{
{
Name: "roll",
Description: "Rolls a dice",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "amount",
Description: "Amount of dice to roll, n to roll n CoD dice, 'ndx' to roll n x-sided dice",
Required: true,
},
{
Type: discordgo.ApplicationCommandOptionString,
Name: "modifier",
Description: "Modifier to add to the roll",
Required: false,
},
},
},
{
Name: "shadowroll",
Description: "Rolls dice using Shadowrun rules",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionInteger,
Name: "amount",
Description: "Amount of dice to roll",
Required: true,
},
{
Type: discordgo.ApplicationCommandOptionBoolean,
Name: "exploding",
Description: "Whether to explode 6s"},
},
},
}
commandsRegistered = map[string]bool{}
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"roll": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
options := i.ApplicationCommandData().Options
amount := options[0].StringValue()
var modifier string
if len(options) > 1 {
modifier = options[1].StringValue()
} else {
modifier = ""
}
response, err := roll(amount, modifier)
if err != nil {
log.Printf("Error rolling dice: %v", err)
response = err.Error()
}
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: response,
},
})
if err != nil {
log.Println(err)
}
},
"shadowroll": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
amountStr := i.ApplicationCommandData().Options[0].StringValue()
exploding := i.ApplicationCommandData().Options[1].BoolValue()
amount, err := strconv.Atoi(amountStr)
if err != nil {
log.Println(err)
return
}
response := shadowroll(amount, exploding)
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: response,
},
})
if err != nil {
log.Println(err)
}
},
}
)
type Bot struct {
token string
log *slog.Logger
AppID string
}
func NewBot(logger *slog.Logger, token string, appID string) *Bot {
return &Bot{
token: token,
log: logger,
AppID: appID,
}
}
func (b *Bot) Start() error {
dg, err := discordgo.New("Bot " + b.token)
if err != nil {
return err
}
dg.AddHandler(b.messageCreate)
dg.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if i.Type == discordgo.InteractionApplicationCommand {
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
// TODO convert types etc here for backwards compatibility?
h(s, i)
}
}
})
// In this example, we only care about receiving message events.
dg.Identify.Intents = discordgo.IntentsGuildMessages
dg.Identify.Intents |= discordgo.IntentsDirectMessages
// Open a websocket connection to Discord and begin listening.
err = dg.Open()
if err != nil {
return err
}
// Wait here until CTRL-C or other term signal is received.
b.log.Info("Avina bot was started")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
// Cleanly close down the Discord session.
return dg.Close()
}
func (b *Bot) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
if !commandsRegistered[m.GuildID] {
b.log.Info("Registering commands", "guild", m.GuildID)
_, err := s.ApplicationCommandBulkOverwrite(b.AppID, m.GuildID, commands)
if err != nil {
b.log.Error("Error creating application command", "error", err)
return
}
commandsRegistered[m.GuildID] = true
b.log.Info("Commands registered", "guild", m.GuildID)
}
// get first word
words := strings.Split(m.Content, " ")
if len(words) > 0 {
switch words[0] {
case "roll", "r":
if len(words) > 1 {
var amount, modifier string
amount = words[1]
if len(words) > 2 {
modifier = words[2]
}
response, err := roll(amount, modifier)
if err != nil {
log.Printf("Error rolling dice: %v", err)
response = err.Error()
}
response = m.Author.Mention() + "\n" + response
send, err := s.ChannelMessageSend(m.ChannelID, response)
if err != nil {
b.log.Error("Error sending message", "error", err, "message", send)
}
} else {
send, err := s.ChannelMessageSend(m.ChannelID, "Please specify amount and optional modifier")
if err != nil {
b.log.Error("Error sending message", "error", err, "message", send)
}
}
case "shadowroll", "sr":
if len(words) > 1 {
var amountStr string
amountStr = words[1]
exploding := false
if len(words) > 2 {
switch words[2] {
case "e", "exploding", "edge", "!", "!!":
exploding = true
default:
send, err := s.ChannelMessageSend(m.ChannelID, "Please specify amount and optional exploding flag")
if err != nil {
b.log.Error("Error sending message", "error", err, "message", send)
}
return
}
}
amount, err := strconv.Atoi(amountStr)
if err != nil {
send, err := s.ChannelMessageSend(m.ChannelID, "Please specify amount as a number")
if err != nil {
b.log.Error("Error sending message", "error", err, "message", send)
}
return
}
response := shadowroll(amount, exploding)
response = m.Author.Mention() + "\n" + response
send, err := s.ChannelMessageSend(m.ChannelID, response)
if err != nil {
b.log.Error("Error sending message", "error", err, "message", send)
}
} else {
send, err := s.ChannelMessageSend(m.ChannelID, "Please specify amount")
if err != nil {
b.log.Error("Error sending message", "error", err, "message", send)
}
}
}
}
}