-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
294 lines (273 loc) · 9.01 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package main
import (
"bytes"
"flag"
"fmt"
"log"
"math/big"
"os"
"os/signal"
"github.com/bwmarrin/discordgo"
"github.com/ubiq/bishop-discord/nft"
)
var (
// Flags
GuildID = flag.String("guild", "", "Test guild ID. If not passed - bot registers commands globally")
BotToken = flag.String("token", "", "Bot access token")
RpcURL = flag.String("rpcURL", "http://127.0.0.1:8588", "RPC URL")
cryptsters = nft.Collection{
Name: "Cryptsters",
Description: "Cryptsters is a collection of 888 randomly generated and stylistically curated NFTs that exist on the Ubiq blockchain. Cryptsters is licensed under CC0 - You can modify, distribute and perform the work, even for commercial purposes, all without asking permission.",
ImageBaseURL: "https://cryptstersapi.ubiqsmart.com/image",
MetadataBaseURL: "https://cryptstersapi.ubiqsmart.com/cryptster",
JawacampBaseURL: "https://jawacamp.ubiqsmart.com/collections/0xc3d585288d35215754e970a23dd8de37a08e71b7",
CountMax: 888,
Attributes: true,
Revealed: true,
}
)
var s *discordgo.Session
func init() { flag.Parse() }
func init() {
var err error
s, err = discordgo.New("Bot " + *BotToken)
if err != nil {
log.Fatalf("Invalid bot parameters: %v", err)
}
}
var (
commands = []*discordgo.ApplicationCommand{
{
Name: "chimp",
Description: "Retrieves the Chimp NFT based on the supplied Token ID",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionInteger,
Name: "tokenid",
Description: "Token ID",
Required: true,
},
},
},
{
Name: "cryptster",
Description: "Retrieves the NFT based on the supplied Token ID",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionInteger,
Name: "tokenid",
Description: "Token ID",
Required: true,
},
},
},
{
Name: "gb89",
Description: "Retrieves the GB89 NFT based on the supplied Token ID",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionInteger,
Name: "tokenid",
Description: "Token ID",
Required: true,
},
},
},
{
Name: "nception",
Description: "Retrieves the nCeption NFT based on the supplied Token ID",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionInteger,
Name: "tokenid",
Description: "Token ID",
Required: true,
},
},
},
}
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"chimp": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
tokenID := big.NewInt(i.ApplicationCommandData().Options[0].IntValue())
var iNFT nft.NFT
var msgformat string
if tokenID.Cmp(big.NewInt(0)) == -1 {
msgformat = fmt.Sprintf("Invalid Token ID: %d\n", tokenID)
} else {
iNFT = nft.HandleChimp(*RpcURL, tokenID)
if iNFT.Owner.Hex() == "0x0000000000000000000000000000000000000000" {
msgformat = "Unclaimed\n"
}
}
var fields []*discordgo.MessageEmbedField
for key, element := range iNFT.Attributes {
fields = append(fields, &discordgo.MessageEmbedField{Name: key, Value: element})
}
msgembed := discordgo.MessageEmbed{
Title: fmt.Sprintf("CHIMP NFT #%d", tokenID),
URL: "https://chimp.ubiqsmart.com",
Color: 170,
Description: msgformat,
Fields: fields,
Image: &discordgo.MessageEmbedImage{
URL: "attachment://output.png",
},
}
attachment := discordgo.File{
Name: "output.png",
ContentType: "image/png",
Reader: bytes.NewReader(iNFT.Picture),
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{&msgembed},
Files: []*discordgo.File{&attachment},
},
})
},
"cryptster": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
tokenID := i.ApplicationCommandData().Options[0].IntValue()
var iNFT nft.NFT
var msgformat string
if tokenID < 1 || tokenID > int64(cryptsters.CountMax) {
msgformat = fmt.Sprintf("Invalid Token ID: %d\n", tokenID)
} else {
imageURL := fmt.Sprintf("%s/%d", cryptsters.ImageBaseURL, tokenID)
msgformat = fmt.Sprintf("Tweet with hash tag #%s - Image URL: %s", cryptsters.Name, imageURL)
iNFT = cryptsters.HandleCollection(tokenID)
}
var fields []*discordgo.MessageEmbedField
for key, element := range iNFT.Attributes {
fields = append(fields, &discordgo.MessageEmbedField{Name: key, Value: element})
}
msgembed := discordgo.MessageEmbed{
Title: fmt.Sprintf("%s #%d", cryptsters.Name, tokenID),
URL: fmt.Sprintf("%s/%d", cryptsters.JawacampBaseURL, tokenID),
Color: 170,
Description: msgformat,
Fields: fields,
Image: &discordgo.MessageEmbedImage{
URL: "attachment://output.png",
},
}
attachment := discordgo.File{
Name: "output.png",
ContentType: "image/png",
Reader: bytes.NewReader(iNFT.Picture),
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{&msgembed},
Files: []*discordgo.File{&attachment},
},
})
},
"gb89": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
tokenID := big.NewInt(i.ApplicationCommandData().Options[0].IntValue())
var iNFT nft.NFT
var msgformat string
if tokenID.Cmp(big.NewInt(0)) == -1 || tokenID.Cmp(big.NewInt(255)) == 1 {
msgformat = fmt.Sprintf("Invalid Token ID: %d\n", tokenID)
} else {
iNFT = nft.HandleGB89(*RpcURL, tokenID)
if iNFT.Owner.Hex() == "0x0000000000000000000000000000000000000000" {
msgformat = "Unclaimed\n"
}
}
var fields []*discordgo.MessageEmbedField
for key, element := range iNFT.Attributes {
fields = append(fields, &discordgo.MessageEmbedField{Name: key, Value: element})
}
msgembed := discordgo.MessageEmbed{
Title: fmt.Sprintf("GB89 NFT #%d", tokenID),
URL: "https://ubiq.github.io/gb89/",
Color: 170,
Description: msgformat,
Fields: fields,
Image: &discordgo.MessageEmbedImage{
URL: "attachment://output.png",
},
}
attachment := discordgo.File{
Name: "output.png",
ContentType: "image/png",
Reader: bytes.NewReader(iNFT.Picture),
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{&msgembed},
Files: []*discordgo.File{&attachment},
},
})
},
"nception": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
tokenID := big.NewInt(i.ApplicationCommandData().Options[0].IntValue())
var iNFT nft.NFT
var msgformat string
if tokenID.Cmp(big.NewInt(1)) == -1 || tokenID.Cmp(big.NewInt(8888)) == 1 {
msgformat = fmt.Sprintf("Invalid Token ID: %d\n", tokenID)
} else {
iNFT = nft.HandleNception(*RpcURL, tokenID)
if iNFT.Owner.Hex() == "0x0000000000000000000000000000000000000000" {
msgformat = "Unclaimed\n"
}
}
var fields []*discordgo.MessageEmbedField
for key, element := range iNFT.Attributes {
fields = append(fields, &discordgo.MessageEmbedField{Name: key, Value: element})
}
msgembed := discordgo.MessageEmbed{
Title: fmt.Sprintf("nCeption NFT #%d", tokenID),
URL: "https://nception.ubiqsmart.com",
Color: 170,
Description: msgformat,
Fields: fields,
Image: &discordgo.MessageEmbedImage{
URL: "attachment://output.png",
},
}
attachment := discordgo.File{
Name: "output.png",
ContentType: "image/png",
Reader: bytes.NewReader(iNFT.Picture),
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{&msgembed},
Files: []*discordgo.File{&attachment},
},
})
},
}
)
func init() {
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
})
}
func main() {
s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Println("Bot is up!")
})
err := s.Open()
if err != nil {
log.Fatalf("Cannot open the session: %v", err)
}
for _, v := range commands {
_, err := s.ApplicationCommandCreate(s.State.User.ID, *GuildID, v)
if err != nil {
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
}
}
defer s.Close()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
<-stop
log.Println("Gracefully shutting down")
}