-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiktok_upload_bot.go
58 lines (52 loc) · 1.68 KB
/
tiktok_upload_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
package main
import (
"fmt"
"io"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/bwmarrin/discordgo"
)
func main() {
token := os.Getenv("DISCORD_BOT_TOKEN")
targetChannelID := os.Getenv("DISCORD_TARGET_CHANNEL_ID")
session, err := discordgo.New(fmt.Sprintf("Bot %s", token))
if err != nil {
fmt.Printf("unable to create discord bot session, see: %s", err)
return
}
session.AddHandler(func(s *discordgo.Session, m *discordgo.MessageReactionAdd) {
if m.MessageReaction.Emoji.Name == "✅" && m.ChannelID == targetChannelID {
msg, err := session.ChannelMessage(targetChannelID, m.MessageID)
if err != nil {
session.ChannelMessageSend(targetChannelID, fmt.Sprintf("something went wrong, with message retrieval, see: %s", err))
} else {
if len(msg.Attachments) != 1 {
session.ChannelMessageSend(targetChannelID, "Please ensure you only attach on file per upload.")
return
}
url := msg.Attachments[0].URL
session.ChannelMessageSend(targetChannelID, fmt.Sprintf("url of file to upload: %s", url))
resp, err := http.Get(url)
if err != nil {
session.ChannelMessageSend(targetChannelID, "Failed to retrieve blob from CDN. Exiting upload process.")
return
}
respBlob, err := io.ReadAll(resp.Body)
resp.Body.Close()
session.ChannelMessageSend(targetChannelID, fmt.Sprintf("blob buffered, size: %d", len(respBlob)))
}
}
})
err = session.Open()
if err != nil {
fmt.Printf("unable to open discord bot session, see: %s", err)
return
}
fmt.Println("Bot running... Press CTRL+C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
session.Close()
}