-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.go
179 lines (158 loc) · 4.47 KB
/
bot.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
package main
import (
"log"
"os"
"regexp"
"github.com/bwmarrin/discordgo"
"gopkg.in/yaml.v3"
)
type Prefs struct {
ActiveSession *DiscordTerminal `yaml:"-"`
CanOpen bool `yaml:"canopen"`
DefaultSharedUsers []string `yaml:"defaultsharedusers"`
Interactive bool `yaml:"interactive"`
AutoSubmit bool `yaml:"autosubmit"`
}
type Macro struct {
Name string `yaml:"name"`
In string `yaml:"in"`
Whitelist bool `yaml:"whitelist"`
AllowedIDs []string `yaml:"allowedids"`
}
type Config struct {
Token string `yaml:"token"`
Prefix string `yaml:"prefix"`
OwnerID string `yaml:"ownerid"`
Macros []Macro `yaml:"macros"`
UserPrefs map[string]*Prefs `yaml:"userprefs"`
}
type Bot struct {
Config Config
Session *discordgo.Session
Terminals []*DiscordTerminal
}
func (bot *Bot) RespondString(i *discordgo.Interaction, msg string) {
bot.Session.InteractionRespond(i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: msg,
Flags: uint64(discordgo.MessageFlagsEphemeral),
},
})
}
func (bot *Bot) CreatePrefIfNotExistsFor(user *discordgo.User) {
if _, ok := bot.Config.UserPrefs[user.ID]; !ok {
bot.Config.UserPrefs[user.ID] = &Prefs{
ActiveSession: nil,
CanOpen: false,
DefaultSharedUsers: []string{},
Interactive: false,
AutoSubmit: false,
}
}
}
func (bot *Bot) MessageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore self
if m.Author.ID == s.State.User.ID {
return
}
bot.CreatePrefIfNotExistsFor(m.Author)
msg := [][]string{{"", ""}}
if !bot.Config.UserPrefs[m.Author.ID].Interactive {
prefix := regexp.MustCompile(`^\` + bot.Config.Prefix + `(.+)`)
msg = prefix.FindAllStringSubmatch(m.Content, -1) // No prefix shall be there
} else {
msg[0][1] = m.Content
}
if msg != nil {
instr := ParseSequences(msg[0][1])
t := bot.Config.UserPrefs[m.Author.ID].ActiveSession
if t == nil {
if bot.Config.UserPrefs[m.Author.ID].Interactive {
// Ignore other channels
return
}
bot.Session.ChannelMessageSend(m.ChannelID, "No active session")
return
}
if m.ChannelID == t.Msg.ChannelID {
if bot.Config.UserPrefs[m.Author.ID].AutoSubmit {
instr += "\r"
}
t.Pty.WriteString(instr) // Write message input to the terminal's pty
} else {
if bot.Config.UserPrefs[m.Author.ID].Interactive {
// Ignore other channels
return
}
chn, _ := bot.Session.Channel(t.Msg.ChannelID)
bot.Session.ChannelMessageSend(m.ChannelID, "Your active session is in another channel ("+chn.Mention()+")")
}
err := bot.Session.ChannelMessageDelete(m.ChannelID, m.Message.ID) // Remove user's command msg
if err != nil {
log.Printf("Cannot remove user command message: %s", err)
}
return
}
}
func (bot *Bot) InteractionHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
bot.CreatePrefIfNotExistsFor(i.Member.User)
switch i.Type {
case discordgo.InteractionApplicationCommand:
bot.CommandHandler(i)
case discordgo.InteractionMessageComponent:
bot.ComponentHandler(i)
}
}
func (bot *Bot) Shutdown() {
for _, t := range bot.Terminals {
t.Close()
}
err := bot.Session.Close()
if err != nil {
log.Panicf("Cannot shutdown bot: %s", err)
}
yamlconf, err := yaml.Marshal(bot.Config)
if err != nil {
log.Panicf("Cannot generate YAML: %s", err)
}
f, err := os.OpenFile("config.yaml", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
log.Panicf("Cannot open config file: %s", err)
}
_, err = f.Write(yamlconf)
if err != nil {
log.Panicf("Cannot save config file: %s", err)
}
}
func NewTerminalBot() *Bot {
config := Config{
UserPrefs: make(map[string]*Prefs),
}
conf, err := os.ReadFile("config.yaml")
if err != nil {
panic(err)
}
if err := yaml.Unmarshal(conf, &config); err != nil {
panic(err)
}
this := Bot{Config: config}
sess, err := discordgo.New("Bot " + config.Token)
if err != nil {
panic(err)
}
this.Session = sess
this.Session.AddHandler(this.MessageHandler)
this.Session.AddHandler(this.InteractionHandler)
this.Session.Identify.Intents = discordgo.IntentMessageContent | discordgo.IntentGuildMessages
err = this.Session.Open()
if err != nil {
panic(err)
}
this.RegisterCommands()
for uid := range this.Config.UserPrefs {
// u, _ := this.Session.User(uid)
log.Println("Loaded preferences for " + uid)
}
return &this
}