-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
238 lines (223 loc) · 6.04 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package main
import (
"fmt"
"log"
"math/rand"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"github.com/RTradeLtd/dpinner/config"
ipfsapi "github.com/RTradeLtd/go-ipfs-api"
"github.com/RTradeLtd/thc"
"github.com/bwmarrin/discordgo"
)
var (
shell *ipfsapi.Shell
tc *thc.V2
)
func main() {
cfg, err := config.LoadConfig("./config.json")
if err != nil {
log.Fatal(err)
}
if cfg.Discord.Token == "" {
cfg.Discord.Token = os.Getenv("DISCORD_TOKEN")
}
if cfg.Temporal.User == "" {
cfg.Temporal.User = os.Getenv("TEMPORAL_USER")
}
if cfg.Temporal.Pass == "" {
cfg.Temporal.Pass = os.Getenv("TEMPORAL_PASS")
}
if cfg.Temporal.WarpURL == "" {
cfg.Temporal.WarpURL = "https://dev.api.ipfs.temporal.cloud"
}
tc = thc.NewV2(cfg.Temporal.User, cfg.Temporal.Pass, thc.DevURL)
if err := tc.Login(); err != nil {
fmt.Println("failed to login with temporal")
log.Fatal(err)
}
jwt, err := tc.GetJWT()
if err != nil {
fmt.Println("failed to get jwt")
log.Fatal(err)
}
shell = ipfsapi.NewDirectShell(cfg.Temporal.WarpURL)
shell = shell.WithAuthorization(jwt)
// we need to prepend Bot to allow discord
// to assign permissions properly
dg, err := discordgo.New("Bot " + cfg.Discord.Token)
if err != nil {
fmt.Println("failed to authenticate with discord")
log.Fatal(err)
}
dg.AddHandler(messageCreate)
if err := dg.Open(); err != nil {
log.Fatal(err)
}
fmt.Println("bot is now running")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
// 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 autenticated bot has access to.
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
// This isn't required in this specific example but it's a good practice.
if m.Author.ID == s.State.User.ID {
return
}
// parse the message contents based off string fields
args := strings.Fields(m.Content)
if len(args) == 0 {
return
}
// ensure the first field is a valid invocation of dpinner
if args[0] != "!dpinner" {
return
}
// If the message is "ping" reply with "Pong!"
if args[1] == "ping" {
if _, err := s.ChannelMessageSend(m.ChannelID, "Pong!"); err != nil {
fmt.Println(err)
}
return
}
// If the message is "pong" reply with "Ping!"
if args[1] == "pong" {
if _, err := s.ChannelMessageSend(m.ChannelID, "Ping!"); err != nil {
fmt.Println(err)
}
return
}
if args[1] == "upload" {
processUpload(s, m.Attachments, m.ChannelID)
}
if args[1] == "pin" {
for i := 2; i < len(args); i++ {
if err := shell.Pin(args[i]); err != nil {
s.ChannelMessageSend(m.ChannelID, "failed to process pin request(s)")
return
}
}
s.ChannelMessageSend(m.ChannelID, "successfully processed pin request(s)")
}
if args[1] == "help" {
msg := &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{
Name: "dpinner help menu",
},
Description: `available commands are:
upload: upload an attachment to ipfs
pin: pin hash(es) with Temporal
index: index hash(es) with Lens
search: search Lens for ipfs hashes matching your query
`,
Footer: &discordgo.MessageEmbedFooter{
Text: "dpinner makes use of Lens, Temporal, and Warp. Try Temporal at https://temporal.cloud",
},
Fields: []*discordgo.MessageEmbedField{
{
Name: "upload",
Value: `
when uploading an attachment, use a comment of **!dpinner upload**
this will upload all attachments associated with the comment to ipfs
`,
},
{
Name: "pin",
Value: `
the command is **!dpinner upload** and takes a list of hashes
example: **!dpinner upload hash1 hash2 hash3**`,
},
{
Name: "index",
Value: `
the command is **!dpinner index** and takes a list of hashes
example: **!dpinner index hash1 hash2 hash3**`,
},
{
Name: "search",
Value: `
the command is **!dpinner search** and takes a search query
example: **!dpinner search blockchain**`,
},
},
}
s.ChannelMessageSendEmbed(m.ChannelID, msg)
}
if args[1] == "index" {
for i := 2; i < len(args)-1; i++ {
if _, err := tc.IndexHash(args[i], args[len(args)-1] == "reindex=true"); err != nil {
s.ChannelMessageSend(m.ChannelID, "failed to process lens index request(s)")
return
}
}
s.ChannelMessageSend(m.ChannelID, "successfully indexed hashe(s)")
}
if args[1] == "search" {
processLensSearch(s, m.ChannelID, args)
}
}
func processLensSearch(s *discordgo.Session, channelID string, args []string) {
searchArgs := args[2:]
query := strings.Join(searchArgs, " ")
resp, err := tc.SearchLens(query)
if err != nil {
s.ChannelMessageSend(channelID, "failed to submit search query to lens")
return
}
var (
results []string
count int
)
if len(resp.Response.Results) > 10 {
foundResults := make(map[string]bool)
for {
if count == 10 {
break
}
hash := resp.Response.Results[rand.Intn(len(resp.Response.Results))].Doc.Hash
if foundResults[hash] {
continue
}
results = append(results, hash)
foundResults[hash] = true
count++
}
} else {
for _, v := range resp.Response.Results {
results = append(results, v.Doc.Hash)
}
}
output := strings.Join(results, " ")
msg := &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{
Name: "Lens Search Results",
},
Description: output,
}
s.ChannelMessageSendEmbed(channelID, msg)
}
func processUpload(s *discordgo.Session, attachments []*discordgo.MessageAttachment, channelID string) {
for _, v := range attachments {
fmt.Printf("fetching object %+v\n", v)
resp, err := http.Get(v.URL)
if err != nil {
s.ChannelMessageSend(channelID, "failed to process attachments")
return
}
defer resp.Body.Close()
hash, err := shell.Add(resp.Body)
if err != nil {
s.ChannelMessageSend(channelID, "failed to add attachments to ipfs")
return
}
s.ChannelMessageSend(channelID, fmt.Sprintf("the hash of your file is %s", hash))
}
}