-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
89 lines (75 loc) · 1.82 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
package main
import (
"encoding/json"
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"userinfo-api/config"
)
func main() {
config.Load()
token := config.Get("token")
client, err := discordgo.New("Bot " + token)
if err != nil {
log.Fatal(err)
return
}
client.AddHandler(handleAPI)
err = client.Open()
if err != nil {
log.Fatal(err)
return
}
fmt.Println("[BOT] Online")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
_ = client.Close()
}
func handleAPI(s *discordgo.Session, e *discordgo.Ready) {
r := mux.NewRouter()
corsObj:=handlers.AllowedOrigins([]string{"*"})
r.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
_ = json.NewEncoder(writer).Encode(Alive{Alive: true})
}).Methods("GET")
r.HandleFunc("/api/user/{user}", func(writer http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
user, err := s.User(vars["user"])
if err != nil {
if err.Error() == "HTTP 404 Not Found, {\"message\": \"Unknown User\", \"code\": 10013}" {
writer.WriteHeader(http.StatusNotFound)
_ = json.NewEncoder(writer).Encode(UserNotFoundResponse{
Success: false,
Message: "User not found",
})
return
} else {
log.Println(err)
}
}
_ = json.NewEncoder(writer).Encode(Response{
Success: true,
Message: UserResponse{
Tag: user.Username + "#" + user.Discriminator,
Id: user.ID,
Bot: user.Bot,
User: User{
Username: user.Username,
Discriminator: user.Discriminator,
Avatar: user.AvatarURL("128"),
},
},
})
return
}).Methods("GET")
err := http.ListenAndServe(":2011", handlers.CORS(corsObj)(r))
if err != nil {
log.Fatal(err)
}
}