-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
205 lines (179 loc) · 5.73 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
package main
import (
"os"
"os/signal"
"syscall"
"log"
"strconv"
"fmt"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/Team-Alua/kat/discord"
"github.com/Team-Alua/kat/cmdline"
"github.com/Team-Alua/kat/userfs"
)
// Variables used for command line parameters
var (
Token string
)
var requests chan discord.ClientRequest
func init() {
data, err := os.ReadFile("token")
if err != nil {
panic(err)
}
Token = strings.TrimSpace(string(data))
}
func RequestHandler(ch <-chan discord.ClientRequest) {
respChan := make(chan string)
states := make(map[string]chan discord.ClientRequest)
active := make(map[string]bool)
for {
select {
case cr := <-ch:
id := cr.Message.ChannelID + "_" + cr.Message.Author.ID
if _, ok := states[id]; !ok {
states[id] = make(chan discord.ClientRequest, 10)
}
// Give user their own goroutine
if _, ok := active[id]; !ok {
active[id] = false
// Move all this into a new object
crc := states[id]
rc := respChan
s := cr.Session
c := cr.Message
go InterpreterLoop(crc, rc, s, c)
}
select {
case states[id] <- cr:
default:
// Do nothing
}
case id := <-respChan:
delete(active, id)
close(states[id])
delete(states, id)
}
}
}
func StartRequestListener() {
requests = make(chan discord.ClientRequest)
go RequestHandler(requests)
}
func main() {
debug := true
if debug {
rw := cmdline.NewReadWriter()
mfs, err := userfs.Create("1234")
if err != nil {
rw.WriteString(err.Error())
return
}
InterpreterLoopMain("default", mfs, rw)
return
}
// Create a new Discord session using the provided bot token.
dg, err := discordgo.New("Bot " + Token)
if err != nil {
log.Fatalf("error creating Discord session, %s", err)
return
}
dg.AddHandler(messageCreate)
dg.Identify.Intents = discordgo.IntentsGuildMessages
dg.Identify.Intents |= discordgo.IntentsGuilds
// Open a websocket connection to Discord and begin listening.
err = dg.Open()
if err != nil {
log.Fatalf("error opening connection, %s", err)
return
}
StartRequestListener()
// Restrict access to
// Wait here until CTRL-C or other term signal is received.
log.Printf("Bot is now running.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
log.Printf("Killing...")
log.Printf("Waiting for all remaining ")
// Cleanly close down the Discord session.
dg.Close()
}
// This function will be called (due to AddHandler above) every time a new
// message is created on any channel that the authenticated bot has access to.
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
msg := strings.TrimSpace(m.Content)
m.Content = msg
ch, _ := s.State.Channel(m.ChannelID);
// Check if user is in their thread
var threadChannel *discordgo.Channel = nil
if IsUserThreadChannel(ch, m.Author) {
threadChannel = ch
m.ChannelID = threadChannel.ID
} else if ch.IsThread() {
// We don't care about this thread
return
} else {
// Check if user already has an active thread
guild, _ := s.State.Guild(m.GuildID)
utn := GetUserThreadChannelName(m.Author)
for _, ch := range guild.Threads {
if ch.Name == utn {
// User already has a thread active
// so ignore new attempts to
// to create a new channel
threadChannel = ch
break
}
}
// We know the current channel
// is not the user thread channel
// We only care about this command
if msg == "+wakeup" {
if threadChannel == nil {
threadChannel = StartBotInteraction(s,m)
if threadChannel == nil {
s.ChannelMessageSend(m.ChannelID, "I could not create a new thread. Please make sure all thread permissions are enabled for me. Heading back to sleep.")
} else {
s.ChannelMessageSend(m.ChannelID, "Woken up")
}
} else {
m.ChannelID = threadChannel.ID
s.ChannelMessageSend(m.ChannelID, "Woken up again")
}
}
return
}
// Every message sent will go to another goroutine that will handle
// the stateful ness of this
requests <- discord.NewClientRequest(s,m)
}
func GetUserThreadChannelName(user *discordgo.User) string {
valId, _ := strconv.ParseInt(user.ID, 10, 64)
return "Save Edit " + user.Username + " " + fmt.Sprintf("%X", valId)
}
func IsUserThreadChannel(ch *discordgo.Channel, user *discordgo.User) bool {
if !ch.IsThread() {
return false
}
chName := GetUserThreadChannelName(user)
return ch.Name == chName
}
func StartBotInteraction(s *discordgo.Session , m*discordgo.MessageCreate) *discordgo.Channel {
thread, err := s.MessageThreadStartComplex(m.ChannelID, m.ID, &discordgo.ThreadStart{
Name: GetUserThreadChannelName(m.Author),
AutoArchiveDuration: 60,
Invitable: false,
RateLimitPerUser: 5,
})
if err != nil {
return nil
}
// The bot will now only talk in this thread
m.ChannelID = thread.ID
return thread
}