-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathping.go
executable file
·83 lines (74 loc) · 1.82 KB
/
ping.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
package main
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
"github.com/meooow25/cfspy/bot"
)
const (
cfHomeURL = "https://codeforces.com"
timeout = 5 * time.Second
)
func onPingCf(ctx *bot.Context) {
go func() {
if len(ctx.Args) > 1 {
ctx.SendIncorrectUsageMsg()
return
}
timeoutCtx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
req, err := http.NewRequestWithContext(timeoutCtx, http.MethodHead, cfHomeURL, nil)
if err != nil {
ctx.Logger.Error(err) // No reason for new request to fail.
return
}
start := time.Now()
resp, err := http.DefaultClient.Do(req)
if err != nil {
err := err.(*url.Error)
if err.Timeout() {
ctx.Send(fmt.Sprintf("Connecting to <%v> timed out after %v", cfHomeURL, timeout))
} else {
ctx.Send(fmt.Sprintf("Error: %v", err))
}
return
}
lat := time.Since(start).Round(time.Millisecond)
ctx.Send(fmt.Sprintf("Pinged <%v>: Response %v, Latency %v", cfHomeURL, resp.Status, lat))
}()
}
func onPing(ctx *bot.Context) {
go func() {
if len(ctx.Args) > 1 {
ctx.SendIncorrectUsageMsg()
return
}
start := time.Now()
pongMsg, err := ctx.Send("pong!")
if err != nil {
return
}
lat := time.Since(start).Round(time.Millisecond)
ctx.EditMsg(pongMsg, fmt.Sprintf("Latency %v", lat))
}()
}
// Installs the pingcf command.
func installPingCfCommand(b *bot.Bot) {
b.Client.Logger().Info("Setting up pingcf command")
b.AddCommand(&bot.Command{
ID: "pingcf",
Description: "Checks the latency of codeforces.com",
Handler: onPingCf,
})
}
// Installs the ping command.
func installPingCommand(b *bot.Bot) {
b.Client.Logger().Info("Setting up ping command")
b.AddCommand(&bot.Command{
ID: "ping",
Description: "Checks the latency of the Discord REST API",
Handler: onPing,
})
}