-
Notifications
You must be signed in to change notification settings - Fork 56
/
npm.go
119 lines (100 loc) · 2.58 KB
/
npm.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
package developer
import (
"encoding/json"
"fmt"
"github.com/dustin/go-humanize"
"github.com/sapphire-cord/sapphire"
"io/ioutil"
"net/http"
"strings"
"time"
)
// Search the NPM registry for information about a package.
// Usage: <package:string>
// Aliases: npmpackage, npmpkg, nodepackagemanager
// Cooldown: 5
func NPM(ctx *sapphire.CommandContext) {
res, err := http.Get("https://registry.npmjs.com/" + ctx.Arg(0).AsString())
if err != nil {
ctx.Error(err)
return
}
defer res.Body.Close()
if res.StatusCode != 200 {
ctx.Reply("No results found.")
return
}
buf, err := ioutil.ReadAll(res.Body)
if err != nil {
ctx.Error(err)
return
}
var data struct {
License string `json:"license"`
Description string `json:"description"`
Tags struct {
Latest string `json:"string"`
} `json:"dist-tags"`
Versions map[string]struct {
Dependencies map[string]interface{} `json:"dependencies"`
} `json:"versions"`
Maintainers []struct {
Name string `json:"name"`
} `json:"maintainers"`
Author struct {
Name string `json:"name"`
} `json:"author"`
Time struct {
Modified time.Time `json:"modified"`
} `json:"time"`
}
err = json.Unmarshal(buf, &data)
if err != nil {
ctx.Error(err)
return
}
version := data.Versions[data.Tags.Latest]
description := "No Description."
if data.Description != "" {
description = data.Description
}
author := "Unknown"
if data.Author.Name != "" {
author = data.Author.Name
}
maintainers := make([]string, 0)
deps := make([]string, 0)
for k, _ := range version.Dependencies {
deps = append(deps, k)
}
for _, v := range data.Maintainers {
maintainers = append(maintainers, v.Name)
}
if len(maintainers) > 10 {
length := len(maintainers) - 10
maintainers = maintainers[:10]
maintainers = append(maintainers, fmt.Sprintf("...%d more.", length))
}
if len(deps) > 10 {
length := len(deps) - 10
deps = deps[:10]
deps = append(deps, fmt.Sprintf("...%d more.", length))
}
if len(deps) == 0 {
deps = append(deps, "None")
}
ctx.BuildEmbed(sapphire.NewEmbed().
SetTitle("NPM - "+ctx.Arg(0).AsString()).
SetColor(0xDFAC7C).
SetURL("https://npmjs.com/package/"+ctx.Arg(0).AsString()).
SetAuthor(ctx.Author.Username, ctx.Author.AvatarURL("256")).
SetDescription(fmt.Sprintf("%s\n\n❯ **Version:** %s\n❯ **License:** %s\n❯ **Author:** %s\n❯ **Modified:** %s\n❯ **Dependencies:** %s\n❯ **Maintainers:** %s",
description,
data.Tags.Latest,
data.License,
author,
humanize.Time(data.Time.Modified),
strings.Join(deps, ", "),
strings.Join(maintainers, ", "),
)))
}