-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproblem.go
57 lines (49 loc) · 1.61 KB
/
problem.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
package main
import (
"context"
"fmt"
"github.com/andersfylling/disgord"
"github.com/meooow25/cfspy/bot"
"github.com/meooow25/cfspy/fetch"
)
// Installs the problem watcher feature. The bot watches for Codeforces problem links and responds
// with an embed containing info about the problem.
func installProblemFeature(bot *bot.Bot) {
bot.Client.Logger().Info("Setting up CF problem feature")
bot.OnMessageCreate(maybeHandleProblemURL)
}
func maybeHandleProblemURL(ctx *bot.Context, evt *disgord.MessageCreate) {
go func() {
problemURLMatches := fetch.ParseProblemURLs(evt.Message.Content)
if len(problemURLMatches) == 0 {
return
}
first := problemURLMatches[0]
handleProblemURL(ctx, first.URL)
}()
}
// Fetches the problem page and responds on the Discord channel with some basic info on the problem.
// TODO: Maybe show a preview of the statement like DMOJ.
func handleProblemURL(ctx *bot.Context, problemURL string) {
ctx.Logger.Info("Processing problem URL: ", problemURL)
problemInfo, err := fetch.Problem(context.Background(), problemURL)
if err != nil {
err = fmt.Errorf("Error fetching problem from %v: %w", problemURL, err)
ctx.Logger.Error(err)
respondWithError(ctx, err)
return
}
page := bot.NewPage("", makeProblemEmbed(problemInfo))
if err = respondWithOnePagePreview(ctx, page); err != nil {
ctx.Logger.Error(fmt.Errorf("Error sending problem info: %w", err))
}
}
func makeProblemEmbed(p *fetch.ProblemInfo) *disgord.Embed {
return &disgord.Embed{
Title: p.Name,
URL: p.URL,
Author: &disgord.EmbedAuthor{
Name: fmt.Sprintf("%s [%s]", p.ContestName, p.ContestStatus),
},
}
}