/
cmd-github.go
183 lines (160 loc) · 7.25 KB
/
cmd-github.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
package main
import (
"context"
"strconv"
"strings"
"github.com/andygrunwald/go-trending"
"github.com/bwmarrin/discordgo"
"github.com/google/go-github/github"
)
func commandGitHub(args []string, env *CommandEnvironment) *discordgo.MessageEmbed {
switch args[0] {
case "trend", "trends", "trending":
if len(args) <= 1 {
return NewErrorEmbed("GitHub Error", "Not enough arguments. Type ``cli$help "+env.Command+"`` for command usage.")
}
time := ""
if len(args) >= 3 {
switch args[2] {
case "daily", "today":
time = "daily"
case "weekly", "week":
time = "weekly"
case "monthly", "month":
time = "monthly"
default:
return NewErrorEmbed("GitHub Error", "Invalid trending time ``"+args[2]+"``. Type ``cli$help "+env.Command+"`` for command usage.")
}
}
language := ""
if len(args) >= 4 {
language = args[3]
}
switch args[1] {
case "repo", "repos", "repository", "repositories":
projects, err := trending.NewTrending().GetProjects(time, language)
if err != nil {
return NewErrorEmbed("GitHub Error", "There was an error fetching the trending repositories.")
}
trendingEmbed := NewEmbed().
SetTitle("GitHub Trending").
SetDescription("These projects are currently trending.").
SetColor(0x24292D)
for i, project := range projects {
details := "Project [" + project.RepositoryName + "](https://github.com/" + project.Owner + "/" + project.RepositoryName + ") created by [" + project.Owner + "](https://github.com/" + project.Owner + ")"
if project.Language != "" {
details += ", written in ``" + project.Language + "``."
} else {
details += "."
}
trendingEmbed.AddField("Project #"+strconv.Itoa(i+1)+": "+project.RepositoryName, details)
}
return trendingEmbed.MessageEmbed
case "user", "users":
developers, err := trending.NewTrending().GetDevelopers(time, language)
if err != nil {
return NewErrorEmbed("GitHub Error", "There was an error fetching the trending developers.")
}
trendingEmbed := NewEmbed().
SetTitle("GitHub Trending").
SetDescription("These developers are currently trending.").
SetColor(0x24292D)
for i, developer := range developers {
trendingEmbed.AddField("Developer #"+strconv.Itoa(i+1)+": "+developer.DisplayName, "Developer "+developer.FullName+", AKA ["+developer.DisplayName+"](https://github.com/"+developer.DisplayName+").")
}
return trendingEmbed.MessageEmbed
default:
return NewErrorEmbed("GitHub Error", "Invalid trending type ``"+args[1]+"``. Type ``cli$help "+env.Command+"`` for command usage.")
}
default:
request := strings.Split(args[0], "/")
switch len(request) {
case 1: //Only user was specified
user, err := GitHubFetchUser(request[0])
if err != nil {
return NewErrorEmbed("GitHub Error", "There was an error fetching information about the specified user.")
}
fields := []*discordgo.MessageEmbedField{}
//Gather user info
if user.Bio != nil {
fields = append(fields, &discordgo.MessageEmbedField{Name: "Bio", Value: *user.Bio})
}
fields = append(fields, &discordgo.MessageEmbedField{Name: "Username", Value: *user.Login})
if user.Name != nil {
fields = append(fields, &discordgo.MessageEmbedField{Name: "Name", Value: *user.Name})
}
if user.Company != nil {
fields = append(fields, &discordgo.MessageEmbedField{Name: "Company", Value: *user.Company})
}
if *user.Blog != "" {
fields = append(fields, &discordgo.MessageEmbedField{Name: "Blog", Value: *user.Blog})
}
if user.Location != nil {
fields = append(fields, &discordgo.MessageEmbedField{Name: "Location", Value: *user.Location})
}
fields = append(fields, &discordgo.MessageEmbedField{Name: "Public Repos", Value: strconv.Itoa(*user.PublicRepos)})
fields = append(fields, &discordgo.MessageEmbedField{Name: "Public Gists", Value: strconv.Itoa(*user.PublicGists)})
fields = append(fields, &discordgo.MessageEmbedField{Name: "Following", Value: strconv.Itoa(*user.Following)})
fields = append(fields, &discordgo.MessageEmbedField{Name: "Followers", Value: strconv.Itoa(*user.Followers)})
fields = append(fields, &discordgo.MessageEmbedField{Name: "GitHub URL", Value: *user.HTMLURL})
//Build embed about user
responseEmbed := NewEmbed().
SetTitle("GitHub User: " + *user.Login).
SetImage(*user.AvatarURL).
SetColor(0x24292D)
responseEmbed.Fields = fields
responseEmbed.InlineAllFields()
return responseEmbed.MessageEmbed
case 2: //Repo was specified
repo, err := GitHubFetchRepo(request[0], request[1])
if err != nil {
return NewErrorEmbed("GitHub Error", "There was an error fetching information about the specified repo.")
}
fields := []*discordgo.MessageEmbedField{}
//Gather repo info
if repo.Description != nil && *repo.Description != "" {
fields = append(fields, &discordgo.MessageEmbedField{Name: "Description", Value: *repo.Description})
}
fields = append(fields, &discordgo.MessageEmbedField{Name: "Name", Value: *repo.FullName})
if repo.Homepage != nil && *repo.Homepage != "" {
fields = append(fields, &discordgo.MessageEmbedField{Name: "Homepage", Value: *repo.Homepage})
}
if len(repo.Topics) > 0 {
fields = append(fields, &discordgo.MessageEmbedField{Name: "Topics", Value: strings.Join(repo.Topics, ", ")})
}
fields = append(fields, &discordgo.MessageEmbedField{Name: "Default Branch", Value: *repo.DefaultBranch})
fields = append(fields, &discordgo.MessageEmbedField{Name: "Is Fork", Value: strconv.FormatBool(*repo.Fork)})
fields = append(fields, &discordgo.MessageEmbedField{Name: "Forks", Value: strconv.Itoa(*repo.ForksCount)})
fields = append(fields, &discordgo.MessageEmbedField{Name: "Networks", Value: strconv.Itoa(*repo.NetworkCount)})
fields = append(fields, &discordgo.MessageEmbedField{Name: "Open Issues", Value: strconv.Itoa(*repo.OpenIssuesCount)})
fields = append(fields, &discordgo.MessageEmbedField{Name: "Stargazers", Value: strconv.Itoa(*repo.StargazersCount)})
fields = append(fields, &discordgo.MessageEmbedField{Name: "Subscribers", Value: strconv.Itoa(*repo.SubscribersCount)})
fields = append(fields, &discordgo.MessageEmbedField{Name: "Watchers", Value: strconv.Itoa(*repo.WatchersCount)})
fields = append(fields, &discordgo.MessageEmbedField{Name: "GitHub URL", Value: *repo.HTMLURL})
fields = append(fields, &discordgo.MessageEmbedField{Name: "Clone URL", Value: *repo.CloneURL})
fields = append(fields, &discordgo.MessageEmbedField{Name: "Git URL", Value: *repo.GitURL})
//Build embed about repo
responseEmbed := NewEmbed().
SetTitle("GitHub Repo: " + *repo.FullName).
SetColor(0x24292D)
responseEmbed.Fields = fields
responseEmbed.InlineAllFields()
return responseEmbed.MessageEmbed
}
return NewErrorEmbed("GitHub Error", "Not enough arguments. Type ``cli$help "+env.Command+"`` for command usage.")
}
}
func GitHubFetchUser(username string) (*github.User, error) {
user, _, err := botData.BotClients.GitHub.Users.Get(context.Background(), username)
if err != nil {
return nil, err
}
return user, nil
}
func GitHubFetchRepo(owner string, repository string) (*github.Repository, error) {
repo, _, err := botData.BotClients.GitHub.Repositories.Get(context.Background(), owner, repository)
if err != nil {
return nil, err
}
return repo, nil
}