-
Notifications
You must be signed in to change notification settings - Fork 927
/
ping.go
56 lines (44 loc) · 1.35 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
package ping
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/jonas747/dcmd"
"github.com/jonas747/yagpdb/bot/eventsystem"
"github.com/jonas747/yagpdb/commands"
"github.com/jonas747/yagpdb/common"
)
var Command = &commands.YAGCommand{
CmdCategory: commands.CategoryDebug,
Name: "Ping",
Description: "Shows the latency from the bot to the discord servers.",
LongDescription: "Note that high latencies can be the fault of ratelimits and the bot itself, it's not a absolute metric.",
RunFunc: func(data *dcmd.Data) (interface{}, error) {
return fmt.Sprintf(":PONG;%d", time.Now().UnixNano()), nil
},
}
func HandleMessageCreate(evt *eventsystem.EventData) {
m := evt.MessageCreate()
bUser := common.BotUser
if bUser == nil {
return
}
if bUser.ID != m.Author.ID {
return
}
// ping pong
split := strings.Split(m.Content, ";")
if split[0] != ":PONG" || len(split) < 2 {
return
}
parsed, err := strconv.ParseInt(split[1], 10, 64)
if err != nil {
return
}
taken := time.Duration(time.Now().UnixNano() - parsed)
started := time.Now()
common.BotSession.ChannelMessageEdit(m.ChannelID, m.ID, "Gateway (http send -> gateway receive time): "+taken.String())
httpPing := time.Since(started)
common.BotSession.ChannelMessageEdit(m.ChannelID, m.ID, "HTTP API (Edit Msg): "+httpPing.String()+"\nGateway: "+taken.String())
}