-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprofile.go
60 lines (52 loc) · 1.66 KB
/
profile.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
package main
import (
"context"
"fmt"
"github.com/andersfylling/disgord"
"github.com/meooow25/cfspy/bot"
"github.com/meooow25/cfspy/fetch"
)
// Installs the profile watcher feature. The bot watches for Codeforces profile links and responds
// with an embed containing some profile info.
func installProfileFeature(bot *bot.Bot) {
bot.Client.Logger().Info("Setting up CF profile feature")
bot.OnMessageCreate(maybeHandleProfileURL)
}
func maybeHandleProfileURL(ctx *bot.Context, evt *disgord.MessageCreate) {
go func() {
profileURLMatches := fetch.ParseProfileURLs(evt.Message.Content)
if len(profileURLMatches) == 0 {
return
}
first := profileURLMatches[0]
handleProfileUrl(ctx, first.URL)
}()
}
// Responds on the Discord channel with some user profile information.
func handleProfileUrl(ctx *bot.Context, url string) {
ctx.Logger.Info("Processing profile URL: ", url)
profileInfo, err := fetch.Profile(context.Background(), url)
if err != nil {
err = fmt.Errorf("Error fetching profile from %v: %w", url, err)
ctx.Logger.Error(err)
respondWithError(ctx, err)
return
}
page := bot.NewPage("", makeProfileEmbed(profileInfo))
if err = respondWithOnePagePreview(ctx, page); err != nil {
ctx.Logger.Error(fmt.Errorf("Error sending profile info: %w", err))
}
}
func makeProfileEmbed(p *fetch.ProfileInfo) *disgord.Embed {
desc := p.Rank
if p.Rating != 0 || p.Rank != "Unrated" && p.Rank != "Headquarters" {
desc += fmt.Sprintf("\nRating: %v (max. %v)", p.Rating, p.MaxRating)
}
return &disgord.Embed{
Title: p.Handle,
URL: p.URL,
Thumbnail: &disgord.EmbedThumbnail{URL: p.Avatar},
Color: p.Color,
Description: desc,
}
}